Detecting Rectangle collision with a Circle

前端 未结 3 566
遥遥无期
遥遥无期 2021-01-07 08:45

I have a Circle with a center point (Center_X, Center_Y) and I am detecting if a rectangle falls into it\'s Radius (Radius). How would I be able to perform this task? I ha

3条回答
  •  甜味超标
    2021-01-07 09:32

    You have two common options for this kind of collision detection.

    The first is to understand the ways two 2D objects can collide.

    1. A vertex of one can be inside the other
    2. Their sides can cross (even thought no verice is inside)
    3. One can be completely interior to the other.

    Technically case 1. can only occur if case 2. also occurs, but it is often a cheaper check. Also case 3 is checked by case 1, in the case where both objects vertices are checked.

    I would proceed like this. (as it is in order of cheapness)

    1. Check that their bounding boxes intersect.
    2. Check whether any vertex of the square is inside the
    3. Check if the center of the circle is inside the rectangle
    4. Check for circle - edge intersections.

    The second and more general method is based on the notion of the product / expansion of shapes. This operation allows you to convert the intersection question into a point containment question.

    In this case the circle / rectangle box intersection can be replaced with a check for a point in a rounded rectangle.

提交回复
热议问题