How to prevent objects overlay and improper moves?

I have two draggable squares on display.

local square1 = display.newRect(100, 100, 50, 50) etc.

local square2 = display.newRect(200, 200, 50, 50) etc.

How to prevent overlaying of this squares (or any objects and sprites, in common) and to stop move out of screen borders while dragging them? Will be this move stopped by object’s border or center? Here is the illustration:

untitltit.png

I tried to follow Rob Miracle’s tutorial https://code.coronalabs.com/code/detecting-collisions-without-physics and used square1 and square2 instead of obj1 and obj2.

For your object you have three property. The center of the object (x,y) and the width.

To know if two square overlay it’s easy. With a simple if you can know it:

if((math.abs(square1.x-square2.x)\<=(square1.width+square2.width)/2) and (math.abs(square1.y-square2.y)\<=(square1.width+square2.width)/2)) object overlay&nbsp;

Are you searching for a way to find the best location for the object if it’s outside the screen or over an other object ?

If it’s what you are looking for, here is the code for the most logical location of your object

testinbatiment is finger position ancienxvalide is the old x of the autorise localisation ancienyvalide is the old y of the autorise location local distancemin=(testinbatiment.x-ancienxvalide)\*(testinbatiment.x-ancienxvalide)+(testinbatiment.y-ancienyvalide)\*(testinbatiment.y-ancienyvalide)-- distance between you finger and the previous object localisation if distancemin\>1000 then -- if the distance is a bit big local limitex1carre=ancienxvalide -- define limits to find a logical place for the object local limitex2carre=testinbatiment.x local limitey1carre=ancienyvalide local limitey2carre=testinbatiment.y local minx=limitex1carre local maxx=limitex2carre if minx\>maxx then maxx=limitex1carre minx=limitex2carre end maxx=maxx+60 minx=minx-60 local miny=limitey1carre local maxy=limitey2carre if miny\>maxy then miny=limitey2carre maxy=limitey1carre end maxy=maxy+60 miny=miny-60 local testposbat={} testposbat.x=minx testposbat.y=miny local varrandx=mathfloor((maxx-minx)\*0.2) if varrandx\<10 then -- max precision require varrandx=10 end local varrandy=mathfloor((maxy-miny)\*0.2) if varrandy\<10 then varrandy=10 end while testposbat.x\<maxx do while testposbat.y\<maxy do local distancemintest=(testinbatiment.x-testposbat.x)\*(testinbatiment.x-testposbat.x)+(testinbatiment.y-testposbat.y)\*(testinbatiment.y-testposbat.y) if distancemin\>distancemintest then local autorise1=autorisefunction(testposbat) --verify if the location is autorise if autorise1==0 then distancemin=distancemintest ancienxvalide=testposbat.x ancienyvalide=testposbat.y end end testposbat.y=testposbat.y+varrandy end testposbat.y=miny testposbat.x=testposbat.x+varrandx end end&nbsp;

Thank you, remiduchalard.

I’m looking for the best way to prevent any situations when an object is over another one or could be dragged out of the border of the screen.

For your object you have three property. The center of the object (x,y) and the width.

To know if two square overlay it’s easy. With a simple if you can know it:

if((math.abs(square1.x-square2.x)\<=(square1.width+square2.width)/2) and (math.abs(square1.y-square2.y)\<=(square1.width+square2.width)/2)) object overlay&nbsp;

Are you searching for a way to find the best location for the object if it’s outside the screen or over an other object ?

If it’s what you are looking for, here is the code for the most logical location of your object

testinbatiment is finger position ancienxvalide is the old x of the autorise localisation ancienyvalide is the old y of the autorise location local distancemin=(testinbatiment.x-ancienxvalide)\*(testinbatiment.x-ancienxvalide)+(testinbatiment.y-ancienyvalide)\*(testinbatiment.y-ancienyvalide)-- distance between you finger and the previous object localisation if distancemin\>1000 then -- if the distance is a bit big local limitex1carre=ancienxvalide -- define limits to find a logical place for the object local limitex2carre=testinbatiment.x local limitey1carre=ancienyvalide local limitey2carre=testinbatiment.y local minx=limitex1carre local maxx=limitex2carre if minx\>maxx then maxx=limitex1carre minx=limitex2carre end maxx=maxx+60 minx=minx-60 local miny=limitey1carre local maxy=limitey2carre if miny\>maxy then miny=limitey2carre maxy=limitey1carre end maxy=maxy+60 miny=miny-60 local testposbat={} testposbat.x=minx testposbat.y=miny local varrandx=mathfloor((maxx-minx)\*0.2) if varrandx\<10 then -- max precision require varrandx=10 end local varrandy=mathfloor((maxy-miny)\*0.2) if varrandy\<10 then varrandy=10 end while testposbat.x\<maxx do while testposbat.y\<maxy do local distancemintest=(testinbatiment.x-testposbat.x)\*(testinbatiment.x-testposbat.x)+(testinbatiment.y-testposbat.y)\*(testinbatiment.y-testposbat.y) if distancemin\>distancemintest then local autorise1=autorisefunction(testposbat) --verify if the location is autorise if autorise1==0 then distancemin=distancemintest ancienxvalide=testposbat.x ancienyvalide=testposbat.y end end testposbat.y=testposbat.y+varrandy end testposbat.y=miny testposbat.x=testposbat.x+varrandx end end&nbsp;

Thank you, remiduchalard.

I’m looking for the best way to prevent any situations when an object is over another one or could be dragged out of the border of the screen.