2

I am currently working on a simple space game for android. My collision detection will be done using rectangular and circular bounding boxes. These bounding boxes need to be able to rotate, so my question is: what is the best to detect a collision between a rotated rectangle and a circle? Thanks for your help :)

4

2 回答 2

0

Ok, I have solved my own problem! There are only two cases when a circle intersects a rectangle: 1. The center of the circle is inside the rectangle 2. The circle is intersecting one of the sides of the rectangle So to check for the collision, I first check if the center of the circle is inside the rectangle, after rotating the center of the circle according to the rotation of the rectangle, to simplify my calculations. If the center of the circle is inside the rectangle, I know there is an intersection, and return true. If the first check returns false, then I check for intersections between each side of the rectangle, and the circle. If there is an intersection I return true. Feel free to comment if anyone wants the code, thanks for the help guys! :)

于 2013-07-16T05:10:36.457 回答
-1

Generally, bounding boxes just define the bounds of an object, (a shape generated by the vertices of an object's max & min's X & Y) - this makes it much simpler to calculate, bounding boxes do not need to rotate as their purpose is met simply as I have explained. If you want to use them for collision detection simply check if the center of the circle plus its radius intersects the rectangle in both axis such as:

public boolean boxintersectscircle(BoundingBox box, BoundingCircle circle) {
    if (box.x > circle.centerx+circle.radius) return false;
    if (box.y > circle.centery+circle.radius) return false;
    if (box.x+box.width < circle.centerx-circle.radius) return false;
    if (box.y+box.height < circle.centery-circle.radius) return false;
    return true;
}

However bounding boxes aren't accurate - they can leave a lot of unoccupied space so if that's a worry for your game (player frustration), Personally I would implement the separating axis theorem or line/circle detection.

于 2013-07-16T00:39:37.690 回答