Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to prevent objects overlay and improper moves?
Started by srzhnv Jun 01 2018 06:01 AM

2 replies to this topic
collisions overlay
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

srzhnv

[GLOBAL: userInfoPane.html]
srzhnv
  • Observer

  • 10 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 305 posts
  • Corona SDK

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 

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 

  • srzhnv likes this

[TOPIC: post.html]
#3

srzhnv

[GLOBAL: userInfoPane.html]
srzhnv
  • Observer

  • 10 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: collisions, overlay