Collision Detection

I am having trouble differentiating between collisions. For example, if A hits B, then you get points, but if A hits C, then you lose points. Can anyone help? Thanks!

What seems to be ther problem?

A, B, and C are all physics objects?

http://docs.coronalabs.com/api/event/collision/index.html

http://docs.coronalabs.com/api/event/collision/object1.html

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " & " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName ) end if ( event.phase == "ended" ) then if ( (event.object1.myName == "A" and event.object2.myName == "B") or (event.object1.myName == "B" and event.object2.myName == "A")) then myPoints = myPoints +1; elseif (event.object1.myName == "A" and event.object2.myName == "C") or (event.object1.myName == "C" and event.object2.myName == "A") then KillPlayer=true; CallKillPlayerEndGame(); end end

As long as you assigned them some special field name like name or “ID” you can get at each one in the collision event

Sorry! I’m kind of new at this stuff. How do you assign special field names?

When you create an object in LUA or a table you can assign properties to it dynamically at runtime, it works exactly like a JavaScript Dynamic Object if you know about javascript.

LUA example:

myShoppingCart = {}  – Create a Table

myShoppingCard.ItemName = “Laptop”

myShoppingCard.ItemPrice = 25.00

so in your original example you have two objects lets say Ship and Enemy

I think you will most likely add a field or property to each  object to help you figure out what object is being hit.

Ship.Name = “ship”

Enemy.Name = “enemy”

You assign the names so that inside of the onCollision event if makes it easier to figure out who is hitting what.

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.Name .. " & " .. event.object2.Name ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.Name .. " & " .. event.object2yName ) end if ( event.phase == "ended" ) then if ( (event.object1.Name == "ship" and event.object2.Name == "enemy") then myPoints = myPoints +1; CallKillPlayerEndGame(event.object2); elseif (event.object1.Name == "enemy" and event.object2.Name == "ship")) then myPoints = myPoints +1; CallKillPlayerEndGame(event.object1); elseif (event.object1.Name == "ship" and event.object2.Name == "goal") or (event.object1.Name == "goal" and event.object2.Name == "ship") then end end function CallKillPlayerEndGame(enemytoKill) --Used a timer to execute to kill the enemy - because you can not remove an object during the --Collision so we execute a small delay to allow the collision to complete. timer.performWithDelay( 1, function() enemytoKill:removeSelf() end ) end 

Hope this helps,

Larry

Thank you so much for your patience with me. Say in this example, if an enemy hits a ship,  I want to make the enemy disappear. I tried doing a enemy:removeSelf(), but it removed the wrong enemy on the screen. Help?

Look at the code above, I modified it so as to make sure I am removing the correct ship based on which one is the enemy during the collision.

note: event.object1 and event.object2 above.

Good luck Larry

What seems to be ther problem?

A, B, and C are all physics objects?

http://docs.coronalabs.com/api/event/collision/index.html

http://docs.coronalabs.com/api/event/collision/object1.html

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " & " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName ) end if ( event.phase == "ended" ) then if ( (event.object1.myName == "A" and event.object2.myName == "B") or (event.object1.myName == "B" and event.object2.myName == "A")) then myPoints = myPoints +1; elseif (event.object1.myName == "A" and event.object2.myName == "C") or (event.object1.myName == "C" and event.object2.myName == "A") then KillPlayer=true; CallKillPlayerEndGame(); end end

As long as you assigned them some special field name like name or “ID” you can get at each one in the collision event

Sorry! I’m kind of new at this stuff. How do you assign special field names?

When you create an object in LUA or a table you can assign properties to it dynamically at runtime, it works exactly like a JavaScript Dynamic Object if you know about javascript.

LUA example:

myShoppingCart = {}  – Create a Table

myShoppingCard.ItemName = “Laptop”

myShoppingCard.ItemPrice = 25.00

so in your original example you have two objects lets say Ship and Enemy

I think you will most likely add a field or property to each  object to help you figure out what object is being hit.

Ship.Name = “ship”

Enemy.Name = “enemy”

You assign the names so that inside of the onCollision event if makes it easier to figure out who is hitting what.

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.Name .. " & " .. event.object2.Name ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.Name .. " & " .. event.object2yName ) end if ( event.phase == "ended" ) then if ( (event.object1.Name == "ship" and event.object2.Name == "enemy") then myPoints = myPoints +1; CallKillPlayerEndGame(event.object2); elseif (event.object1.Name == "enemy" and event.object2.Name == "ship")) then myPoints = myPoints +1; CallKillPlayerEndGame(event.object1); elseif (event.object1.Name == "ship" and event.object2.Name == "goal") or (event.object1.Name == "goal" and event.object2.Name == "ship") then end end function CallKillPlayerEndGame(enemytoKill) --Used a timer to execute to kill the enemy - because you can not remove an object during the --Collision so we execute a small delay to allow the collision to complete. timer.performWithDelay( 1, function() enemytoKill:removeSelf() end ) end 

Hope this helps,

Larry

Thank you so much for your patience with me. Say in this example, if an enemy hits a ship,  I want to make the enemy disappear. I tried doing a enemy:removeSelf(), but it removed the wrong enemy on the screen. Help?

Look at the code above, I modified it so as to make sure I am removing the correct ship based on which one is the enemy during the collision.

note: event.object1 and event.object2 above.

Good luck Larry