Reputation: 43
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?).
Upvotes: 2
Views: 4888
Reputation: 1533
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.
Upvotes: 3
Reputation: 6263
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.
Upvotes: 1
Reputation: 2302
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/
Upvotes: 2