Wednesday, 11 September 2013

Detecting if rectangles (buttons) overlap on x or y axis

Detecting if rectangles (buttons) overlap on x or y axis

I am currently trying to develop a prototype in android where the user is
able to drag 4 separate buttons around the screen.
The trouble I am having is with collision detection. If one of the buttons
is alongside another, for example, only movement along the Y axis should
be permitted. Similarly, if one of the buttons is touching the top or
bottom of another button, only movement along the X axis should be
allowed.
Something like
for (TouchButton t:myButtons)
{
if(!(t.equals(b)))
{
if((b.getY() >= t.getY() && (b.getY() <= (t.getY() +
t.getMeasuredHeight()))))
{
if((b.getX() >= t.getX() && (b.getX() <= (t.getX() +
t.getMeasuredWidth()))))
{
//dont move
}
}
}
should be able to detect if the buttons are touching across both axis? But
how do I determine if it should then be possible to slide up or across the
object?

No comments:

Post a Comment