Note that there are some explanatory texts on larger screens.

plurals
  1. POCircle-Rectangle collision detection finished exampe
    primarykey
    data
    text
    <p>I need a algorithm for detecting if a circle has hit a square, and I saw this post: <a href="https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection">Circle-Rectangle collision detection (intersection)</a></p> <p>It looks like I should go for ShreevatsaR's answer, but I am a math fool, and I have no idea how to finish the algorithm. Could anyone find the time to make a complete example for me please, I have searched the net for this, and have yet found no working example.</p> <p>Thank you very much<br> Soeren</p> <p>EDIT:</p> <p>Ok here is my attempt. It is not working, it never detects any collisions.</p> <pre><code>typedef struct { double x; double y; } point; typedef struct { point one; point two; } segment; typedef struct { point center; double radius; } circle; typedef struct { point p; int width; int height; point a; point b; point c; point d; } rectangle; double slope(point one, point two) { return (double)(one.y-two.y)/(one.x-two.x); } double distance(point p, segment s) { // Line one is the original line that was specified, and line two is // the line we're constructing that runs through the specified point, // at a right angle to line one. // // if it's a vertical line return the horizontal distance if ( s.one.x == s.two.x) return fabs(s.one.x - p.x); // if it's a horizontal line return the vertical distance if ( s.one.y == s.two.y ) return fabs(s.one.y - p.y); // otherwise, find the slope of the line double m_one = slope(s.one, s.two); // the other slope is at a right angle. double m_two = -1.0 / m_one; // find the y-intercepts. double b_one = s.one.y - s.one.x * m_one; double b_two = p.y - p.x * m_two; // find the point of intersection double x = (b_two - b_one) / (m_one - m_two); double y = m_one * x + b_one; // find the x and y distances double x_dist = x - p.x; double y_dist = y - p.y; // and return the total distance. return sqrt(x_dist * x_dist + y_dist * y_dist); } bool intersectsCircle(segment s, circle c) { return distance(c.center, s) &lt;= c.radius; } bool pointInRectangle(point p, rectangle r) { float right = r.p.x + r.width; float left = r.p.x - r.width; float top = r.p.y + r.height; float bottom = r.p.y - r.height; return ((left &lt;= p.x &amp;&amp; p.x &lt;= right) &amp;&amp; (top &lt;= p.y &amp;&amp; p.y &lt;= bottom)); } bool intersect(circle c, rectangle r) { segment ab; ab.one = r.a; ab.two = r.b; segment bc; ab.one = r.b; ab.two = r.c; segment cd; ab.one = r.c; ab.two = r.d; segment da; ab.one = r.d; ab.two = r.a; return pointInRectangle(c.center, r) || intersectsCircle(ab, c) || intersectsCircle(bc, c) || intersectsCircle(cd, c) || intersectsCircle(da, c); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload