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]