Multibody collision

Hi i want to make collision detection with 2 custom body shapes.

There is addBody code:

physics.addBody( body, { density=1.0, friction=1.0, bounce=1.0, shape = leftShape},{ density=1.0, friction=1.0, bounce=1.0, shape = rightShape}) body:addEventListener( "collision", body )

The “leftShape” and “rightShape” are correct. I’ve tested them separetely and it works. Problem is when i want to add 2 bodies. Physics engine don’t accept left and use only right body.

So lets say you have a a circle and a box and when they collide they both fade…

local physics = require("physics") physics.start() local box = display.newImageRect( "box.png", 50, 50 ) box.x = display.contentCenterX box.y = 400 box.myName = "box" -- gives a name to the object physics.addBody( box, "static" ) local circle = display.newImageRect( "ball.png", 40, 40 ) circle.x = display.contentCenterX circle.y = 50 circle.myName = "circle" -- gives a name to the object physics.addBody( circle, "dynamic" ) local function onCollision(event) if event.phase == "began" then if event.target.myName == "box" and event.other.myName == "circle" then box.alpha = 0.3 circle.alpha = 0.3 end end end box:addEventListener( "collision", onCollision )

Heres are the images for box and circle below…

I know how to make collision. THe problem is, that one object can own multiple body shapes(if i understand docs), and my physics engine always accept only last added shape.

So are you trying to do the 

Multi-Element Body?

Yes, but it’s not working for me, and i can’t find out why. ;/

To be honest, i’ve spend long time in C/C++, switched to Corona because of short code to make simple game, but docs are very poor, and i can’t live without pointers ;/

Can you post some code?

local height = math.random(display.contentWidth/5\*4) local body = display.newRect(display.contentWidth/2, baseY\*5,display.contentWidth,baseY\*10) local leftShapeX = 1 local leftShape = { (-body.contentWidth/2),(body.contentHeight/2), (-body.contentWidth/2), (-body.contentHeight/2), (-body.contentWidth/2+height), (-body.contentHeight/2), (-body.contentWidth/2+height),(body.contentHeight/2) } local rightShape = { (height+display.contentWidth/5-body.x),(body.contentHeight/2), (height+display.contentWidth/5-body.x),(-body.contentHeight/2), (body.contentWidth/2),(-body.contentHeight/2), (body.contentWidth/2),(body.contentHeight/2) } local bodyMask = display.newRect(height+display.contentWidth/10,baseY\*5, display.contentWidth/5, baseY\*20) bodyMask:setFillColor(0,0,0) physics.addBody( body, { density=1.0, friction=1.0, bounce=1.0, shape = leftShape}) body:addEventListener( "collision", body )

This is full code that generate new wall with space in the middle.

I don’t see where you include the first portion of the table llstener. Since you haven’t included the actual collision function, perhaps it’s in another portion of your codebase that you haven’t shared. If not, you should mimic the below syntax:

local crate1 = display.newRect(0,0,50,50) physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } ) crate1.myName = "first crate" local function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( self.myName .. ": collision began with " .. event.other.myName ) elseif ( event.phase == "ended" ) then print( self.myName .. ": collision ended with " .. event.other.myName ) end end crate1.collision = onLocalCollision crate1:addEventListener( "collision", crate1 )

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#local-collision-handling

But collisions are working good… The problem is with multi-element body. This is the only part of code that doesn’t work…

What about them isn’t working? Your first post shows what appears to be the correct method for adding multiple physics shapes to your body object. Your second snippet excludes the “rightSide” body. Neither really address what you mean by your original issue, which appears to be the fact that your “leftSide” (as added in the first snippet) isn’t registering physics.

Have you enabled the “hybrid” physics debug mode so that you can see if the bodies are being created? Do you have print statements in your collision listener to display the name/id/number of the body which is being actioned? Have you attempted to accomplish this functionality with justthe code from the docs, to confirm that this actually works and isn’t a bug in your Corona build, or a miss in your code? These will help in paring down the actual issue to get a resolution.

The problem is that i need graphic object with multi-element body. Because my element is line with pause. So i need left and right body elements. When i add 2 body arrays as paramterers to physics.addBody, only last shape array works. Previous(left) dont.

 You should check your shapes. Maybe their verticles have something wrong.

 My code work fine:

local crate1 = display.newImage( "crate.png", 180, -50 ) crate1.myName = "first crate" local h = crate1.contentHeight local w = crate1.contentWidth physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3, shape = {-0.5\*w, -0.5\*h, 0.5\*w, -0.5\*h, 0.5\*w, -0.1\*h,-0.5\*w,-0.1\*h} }, { density=3.0, friction=0.5, bounce=0.3, shape = {-0.5\*w, 0.1\*h, 0.5\*w, 0.1\*h, 0.5\*w, 0.5\*h,-0.5\*w,0.5\*h} } )

 Guess you have problems with your shape coordinates.

If a shape table is specified, the body boundaries will follow the polygon provided by the shape. Note that the maximum number of sides per shape is eight (8), and all angles must be convex. Specifying a shape table will override a radius property, if both are specified in the same element. Note that the shape coordinates must be defined in clockwise order, and the first and last vertex must be different.

 Check the  clockwise order

I have them clockwise, and both are working good when i use only one of them. 

physics.addBody( body, { density=1.0, friction=1.0, bounce=1.0, shape = leftShape}) ... OR ... physics.addBody( body, { density=1.0, friction=1.0, bounce=1.0, shape = rightShape})

works fine, and debuger shows me good sizes of my shapes.

Problem is only with using both of them. Physics use only last defined shape

Since you said that your shapes are clockwise, here’s what I recommend you check:

  • Shapes have fewer than 8 points

  • Shapes have more then 2 points

  • Shapes are concave

I know you said they work when you have only one of them, but I once had a bug in my game where a concave body worked about 80% of the time (objects would stick to it and have strange collisions the other 20%), and it was only when I added another body that it failed reliably.

Also, as Alex said, you should try using the code from the docs (that is, use a shape guaranteed to work) to see if it’s a bug with your version of Corona or something like that.

  • Caleb

Code is ok. I don’t know why, but now it works… I’ve made some undo/redo, then played Starcraft to relax and it’s working now…

Thanks guys for help :wink:

So lets say you have a a circle and a box and when they collide they both fade…

local physics = require("physics") physics.start() local box = display.newImageRect( "box.png", 50, 50 ) box.x = display.contentCenterX box.y = 400 box.myName = "box" -- gives a name to the object physics.addBody( box, "static" ) local circle = display.newImageRect( "ball.png", 40, 40 ) circle.x = display.contentCenterX circle.y = 50 circle.myName = "circle" -- gives a name to the object physics.addBody( circle, "dynamic" ) local function onCollision(event) if event.phase == "began" then if event.target.myName == "box" and event.other.myName == "circle" then box.alpha = 0.3 circle.alpha = 0.3 end end end box:addEventListener( "collision", onCollision )

Heres are the images for box and circle below…

I know how to make collision. THe problem is, that one object can own multiple body shapes(if i understand docs), and my physics engine always accept only last added shape.

So are you trying to do the 

Multi-Element Body?

Yes, but it’s not working for me, and i can’t find out why. ;/

To be honest, i’ve spend long time in C/C++, switched to Corona because of short code to make simple game, but docs are very poor, and i can’t live without pointers ;/