How do i make a multi physics body collision?

Hello @hasty, So i tried this and still doesn’t work…

local function onCollision(event) if event.phase == "began" then if event.myName == "circle" and event.selfElement == "red" then display.remove(circle) print("hello") elseif event.selfElement == "red" and event.myName == "circle" then display.remove(circle) print("hello2") end end end Runtime:addEventListener( "collision", onCollision )

@Sonic,

Better re-read those docs (https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#multi-element-collisions).  

event.selfElement event.otherElement

Contain numbers, where ‘otherElement’ for hasty’s example would be 1,2,3, or 4 in order of the sub-bodies.

physics.addBody( nebula, "dynamic", { friction=0.2, bounce=0.4, shape=podT }, --1 { friction=0.8, bounce=0.0, shape=podR }, --2 { friction=0.8, bounce=0.0, shape=podB }, --3 { friction=0.2, bounce=0.4, shape=podL } --4 )

Oh, and don’t use a global listener.  Use a local listener.  

If you’re totally flumoxed, post your object building code with the code that adds the bodies to it.

Never mind, here is an example using hasty’s code as a base:

local nebula = display.newImage( "nebula.png" ) nebula.x, nebula.y = display.contentCenterX, display.contentCenterY local podT = {1,-89, 14,-83, 20,-70, 14,-57, 1,-51, -12,-57, -18,-70, -12,-83} local podR = {69,-20, 82,-14, 88,-1, 82,12, 69,18, 56,12, 50,-1, 56,-14} local podB = {1,49, 14,55, 20,68, 14,81, 1,87, -12,81, -18,68, -12,55} local podL = {-70,-20, -57,-14, -51,-1, -57,12, -70,18, -83,12, -89,-1, -83,-14} physics.addBody( nebula, "dynamic", { friction=0.2, bounce=0.4, shape=podT }, { friction=0.8, bounce=0.0, shape=podR }, { friction=0.8, bounce=0.0, shape=podB }, { friction=0.2, bounce=0.4, shape=podL } ) local circ = display.newCircle( 10, 10, 10 ) physics.addBody( circ ) circ.isSensor = true function circ.collision( self, event ) if( event.otherElement == 1 ) then print( "Hit podT" ) elseif( event.otherElement == 2 ) then print( "Hit podR" ) elseif( event.otherElement == 3 ) then print( "Hit podB" ) elseif( event.otherElement == 4 ) then print( "Hit podL" ) end return false end circ:addEventListener( "collision" )

Thanks @RoamingGamer… I’ll try it when I get home.

@RoamingGamer, Thanks! I modified the code to my liking and it works perfect!  :D  :smiley:

You’re welcome, Hasty should get most of the thanks.  I only added a little to a great answer.

Cheers,

ed

Well yes thanks hasty … But my question was trying to get help with the on collision function… Not adding multibody stuff… But thanks to all!!