help needed with squares not overlaping

help needed

i have two squares moving in the direction that i swipe them but i need help on making them not go over each other.
here is my code and it was done in galaxy S3 view
[lua]
local whiteBox = display.newRect( 0,0,240, 240)
whiteBox:setReferencePoint( display.CenterReferencePoint )
whiteBox.x = 360; whiteBox.y = 640

local greenBox = display.newRect( 0,0,80, 80)
greenBox:setFillColor( 0, 180, 0)
greenBox:setReferencePoint( display.CenterReferencePoint )
greenBox.x = 280 greenBox.y = 560

local redBox = display.newRect( 0,0,80, 80)
redBox:setFillColor( 255, 0, 0)
redBox:setReferencePoint( display.CenterReferencePoint )
redBox.x = 360; redBox.y = 560

local swiped = false

–function for sliding greenBox
local function slideIt(e)
if e.target.id ~= “nil” then
local greenBox = e.target --Assign e.target as greenBox

if e.phase == “began” then – on began phase set focus and set up for swipe
local parent = greenBox.parent
parent:insert( greenBox )
display.getCurrentStage():setFocus( greenBox, e.id )
greenBox.isFocus = true
xa = e.x; ya = e.y
xPos = greenBox.x
end
if e.phase == “moved” then
xb = e.x; yb = e.y
local xDir = xb - xa – variables to be used to determine the swipe direction
local yDir = yb - ya

if xDir < 20 and xDir > -20 and yDir > 25 and swiped == false then
print(“down”)
if greenBox.y < 720 then – This checks to make sure the green square isn’t too low to transition down
swiped = true
yPos = greenBox.y + 80
transition.to( greenBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end

if yDir < 20 and yDir > -20 and xDir > 25 and swiped == false then
print(“right”)
if greenBox.x < 440 then – This checks to make sure the green square isn’t too far right to transition right
swiped = true
xPos = greenBox.x + 80
transition.to( greenBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
if xDir < 20 and xDir > -20 and yDir < -25 and swiped == false then
print(“up”)
if greenBox.y > 560 then – This checks to make sure the green square isn’t too far up to transition up
swiped = true
yPos = greenBox.y - 80
transition.to( greenBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir < -25 and swiped == false then
print(“left”)
if greenBox.x > 280 then – This checks to make sure the green square isn’t too far left to transition left
swiped = true
xPos = greenBox.x - 80
transition.to( greenBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == “ended” then
display.getCurrentStage():setFocus( greenBox, nil ) – Release the touch focus on “ended” phase
greenBox.isFocus = false
end
return true
end
end

–function for sliding redBox
local function slideIt(e)
if e.target.id ~= “nil” then
local redBox = e.target --Assign e.target as redBox

if e.phase == “began” then – on began phase set focus and set up for swipe
local parent = redBox.parent
parent:insert( redBox )
display.getCurrentStage():setFocus( redBox, e.id )
redBox.isFocus = true
xa = e.x; ya = e.y
xPos = redBox.x
end
if e.phase == “moved” then
xb = e.x; yb = e.y
local xDir = xb - xa – variables to be used to determine the swipe direction
local yDir = yb - ya

if xDir < 20 and xDir > -20 and yDir > 25 and swiped == false then
print(“down”)
if redBox.y < 720 then – This checks to make sure the red square isn’t too low to transition down
swiped = true
yPos = redBox.y + 80
transition.to( redBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end

if yDir < 20 and yDir > -20 and xDir > 25 and swiped == false then
print(“right”)
if redBox.x < 440 then – This checks to make sure the red square isn’t too far right to transition right
swiped = true
xPos = redBox.x + 80
transition.to( redBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
if xDir < 20 and xDir > -20 and yDir < -25 and swiped == false then
print(“up”)
if redBox.y > 560 then – This checks to make sure the red square isn’t too far up to transition up
swiped = true
yPos = redBox.y - 80
transition.to( redBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir < -25 and swiped == false then
print(“left”)
if redBox.x > 280 then – This checks to make sure the red square isn’t too far left to transition left
swiped = true
xPos = redBox.x - 80
transition.to( redBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == “ended” then
display.getCurrentStage():setFocus( redBox, nil ) – Release the touch focus on “ended” phase
redBox.isFocus = false
end
return true
end
end

greenBox:addEventListener( “touch”, slideIt)
redBox:addEventListener( “touch”, slideIt)
[lua] [import]uid: 218976 topic_id: 35570 reply_id: 335570[/import]

Hi @vadimerror.

Just a tip: always put your code between the lua tags, it makes easier for us to read.

To do what you want, just check if the space that you want to move the square is empty or not before moving it.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35570 reply_id: 141386[/import]

How would I do that Renato? [import]uid: 218976 topic_id: 35570 reply_id: 141424[/import]

Enter a less than sign, the word code and a greater than sign: <code> and then at the end close the tag with: </code>
[import]uid: 199310 topic_id: 35570 reply_id: 141430[/import]

You can add both squares to a same groupDisplay and you can create a funcion (lets say called “isSpaceEmpty(x,y)” that receives a position (x,y) and goes thru all elements in the groupDisplay to see if anyone of the squares is in that same position (x,y). The function would return true if no square is in that position and would return false if there is a square there.

So, just include in your if statement that checks “to make sure the red square isn’t too far up to transition up/down/left/right” that funcion call, as example:

[lua]if redBox.y < 720 and isSpaceEmpty(e.x, redBox.y + 80) then[/lua]

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35570 reply_id: 141435[/import]

Check out this blog post:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

It has a function that detects if two rectangles are overlapping. [import]uid: 199310 topic_id: 35570 reply_id: 141440[/import]

okay. i kind of understood it but can you show me how to create the function too please because i cant get it all the way for some reason
thanks [import]uid: 218976 topic_id: 35570 reply_id: 141486[/import]

  
-- rectangle based  
local function hasCollided(obj1, obj2)  
 if obj1 == nil then  
 return false  
 end  
 if obj2 == nil then  
 return false  
 end  
 local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin  
 local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax  
 local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin  
 local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax  
 return (left or right) and (up or down)  
end  
&nbsp;  
-- circle based  
local function hasCollidedCircle(obj1, obj2)  
 if obj1 == nil then  
 return false  
 end  
 if obj2 == nil then  
 return false  
 end  
 local sqrt = math.sqrt  
&nbsp;  
 local dx = obj1.x - obj2.x;  
 local dy = obj1.y - obj2.y;  
&nbsp;  
 local distance = sqrt(dx\*dx + dy\*dy);  
 local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2)  
 if distance \< objectSize then  
 return true  
 end  
 return false  
end  
  
square1 = display.newRect(0,0,100,100)  
square2 = display.newRect(50,50,100,100)  
  
if hasCollided(square1, square2) then   
 print("Squares overlap")  
end  
  

Or something like that. [import]uid: 199310 topic_id: 35570 reply_id: 141607[/import]

it works but only on startup it prints “squares overlap” but later when i move them around and overlap them again, nothing happens, and since my squares are so close to each other aren’t they already colliding?

please help
[import]uid: 218976 topic_id: 35570 reply_id: 141652[/import]

when you move them around are you calling that:

if hasCollieded(square1, square2) then  

again? You have to check it every time you move an object. This is something you would probably put inside your move code.
[import]uid: 199310 topic_id: 35570 reply_id: 141796[/import]

Hi @vadimerror.

Just a tip: always put your code between the lua tags, it makes easier for us to read.

To do what you want, just check if the space that you want to move the square is empty or not before moving it.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35570 reply_id: 141386[/import]

How would I do that Renato? [import]uid: 218976 topic_id: 35570 reply_id: 141424[/import]

Enter a less than sign, the word code and a greater than sign: <code> and then at the end close the tag with: </code>
[import]uid: 199310 topic_id: 35570 reply_id: 141430[/import]

You can add both squares to a same groupDisplay and you can create a funcion (lets say called “isSpaceEmpty(x,y)” that receives a position (x,y) and goes thru all elements in the groupDisplay to see if anyone of the squares is in that same position (x,y). The function would return true if no square is in that position and would return false if there is a square there.

So, just include in your if statement that checks “to make sure the red square isn’t too far up to transition up/down/left/right” that funcion call, as example:

[lua]if redBox.y < 720 and isSpaceEmpty(e.x, redBox.y + 80) then[/lua]

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35570 reply_id: 141435[/import]

Check out this blog post:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

It has a function that detects if two rectangles are overlapping. [import]uid: 199310 topic_id: 35570 reply_id: 141440[/import]

okay. i kind of understood it but can you show me how to create the function too please because i cant get it all the way for some reason
thanks [import]uid: 218976 topic_id: 35570 reply_id: 141486[/import]

  
-- rectangle based  
local function hasCollided(obj1, obj2)  
 if obj1 == nil then  
 return false  
 end  
 if obj2 == nil then  
 return false  
 end  
 local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin  
 local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax  
 local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin  
 local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax  
 return (left or right) and (up or down)  
end  
&nbsp;  
-- circle based  
local function hasCollidedCircle(obj1, obj2)  
 if obj1 == nil then  
 return false  
 end  
 if obj2 == nil then  
 return false  
 end  
 local sqrt = math.sqrt  
&nbsp;  
 local dx = obj1.x - obj2.x;  
 local dy = obj1.y - obj2.y;  
&nbsp;  
 local distance = sqrt(dx\*dx + dy\*dy);  
 local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2)  
 if distance \< objectSize then  
 return true  
 end  
 return false  
end  
  
square1 = display.newRect(0,0,100,100)  
square2 = display.newRect(50,50,100,100)  
  
if hasCollided(square1, square2) then   
 print("Squares overlap")  
end  
  

Or something like that. [import]uid: 199310 topic_id: 35570 reply_id: 141607[/import]

i tried to put it everywhere but it still would not work right. can you maybe use some of my code and show me an example or explain it in simpler way for me to understand it better.
what i want to do is have all of the squares move but not overlap if there is a square in that space already

thanks [import]uid: 218976 topic_id: 35570 reply_id: 142341[/import]

it works but only on startup it prints “squares overlap” but later when i move them around and overlap them again, nothing happens, and since my squares are so close to each other aren’t they already colliding?

please help
[import]uid: 218976 topic_id: 35570 reply_id: 141652[/import]

Put it either just below this line:

 if e.phase == "moved" then  
 if hasCollided(event.target, redSquare) then   
 print("Squares overlap")  
 end  
 if hasCollided(event.target, whiteSquare) then   
 print("Squares overlap")  
 end  

It looks like you have different handlers for different squares, so you will probably need to repeat this changing out the squares as necessary in your other event handler. [import]uid: 199310 topic_id: 35570 reply_id: 142379[/import]