Collision with Corona SDK

  1. Is it possible to have the collision shape consist of 10 different sized squares? And then putting them next to each other to create a shape?

  2. Is it possible to have a stretched circle shape as collision shape? Like an oval shape?

Thanks [import]uid: 14018 topic_id: 5081 reply_id: 305081[/import]

Answers:

  1. Yes. You make create one body out of several convex shapes (squares, triangles, hexagons, trapezoids, etc.) They can be any shape as long as they are convex. To detect which part is being collided with, check event.selfElement or event.otherElement in the collision event handler (if you’re using a table listener). The number in each of these is which ‘shape’ on the body is being collided with, and the number matches the order in which they were added.
    Another option is to weld several bodies together but you only want to do that for parts that can move or spin or need to attach/detach, or need to be in different states from each other (isSensor, bodyType, etc.) Yet another options is Jon Beebe’s ‘proxy’ library - you can use it to listen for position changes in one body and update the position of another body whenever the first one moves.

For the first option, to add multiple shapes to a body, just go like this:

local bumperLeft = {0,4, -22,-10, 0,-10}  
local bumperRight= {0,4, 22,-10, 0,-10}  
physics.addBody(peonSprite, 'dynamic',  
 { density=1.4, friction=0.3, bounce=0.4, radius = 15,},  
 { density=0.1, friction=0.3, bounce=0.4, radius=5, shape = bumperLeft},  
 { density=0.1, friction=0.3, bounce=0.4, radius=5, shape = bumperRight}  
)  

In this example selfElement or otherElement will be 1 for the center circle, 2 for the left bumper, and 3 for the right bumper shape. All on one body. AFAIK, This one body fires a ‘began’-phase and ‘ended’-phase collision event once each for any of the shapes as if they were all one, until the next collision on that shape, but then you can check selfElement or otherElement within the collision handler if you want to know which part of this selfsame body got collided with.
2. Stretched circles - not in Corona. But you can have a many-sided polygon that looks like slightly angular oval. I imagine you want this for the way it interacts with curved surfaces or something. A close approximation from a many-sided polygon should work just fine. It would need a few more points then say ,bumperLeft in the above example, to be smooth and round though.

figuring out polygon coords for your collision shapes can be tedious. It’s worth investing $20 into PhysicsEditor by Andreas Loew in my opinion. It saved me a lot of time anyway. I think it’s only available on MacOS but he might have made a Windows version since then.
[import]uid: 63787 topic_id: 5081 reply_id: 139133[/import]

Answers:

  1. Yes. You make create one body out of several convex shapes (squares, triangles, hexagons, trapezoids, etc.) They can be any shape as long as they are convex. To detect which part is being collided with, check event.selfElement or event.otherElement in the collision event handler (if you’re using a table listener). The number in each of these is which ‘shape’ on the body is being collided with, and the number matches the order in which they were added.
    Another option is to weld several bodies together but you only want to do that for parts that can move or spin or need to attach/detach, or need to be in different states from each other (isSensor, bodyType, etc.) Yet another options is Jon Beebe’s ‘proxy’ library - you can use it to listen for position changes in one body and update the position of another body whenever the first one moves.

For the first option, to add multiple shapes to a body, just go like this:

local bumperLeft = {0,4, -22,-10, 0,-10}  
local bumperRight= {0,4, 22,-10, 0,-10}  
physics.addBody(peonSprite, 'dynamic',  
 { density=1.4, friction=0.3, bounce=0.4, radius = 15,},  
 { density=0.1, friction=0.3, bounce=0.4, radius=5, shape = bumperLeft},  
 { density=0.1, friction=0.3, bounce=0.4, radius=5, shape = bumperRight}  
)  

In this example selfElement or otherElement will be 1 for the center circle, 2 for the left bumper, and 3 for the right bumper shape. All on one body. AFAIK, This one body fires a ‘began’-phase and ‘ended’-phase collision event once each for any of the shapes as if they were all one, until the next collision on that shape, but then you can check selfElement or otherElement within the collision handler if you want to know which part of this selfsame body got collided with.
2. Stretched circles - not in Corona. But you can have a many-sided polygon that looks like slightly angular oval. I imagine you want this for the way it interacts with curved surfaces or something. A close approximation from a many-sided polygon should work just fine. It would need a few more points then say ,bumperLeft in the above example, to be smooth and round though.

figuring out polygon coords for your collision shapes can be tedious. It’s worth investing $20 into PhysicsEditor by Andreas Loew in my opinion. It saved me a lot of time anyway. I think it’s only available on MacOS but he might have made a Windows version since then.
[import]uid: 63787 topic_id: 5081 reply_id: 139133[/import]