Android - 位图碰撞检测(旋转)
我已经了解矩形的矩形碰撞检测,但这次我有 2 个旋转位图。 我的意思是,我有普通位图+一个名为“方向”的浮点变量,并告诉位图在绘制时必须沿哪个方向旋转。
但是我如何知道其中两个物体是否相互碰撞呢?如果你能告诉我两个旋转矩形之间的碰撞检测是如何工作的,那就足够了......或者也许你可以发布一些代码......
如果你能提供帮助,谢谢你(如果你花时间阅读这篇文章,谢谢你或思考答案)
I already know about rectangle collision detection with rectangles, but this time I have 2 rotated bitmaps.
I mean, I have normal bitmaps + a float variable called "direction" and telling in which direction the bitmap must be rotated at drawing.
But how do I find out if 2 of those objects are bumping into each other? It would be enought, too, if you could say me how collision detection between 2 rotated rectangles works... Or maybe you could post some code...
Thank you if you can help (and thank you if you spent time on reading this or thinking about an answer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您需要使用分离轴定理来确定两个旋转的矩形是否发生碰撞。但有一种简单的方法可以在使用 SAT 之前判断矩形是否发生碰撞,这样您就不必进行不必要的处理。您可以进行边界圆检查,通过证明矩形的边界圆不相交来证明矩形不相交。
矩形的外接圆共享其中心,并且直径等于矩形的任一对角线的长度。本质上,如果圆不相交,那么矩形也不能相交。
我不确定您如何执行旋转,但如果您使用的是 Shape/Area 类型对象,则可以使用
AffineTransform
执行旋转,然后使用intersects()
在每个旋转对象的区域上检查它们是否发生碰撞,这样您就不必自己实现它。考虑以下示例:显然,您可以修改它以适合您的实现 - 我希望这能为您指明正确的方向。
Generally, you will needto use the Separating Axis Theorem to determine whether two rotated rectangles are colliding. But there is a simple way that you can tell whether the rectangles are colliding prior to using the SAT so that you don't have to do unneccesary processing. You can do a bounding circle check, where you prove that the rectangles dont intersect by proving that their bounding circles dont intersect.
The bounding circle of a rectangle shares its center and has a diameter equal to the length of either diagonal of the rectangle. Essentially, if the circles do not intersect, than the rectangles cannot intersect either.
I'm not sure how you are performing your rotations, but if you are using Shape/Area type objects, you can use
AffineTransform
to perform the rotation and then useintersects()
on the Area of each rotated object to check if they collide, this saves you from implementing it yourself. Consider the following example:You can obviously, modify this to suite your implementation - I'm hoping this points you in the right direction.