Circle-Rectangle collision detection (intersection)

前端 未结 24 1294
无人共我
无人共我 2020-11-22 02:55

How can I tell whether a circle and a rectangle intersect in 2D Euclidean space? (i.e. classic 2D geometry)

相关标签:
24条回答
  • 2020-11-22 03:15

    I developed this algorithm while making this game: https://mshwf.github.io/mates/

    If the circle touches the square, then the distance between the centerline of the circle and the centerline of the square should equal (diameter+side)/2. So, let's have a variable named touching that holds that distance. The problem was: which centerline should I consider: the horizontal or the vertical? Consider this frame:

    Each centerline gives different distances, and only one is a correct indication to a no-collision, but using our human intuition is a start to understand how the natural algorithm works.

    They are not touching, which means that the distance between the two centerlines should be greater than touching, which means that the natural algorithm picks the horizontal centerlines (the vertical centerlines says there's a collision!). By noticing multiple circles, you can tell: if the circle intersects with the vertical extension of the square, then we pick the vertical distance (between the horizontal centerlines), and if the circle intersects with the horizontal extension, we pick the horizontal distance:

    Another example, circle number 4: it intersects with the horizontal extension of the square, then we consider the horizontal distance which is equal to touching.

    Ok, the tough part is demystified, now we know how the algorithm will work, but how we know with which extension the circle intersects? It's easy actually: we calculate the distance between the most right x and the most left x (of both the circle and the square), and the same for the y-axis, the one with greater value is the axis with the extension that intersects with the circle (if it's greater than diameter+side then the circle is outside the two square extensions, like circle #7). The code looks like:

    right = Math.max(square.x+square.side, circle.x+circle.rad);
    left = Math.min(square.x, circle.x-circle.rad);
    
    bottom = Math.max(square.y+square.side, circle.y+circle.rad);
    top = Math.min(square.y, circle.y-circle.rad);
    
    if (right - left > down - top) {
     //compare with horizontal distance
    }
    else {
     //compare with vertical distance
    }
    
    /*These equations assume that the reference point of the square is at its top left corner, and the reference point of the circle is at its center*/
    
    0 讨论(0)
  • 2020-11-22 03:16

    Here is how I would do it:

    bool intersects(CircleType circle, RectType rect)
    {
        circleDistance.x = abs(circle.x - rect.x);
        circleDistance.y = abs(circle.y - rect.y);
    
        if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
        if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }
    
        if (circleDistance.x <= (rect.width/2)) { return true; } 
        if (circleDistance.y <= (rect.height/2)) { return true; }
    
        cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                             (circleDistance.y - rect.height/2)^2;
    
        return (cornerDistance_sq <= (circle.r^2));
    }
    

    Here's how it works:

    illusration

    1. The first pair of lines calculate the absolute values of the x and y difference between the center of the circle and the center of the rectangle. This collapses the four quadrants down into one, so that the calculations do not have to be done four times. The image shows the area in which the center of the circle must now lie. Note that only the single quadrant is shown. The rectangle is the grey area, and the red border outlines the critical area which is exactly one radius away from the edges of the rectangle. The center of the circle has to be within this red border for the intersection to occur.

    2. The second pair of lines eliminate the easy cases where the circle is far enough away from the rectangle (in either direction) that no intersection is possible. This corresponds to the green area in the image.

    3. The third pair of lines handle the easy cases where the circle is close enough to the rectangle (in either direction) that an intersection is guaranteed. This corresponds to the orange and grey sections in the image. Note that this step must be done after step 2 for the logic to make sense.

    4. The remaining lines calculate the difficult case where the circle may intersect the corner of the rectangle. To solve, compute the distance from the center of the circle and the corner, and then verify that the distance is not more than the radius of the circle. This calculation returns false for all circles whose center is within the red shaded area and returns true for all circles whose center is within the white shaded area.

    0 讨论(0)
  • 2020-11-22 03:17

    The simplest solution I've come up with is pretty straightforward.

    It works by finding the point in the rectangle closest to the circle, then comparing the distance.

    You can do all of this with a few operations, and even avoid the sqrt function.

    public boolean intersects(float cx, float cy, float radius, float left, float top, float right, float bottom)
    {
       float closestX = (cx < left ? left : (cx > right ? right : cx));
       float closestY = (cy < top ? top : (cy > bottom ? bottom : cy));
       float dx = closestX - cx;
       float dy = closestY - cy;
    
       return ( dx * dx + dy * dy ) <= radius * radius;
    }
    

    And that's it! The above solution assumes an origin in the upper left of the world with the x-axis pointing down.

    If you want a solution to handling collisions between a moving circle and rectangle, it's far more complicated and covered in another answer of mine.

    0 讨论(0)
  • 2020-11-22 03:17

    Assuming you have the four edges of the rectangle check the distance from the edges to the center of the circle, if its less then the radius, then the shapes are intersecting.

    if sqrt((rectangleRight.x - circleCenter.x)^2 +
            (rectangleBottom.y - circleCenter.y)^2) < radius
    // then they intersect
    
    if sqrt((rectangleRight.x - circleCenter.x)^2 +
            (rectangleTop.y - circleCenter.y)^2) < radius
    // then they intersect
    
    if sqrt((rectangleLeft.x - circleCenter.x)^2 +
            (rectangleTop.y - circleCenter.y)^2) < radius
    // then they intersect
    
    if sqrt((rectangleLeft.x - circleCenter.x)^2 +
            (rectangleBottom.y - circleCenter.y)^2) < radius
    // then they intersect
    
    0 讨论(0)
  • 2020-11-22 03:18
    1. do a pre-check whether a circle fully encapsulating the rectangle collides with the circle.
    2. check for rectangle corners within the circle.
    3. For each edge, see if there is a line intersection with the circle. Project the center point C onto the line AB to get a point D. If the length of CD is less than radius, there was a collision.
        projectionScalar=dot(AC,AB)/(mag(AC)*mag(AB));
        if(projectionScalar>=0 && projectionScalar<=1) {
            D=A+AB*projectionScalar;
            CD=D-C;
            if(mag(CD)<circle.radius){
                // there was a collision
            }
        }
    
    0 讨论(0)
  • 2020-11-22 03:19

    your sphere and rect intersect IIF
    the distance between the circle-center and one vertex of your rect is smaller than the radius of your sphere
    OR
    the distance between the circle-center and one edge of your rect is smaller than the radius of your sphere ([point-line distance ])
    OR
    the circle center is inside the rect

    point-point distance:

    P1 = [x1,y1]
    P2 = [x2,y2]
    Distance = sqrt(abs(x1 - x2)+abs(y1-y2))
    

    point-line distance:

    L1 = [x1,y1],L2 = [x2,y2] (two points of your line, ie the vertex points)
    P1 = [px,py] some point
    
    Distance d =  abs( (x2-x1)(y1-py)-(x1-px)(y2-y1) ) / Distance(L1,L2)
    


    circle center inside rect:
    take an seperating axis aproach: if there exists a projection onto a line that seperates the rectangle from the point, they do not intersect

    you project the point on lines parallel to the sides of your rect and can then easily determine if they intersect. if they intersect not on all 4 projections, they (the point and the rectangle) can not intersect.

    you just need the inner-product ( x= [x1,x2] , y = [y1,y2] , x*y = x1*y1 + x2*y2 )

    your test would look like that:

    //rectangle edges: TL (top left), TR (top right), BL (bottom left), BR (bottom right)
    //point to test: POI
    
    seperated = false
    for egde in { {TL,TR}, {BL,BR}, {TL,BL},{TR-BR} }:  // the edges
        D = edge[0] - edge[1]
        innerProd =  D * POI
        Interval_min = min(D*edge[0],D*edge[1])
        Interval_max = max(D*edge[0],D*edge[1])
        if not (  Interval_min ≤ innerProd ≤  Interval_max ) 
               seperated = true
               break  // end for loop 
        end if
    end for
    if (seperated is true)    
          return "no intersection"
    else 
          return "intersection"
    end if
    

    this does not assume an axis-aligned rectangle and is easily extendable for testing intersections between convex sets.

    0 讨论(0)
提交回复
热议问题