Android 中的碰撞检测
好的,那么在 Android 中检测冲突的最佳方法是什么?我能找到的唯一示例都涉及查找两个对象何时直接位于彼此上方。就我个人而言,我正在考虑使用边界框,但即使如此,我也不知道该怎么做,然后我仍然想要更精确的东西(每个像素?)。
Ok, so whats the best way to detect collisions in Android? The only examples I can find on it all involves finding when the two objects are directly over each other. Personally I was thinking about using bounding boxes, but even that I do not know how to do, and then I still desire something a little more precise (per-pixel?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
edthethird 几乎是正确的。
您想使用
myRectHitbox.intersect(myOtherRectHitbox)
而不是。包含(...)
。.contains(...)
仅当一个 Rect 完全位于另一个 Rect 内部时才会触发,但.intersect(...)
如果有任何像素与另一个矩形重叠,则会触发。如果您使用
RectF
而不是Rect
,您将在支持子像素的设备上获得子像素精度。edthethird is almost right.
You want to use to
myRectHitbox.intersect(myOtherRectHitbox)
instead of. contains(...)
..contains(...)
will only tigger if the one Rect is completely inside another but.intersect(...)
triggers if there is any pixel that overlaps another.And if you use
RectF
instead ofRect
you will get subpixel accuracy on devices that support subpixels.如果您不是该主题的专家,我建议您使用 Box2d 库来完成此操作。它是一个非常完整的物理引擎,对碰撞检测有很好的支持。
有用的链接:http://www.4feets.com /2009/03/2d-physical-on-android-using-box2d/
If you are not an expert on this subject, I recommend you to use the Box2d library to do it. It is a very complete physics engine with very good support for collisions detection.
Useful link: http://www.4feets.com/2009/03/2d-physics-on-android-using-box2d/
两个步骤:
给所有可能碰撞的东西一个“碰撞箱”。在您的对象中保留一个
矩形
,其大小与可绘制对象相同,并且位于同一位置。调用三个
myRectHitbox.contains(...)
之一。您可以通过这种方式检查点或矩形上的碰撞。Two Steps:
Give everything that can collide a "hitbox". Keep a
Rect
in your objects, the same size as the drawable and at the same place.Call one of the three
myRectHitbox.contains(...)
. You can check collisions on points or rects this way.