How to check if an object is next to another object?

I have two rectangles ,Rect1 and Rect2. I want Rect1 to perform an action only if Rect2 is to the right of Rect1. Is there any ways that i can check whether rect2 is at the right side of Rect1?

Do you need to know if the rectangles are touching or if rect2 is somewhere right of rect1?

Rob

They will be side by side. It’s a grid so I they will be touching.

You should be able to compare their X to do so.
 

local diffX = rect1.x-rect2.x; if diffX \> 0 then &nbsp; --rect2 is at the left of rect1 elseif diffX \< 0 then &nbsp;--rect2 is at the right of rect1 end

@hachisoft’s math will determine if rect2 is right of left of the box, but not exactly adjacent.  Assuming the rectangles are the same size you would need to do if diffX == rect2.width and rect1.y  == rect2.y.

Rob

@Rob Miracle
given that he uses a grid system, he probably doesn’t need to know if the objects are adjacent, that’s why the easy solution (;

If he really needs to check if the objects are touching but not overlapping, it’s enough to use the contentBounds property of the objects such as
 

if rect1.contentBounds.xMax+1 == rect2.contentBounds.xMin then &nbsp;--right side is touching elseif rect1.contentBounds.xMin-1 == rect2.contentBounds.xMax then --left side is touching end

Adjusting it to make it more less sensitive about the “touching” part (:

This seems like it will work. What about if i have multiple instances of the same object? So for example in a 3x3 grid

w | g  | W

w | w | w

w | w | w 

how can I tell the W object to check is the object to the left is a g object? Assuming all the w’s are the same object just different instances? 

This really depends on your code.
Especially if you’re using a grid, you should be able to directly access each point in the grid and see if it contains an object.

If there’s only one G object in the entire scene, it’s not a problem if you make all the W check for it in an enterFrame event for example (the check works the same as the one I already wrote). But if you have several copies of the same objects, you need a way to access them individually, such as looping through your grid and checking which object is in which grid.

I got it to work! Thanks hachisoft and rob miracle for your help! I love this forum!

Do you need to know if the rectangles are touching or if rect2 is somewhere right of rect1?

Rob

They will be side by side. It’s a grid so I they will be touching.

You should be able to compare their X to do so.
 

local diffX = rect1.x-rect2.x; if diffX \> 0 then &nbsp; --rect2 is at the left of rect1 elseif diffX \< 0 then &nbsp;--rect2 is at the right of rect1 end

@hachisoft’s math will determine if rect2 is right of left of the box, but not exactly adjacent.  Assuming the rectangles are the same size you would need to do if diffX == rect2.width and rect1.y  == rect2.y.

Rob

@Rob Miracle
given that he uses a grid system, he probably doesn’t need to know if the objects are adjacent, that’s why the easy solution (;

If he really needs to check if the objects are touching but not overlapping, it’s enough to use the contentBounds property of the objects such as
 

if rect1.contentBounds.xMax+1 == rect2.contentBounds.xMin then &nbsp;--right side is touching elseif rect1.contentBounds.xMin-1 == rect2.contentBounds.xMax then --left side is touching end

Adjusting it to make it more less sensitive about the “touching” part (:

This seems like it will work. What about if i have multiple instances of the same object? So for example in a 3x3 grid

w | g  | W

w | w | w

w | w | w 

how can I tell the W object to check is the object to the left is a g object? Assuming all the w’s are the same object just different instances? 

This really depends on your code.
Especially if you’re using a grid, you should be able to directly access each point in the grid and see if it contains an object.

If there’s only one G object in the entire scene, it’s not a problem if you make all the W check for it in an enterFrame event for example (the check works the same as the one I already wrote). But if you have several copies of the same objects, you need a way to access them individually, such as looping through your grid and checking which object is in which grid.

I got it to work! Thanks hachisoft and rob miracle for your help! I love this forum!