开发者

Collision Detection In 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开发者_StackOverflow社区 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?).


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 of Rect you will get subpixel accuracy on devices that support subpixels.


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/


Two Steps:

  1. Give everything that can collide a "hitbox". Keep a Rect in your objects, the same size as the drawable and at the same place.

  2. Call one of the three myRectHitbox.contains(...). You can check collisions on points or rects this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜