I'm trying to make a collision detection system for two ImageViews, but I can't seem to figure it out. I've tried doing several things from StackOverflow and other places, but they don't work. One ImageView stays still and the other moves toward it (it's animated, not a sprite). They don't do anything when the collide, though. Do I need to make them sprites or drawables on a canvas or something?
1 Answer
Get information (dimensions, coordinates) of two imageviews, then use rectangle intersection condition. Assume that two rectangles A, B with coordinate of top-left and buttom-right as follow:
Rectangle A: (Xa1, Ya1) ; (Xa2, Ya2)
Rectangle B: (Xb1, Yb1) ; (Xb2, Yb2)
Overlap condition:
((Xb1 - Xa2)(Xb2 - Xa1) <= 0) && ((Yb1 - Ya2)(Yb2 - Ya2) <= 0)
Refer the below site for some guidance:
http://www.kilobolt.com/day-4-collision-detection-part-1.html
It explains bounding shapes that will be used to check for collision. In the case of our robot, we will be using four bounding rectangles.