How do i make a multi physics body collision?

So i have a box and on one side there is a red and on the other there is blue… and when the object touches the blue side it turns blue and when it touches the red side it turns red…

I cant seem to figure out how to set up a collision function to let me do that…

Thanks!

You could add the box to its own display group. Then create invisible rectangles over the red and blue sections, making each a physics sensor, and add these to the group. You can then test for collisions on each half independently.

Probably better ways to do this but this is just off the top of my head.

https://docs.coronalabs.com/api/type/Body/isSensor.html

But I have to do the multi body… I can’t do it another way…

The example for a multi-element physics object in the docs is this:

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 } )

So when you call physics.addBody, you will need three tables, one for each element. Each table must define the shape, which is a table consisting of the x, y coordinates of the vertices of the element, { x1, y1, x2, y2, x3, y3, x4, y4}.

Sorry but i already know that… I need a COLLISION function … something like

local function onCollision(event) if event.phase == "began" then if event.myName == "ball" and shape == "red" then print("ball collided with red!") end end end

I cant seem to make anything like that work…

Thanks!

local rectangle ={} --the unique shape with red, invisible and blue     local rectangles=display.newGroup()     rectangles.number1 = display.newRect(rectangles,0,0,10)--for example the blue rectangle     rectangles.number1:setFillColor(0,0,1) rectangles.number1.shape="red"     rectangles.number2 = display.newRect(rectangles,0,0,10) --etc     rectangles.number3 = display.newRect(rectangles,0,0,10) --etc       rectangles.x = 100    --the position of the rectangle with the tree rectangles     rectangles.y = 200     rectangle = rectangles

local function onCollision(event) if event.phase == "began" then if event.myName == "ball" and event.shape == "blue" then print("ball collided with blue!") end end end

not tested but i think it’s something like that

Sorry for the misunderstanding.

Have you seen this: ?

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#multi-element-collisions

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!!

You could add the box to its own display group. Then create invisible rectangles over the red and blue sections, making each a physics sensor, and add these to the group. You can then test for collisions on each half independently.

Probably better ways to do this but this is just off the top of my head.

https://docs.coronalabs.com/api/type/Body/isSensor.html

But I have to do the multi body… I can’t do it another way…

The example for a multi-element physics object in the docs is this:

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 } )

So when you call physics.addBody, you will need three tables, one for each element. Each table must define the shape, which is a table consisting of the x, y coordinates of the vertices of the element, { x1, y1, x2, y2, x3, y3, x4, y4}.

Sorry but i already know that… I need a COLLISION function … something like

local function onCollision(event) if event.phase == "began" then if event.myName == "ball" and shape == "red" then print("ball collided with red!") end end end

I cant seem to make anything like that work…

Thanks!

local rectangle ={} --the unique shape with red, invisible and blue     local rectangles=display.newGroup()     rectangles.number1 = display.newRect(rectangles,0,0,10)--for example the blue rectangle     rectangles.number1:setFillColor(0,0,1) rectangles.number1.shape="red"     rectangles.number2 = display.newRect(rectangles,0,0,10) --etc     rectangles.number3 = display.newRect(rectangles,0,0,10) --etc       rectangles.x = 100    --the position of the rectangle with the tree rectangles     rectangles.y = 200     rectangle = rectangles

local function onCollision(event) if event.phase == "began" then if event.myName == "ball" and event.shape == "blue" then print("ball collided with blue!") end end end

not tested but i think it’s something like that

Sorry for the misunderstanding.

Have you seen this: ?

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#multi-element-collisions