Basic Collision Help...

Hi everyone!

I am very new to Corona and was wondering if I could get some help with my collision code. I have posted it below, if you have any ideas they would be greatly appreciated! Thanks!

local ceilingCollisionBoolean local function moverCollision(event) if event.object1 == "ceiling" and event.object2 == "mover" then ceilingCollisionBoolean = "true" print("collision occurred") elseif event.object1 == "mover" and event.object2 == "ceiling" then ceilingCollisionBoolean = "true"         print("collision occurred") else ceilingCollisionBoolean = "false" end end Runtime:addEventListener ("collision", moverCollision)

You need to make sure the collision object are referenced by their id/name etc. so it would look something like this assuming you have set up your code as follows:

yourceiling.name = “ceiling”

yourmover.name = “mover”

or to that effect.

Also you dont need to put your booleans in quotes

local ceilingCollisionBoolean local function moverCollision(event) if event.phase == "began" then if event.object1.name == "ceiling" and event.object2.name == "mover" then         ceilingCollisionBoolean = true         print("collision occurred") elseif event.object1.name == "mover" and event.object2.name == "ceiling" then ceilingCollisionBoolean = true         print("collision occurred") else     ceilingCollisionBoolean = false end end end    

@Icy Spark

Thanks for responding! I copied the code and placed it into my program, and then also declared:

mover.name = “mover”

ceiling.name = “ceiling”

Yet I am still not receiving any output in the console when I make the two objects collide. Is there any specific area i should define the mover.name and ceiling.name? Or does it matter that both the mover and ceiling are static bodies? Thanks agin for taking the time to help me!

It shouldnt really matter where you define the names as long as it’s after where you initially declare the variable. I usually always set the names right after I declare the variable.

Yes, it does matter that both are static.

You can only detect static and dynamic or dynamic and kinematic collisions.

if you are using gravity try setting mover.gravityScale = 0 and make it a dynamic body to stop it falling down

Thank you, it is working now! I do have one other question, I have an if statement that controls the movement of the mover and now I have added the ceilingCollisionBoolean and I want the mover object only to be able to move up if that variable equals false, how exactly could I do that? Here’s my code:

if beginX \> endX and ceilingCollisionBoolean == false then transition.to(mover, {time=200, x=(mover.x-80), y=mover.y})

Any ideas why this won’t work? Thanks again

I can be wrong but i think the    “and” is not supported…

i think you have to put a" if - then" inside another “if - then” to simulate the “and”, as i have done many times…

if beginX > endX then

                 

                 if  ceilingCollisionBoolean == false then

                     transition.to(mover, {time=200, x=(mover.x-80), y=mover.y})

                 end

end

To answer that question, “and” is most definitely supported in Lua: http://www.lua.org/pil/3.3.html.

  • Andrew

Any other suggestions? Thanks

Hi there,

You said that your code isn’t working – what’s not working specifically?

To understand why your code isn’t doing what you want it to be doing, we’d need to see more code, such as where beginX, endX, and ceilingCollisionBoolean are defined.

  • Andrew

Hi Andrew, thanks for your response, here is more of my code below:

local beginX local beginY local endX local endY local xDistance local yDistance function checkSwipeDirection() xDistance = math.abs(endX - beginX) yDistance = math.abs(endY - beginY) if xDistance \> yDistance then if beginX \> endX and ceilingCollisionBoolean == false then transition.to(mover, {time=200, x=(mover.x-80), y=mover.y}) else transition.to(mover, {time=200, x=(mover.x+80), y=mover.y}) end else if beginY \> endY then transition.to(mover, {time=200, x=mover.x, y=(mover.y-80)}) else transition.to(mover, {time=200, x=mover.x, y=(mover.y+80)}) end end end function swipe(event) if event.phase == "began" then beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection() end end Runtime:addEventListener("touch", swipe) local ceilingCollisionBoolean local function moverCollision(event) if (event.phase == "began") then if event.object1.name == "ceiling" and event.object2.name == "mover" then ceilingCollisionBoolean = true print("collision occurred") elseif event.object1.name == "mover" and event.object2.name == "ceiling" then ceilingCollisionBoolean = true print("collision occurred") else ceilingCollisionBoolean = false end end end Runtime:addEventListener ("collision", moverCollision)

Please let me know if you have any thoughts thanks!

OK, that helps.  It would also be somewhat helpful if you described a bit more in words exactly the behavior you’re hoping to achieve, and what’s happening instead.

One quick thing I notice is that you in your checkSwipeDirection function, you use the ceilingCollisionBoolean when considering the x direction and movement.  But the x direction is horizontal, whereas your ceiling (I imagine) is on the top and should be limiting the vertical movement, which is the y direction.

  • Andrew

Essentially, I am trying to have an object move on a grid and on a swipe move 80px the way the screen is swiped. However, I am trying to add walls that stop the mover but using physics does not seem to stop the transition.to statement, so I am trying to create a function that detects when the object is colliding with a certain wall and then not allow the object to transition a certain way when the object is colliding with a certain wall. I hope this helps you understand and I changed the ceilingCollisionBoolean to govern the moving up statement. Thanks again!

You need to make sure the collision object are referenced by their id/name etc. so it would look something like this assuming you have set up your code as follows:

yourceiling.name = “ceiling”

yourmover.name = “mover”

or to that effect.

Also you dont need to put your booleans in quotes

local ceilingCollisionBoolean local function moverCollision(event) if event.phase == "began" then if event.object1.name == "ceiling" and event.object2.name == "mover" then         ceilingCollisionBoolean = true         print("collision occurred") elseif event.object1.name == "mover" and event.object2.name == "ceiling" then ceilingCollisionBoolean = true         print("collision occurred") else     ceilingCollisionBoolean = false end end end    

@Icy Spark

Thanks for responding! I copied the code and placed it into my program, and then also declared:

mover.name = “mover”

ceiling.name = “ceiling”

Yet I am still not receiving any output in the console when I make the two objects collide. Is there any specific area i should define the mover.name and ceiling.name? Or does it matter that both the mover and ceiling are static bodies? Thanks agin for taking the time to help me!

It shouldnt really matter where you define the names as long as it’s after where you initially declare the variable. I usually always set the names right after I declare the variable.

Yes, it does matter that both are static.

You can only detect static and dynamic or dynamic and kinematic collisions.

if you are using gravity try setting mover.gravityScale = 0 and make it a dynamic body to stop it falling down

Thank you, it is working now! I do have one other question, I have an if statement that controls the movement of the mover and now I have added the ceilingCollisionBoolean and I want the mover object only to be able to move up if that variable equals false, how exactly could I do that? Here’s my code:

if beginX \> endX and ceilingCollisionBoolean == false then transition.to(mover, {time=200, x=(mover.x-80), y=mover.y})

Any ideas why this won’t work? Thanks again

I can be wrong but i think the    “and” is not supported…

i think you have to put a" if - then" inside another “if - then” to simulate the “and”, as i have done many times…

if beginX > endX then

                 

                 if  ceilingCollisionBoolean == false then

                     transition.to(mover, {time=200, x=(mover.x-80), y=mover.y})

                 end

end

To answer that question, “and” is most definitely supported in Lua: http://www.lua.org/pil/3.3.html.

  • Andrew

Any other suggestions? Thanks

Hi there,

You said that your code isn’t working – what’s not working specifically?

To understand why your code isn’t doing what you want it to be doing, we’d need to see more code, such as where beginX, endX, and ceilingCollisionBoolean are defined.

  • Andrew