[Resolved] Help - is there a way to add multiple circle physics bodies to a display object?

I need four circular physics bodies that are symmetrically placed in four corners of, say invisible square space, and they rotate together around a center of the square. Imagine a invisible square box with four corners, and on each corner, I need to place a circular physics body. And I need to rotate these four circular physics bodies around a center of the invisible square.

I initially made this work by doing the following (not so elegant, but still kind of worked):

 local function newBumper()  
 -- circle bumpers  
 local bumperGroup = display.newGroup()  
  
 bumperBody = {friction=0.5, bounce=0.7, radius=6}  
 local circleBumper1 = spriteFactory:newSpriteGroup("circleSpin") -- spriteGroup instance  
 local circleBumper2 = spriteFactory:newSpriteGroup("circleSpin")  
 local circleBumper3 = spriteFactory:newSpriteGroup("circleSpin")  
 local circleBumper4 = spriteFactory:newSpriteGroup("circleSpin")  
  
 physics.addBody(circleBumper1, "static", bumperBody);  
 physics.addBody(circleBumper2, "static", bumperBody);  
 physics.addBody(circleBumper3, "static", bumperBody);  
 physics.addBody(circleBumper4, "static", bumperBody);  
  
 -- added this to make sure the bumper behaves like a bumper  
 circleBumper1.isBullet = true;  
 circleBumper2.isBullet = true;  
 circleBumper3.isBullet = true;  
 circleBumper4.isBullet = true;  
  
 --insert display objects to bumperGroup  
 bumperGroup:insert(circleBumper1);  
 bumperGroup:insert(circleBumper2);  
 bumperGroup:insert(circleBumper3);  
 bumperGroup:insert(circleBumper4);  
  
 circleBumper1:play(); -- animating the spriteGroup  
 circleBumper2:play();  
 circleBumper3:play();  
 circleBumper4:play();  
  
 return bumperGroup;  
 end   
  
 -- spawn a bumper  
 local rotatingBumper = newBumper();  
  
 -- position the bumpers  
 rotatingBumper[1].x = 0.5 \* screenW;  
 rotatingBumper[1].y = 0.5 \* screenH - 38;  
 rotatingBumper[2].x = 0.5 \* screenW + 38;  
 rotatingBumper[2].y = 0.5 \* screenH;   
 rotatingBumper[3].x = 0.5 \* screenW;  
 rotatingBumper[3].y = 0.5 \* screenH + 38;  
 rotatingBumper[4].x = 0.5 \* screenW - 38;  
 rotatingBumper[4].y = 0.5 \* screenH;  
  
 rotatingBumper.xReference = 0.5 \* screenW;  
 rotatingBumper.yReference = 0.5 \* screenH;  
 rotatingBumper.x = 0.5 \* screenW;  
 rotatingBumper.y = 0.5 \* screenH;  
 rotatingBumper.rotation = 0;  
  
 -- rotate bumper  
 local function rotateBumper()  
 rotatingBumper.rotation = rotatingBumper.rotation + 2;  
 end  
 Runtime:addEventListener( "enterFrame", rotateBumper );  
  
 local function stopBumper()  
 Runtime:removeEventListener( "enterFrame", rotateBumper );  
 end  

But it had this oddity described here: http://developer.anscamobile.com/forum/2011/09/12/my-code-doesnt-work-it-should#comment-55577

Meaning, dynamic objects would sometimes go through, or sit on top of rotatingBumper, and sometimes, dynamic objects would be repelled even when they try to go through the gaps found in rotatingBumper.

Based on the helpful insight of shane.lipscomb noted in the above link, I changed my code so that there is only one display object with four physics bodies – but I can’t find the way to attach four circular bodies. I looked and looked, but I couldn’t find any sample code that would help me see the light. So, just as a work-around, I added square bodies, like so:

 --make four animating, circular bumpers with one display object  
  
 local shape1 = {-8,-45, 8,-45, 8,-29, -8,-29}  
 local shape2 = {-8,31, 8,31, 8,47, -8,47}  
 local shape3 = {-47,-8, -31,-8, -31,8, -47,8}  
 local shape4 = {31,-8, 47,-8, 47,8, 31,8}  
  
 local bumperBody1 = {friction=0.5, bounce=0.7, shape=shape1}  
 local bumperBody2 = {friction=0.5, bounce=0.7, shape=shape2}  
 local bumperBody3 = {friction=0.5, bounce=0.7, shape=shape3}  
 local bumperBody4 = {friction=0.5, bounce=0.7, shape=shape4}  
 local rotatingBumper = spriteFactory:newSpriteGroup("spin4circles") -- four animating circles in one spriteSheet  
  
 rotatingBumper:play(); -- animating the spriteGroup  
 physics.addBody(rotatingBumper, "static", bumperBody1, bumperBody2, bumperBody3, bumperBody4);  
  
 rotatingBumper.x = 0.5 \* screenW;  
 rotatingBumper.y = 0.5 \* screenH;  
 rotatingBumper.rotation = 0;  
 -- rotate bumper  
 local function rotateBumper()  
 rotatingBumper.rotation = rotatingBumper.rotation + 2;  
 end  
 Runtime:addEventListener( "enterFrame", rotateBumper );  
  
 local function stopBumper()  
 Runtime:removeEventListener( "enterFrame", rotateBumper );  
 end  
  

It kind of works, but it has it’s own problem (like, dynamic objects not quite behaving in the natural way when they hit the bumper from certain angle – meaning, it behaves fine if it’s hitting a square surface, but it needs to behave as if it’s hitting a circular object).

I could make the octagon shape – but is it the closest to circle it could ever be? And it still wouldn’t behave like objects are colliding with circular object…

If only I can add circle bodies instead! I’d imagine GURUs would know how to do this (if there’s a way to do this), and I’d be so grateful if I could learn how to.

P.S. I saw an example of a car under complex body construction here:
http://developer.anscamobile.com/content/game-edition-physics-bodies#Polygon_bodies
But it doesn’t include circular tires! Does this mean it’s not possible?

[import]uid: 67217 topic_id: 15112 reply_id: 315112[/import]

On my ever growing todo list, is to look into what this physicseditor tool does.

http://www.physicseditor.de/features/

It looks like it makes the job of collision detection for odd shaped objects much easier. Maybe it can help with what you’re trying to do. [import]uid: 67839 topic_id: 15112 reply_id: 55928[/import]

Hi elbowroomapps, thank you so much for the pointer. Among other possible options, I’ll take a look at this editor tool too. Thanks again! [import]uid: 67217 topic_id: 15112 reply_id: 55938[/import]

Actually, I just looked at how Spriteloq generates odd shaped (or complex) physics body here:

http://www.youtube.com/watch?v=bEyNprBtLY8

I use Spriteloq, and this makes it incredibly easy to work out complex physics body. So I probably don’t need another physics editor tool.

That said, I still need to sort out how I might plot out complex physics bodies that must be added to a single display object (that is, multiple of physics bodies that are not touching each other…)
[import]uid: 67217 topic_id: 15112 reply_id: 55957[/import]

So… I could use physics joints for multiple of physics bodies – this will pretty much cover everything I would ever need for creating and adding physics body shapes, it seems:

http://developer.anscamobile.com/content/game-edition-physics-joints [import]uid: 67217 topic_id: 15112 reply_id: 55962[/import]

P.S. I was also able to do the exact same thing using Spriteloq’s Shape Control and a bit of hack (i.e., adding multiple of {} from polys in lua file that Spriteloq spits out). It’s pretty awesome. I can add any shape to any instance of SpriteGroup using this tool! [import]uid: 67217 topic_id: 15112 reply_id: 55973[/import]