简体   繁体   English

旋转的2D边界框碰撞

[英]2D bounding box collisions with rotation

I am currently working on a simple space game for android. 我目前正在为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 :) 谢谢你的帮助 :)

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. 圆与矩形相交只有两种情况:1.圆的中心在矩形内2.圆与矩形的一侧相交。为检查碰撞,我首先检查圆的中心根据矩形的旋转旋转了圆心之后,圆位于矩形内部,以简化计算。 If the center of the circle is inside the rectangle, I know there is an intersection, and return true. 如果圆心在矩形内,则知道存在一个交点,并返回true。 If the first check returns false, then I check for intersections between each side of the rectangle, and the circle. 如果第一次检查返回false,则检查矩形的每边与圆之间的交点。 If there is an intersection I return true. 如果有交叉点,则返回true。 Feel free to comment if anyone wants the code, thanks for the help guys! 如果有人需要该代码,请随时发表评论,谢谢您的帮助! :) :)

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. 通常,边界框仅定义对象的边界 (由对象的最大值和最小值的X和Y的顶点生成的形状)-这使计算变得更加简单,边界框无需旋转即可满足其目的就像我已经解释的那样。 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. 但是边界框是不准确的-边界框会留下很多空余的空间,因此如果您的游戏很麻烦(玩家沮丧),我个人会实施分隔轴定理或线/圆检测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM