Hello all,
I was wondering if someone could help me with regards to kinematic physics bodies that are placed in a display group that is animated via transitions.
Basically, I have several items (balls) that need to respond to collision events; those balls are housed within a display group. The display group is then animated with a transition.
If I animate the balls independently everything works fine. However, if I put the balls into a display group and animate the group, the collisions no longer fire.
My assumption is that the physics engine is not aware that the balls x/y position is changing when the ball’s parent group is animating. But I could be way wrong.
The following code is not taken directly from my project, but I put it together to show proof of concept (so it is by no means optimized). You can see that “Dynamic” and “Independent” collisions work fine, but once the items are grouped and then animated, they fail.
[lua]physics = require(“physics”);
widget = require(“widget”);
physics.start();physics.pause();
–Draw our main block.
local hitBlock = display.newRect(0,0,100,100);
hitBlock.x = display.contentCenterX;
hitBlock.y = display.contentCenterY;
hitBlock:setFillColor(0,255,0,255);
hitBlock.myName = “hitBlock”;
–Draw ground
local ground = display.newRect(0,0,display.contentWidth, 10);
ground.x = display.contentCenterX;
ground.y = hitBlock.y + hitBlock.height/2;
ground:setFillColor(255,0,0,255) ;
ground.myName = “ground”;
local debugText = display.newText(“DEBUG”,0, 0, display.contentWidth, display.contentHeight, native.systemFont,10)
–Add to the physics engine
physics.addBody(hitBlock, “dynamic”, {density=1, bounce=.5, friction=.5});
physics.addBody(ground, “static”, {density=1, bounce=.5, friction=.5});
–Ball functions
local function dynamicBalls()
debugText.text = “STARTING: DYNAMIC BALLS\n”…debugText.text;
local ballContainer = display.newGroup();
for i=1, 20 do
local ball = display.newCircle(0, 0, 5)
ball.x = display.contentCenterX + math.random(-50,50);
ball.y = 10;
ball.myName = “dynamic_ball” … i; --Give it a name
–Fill with random colors
ball:setFillColor(math.random(0,255),math.random(0,255),math.random(0,255),255)
–Ball collision event
function ball:collision(event)
if(event.other.myName == “ground”) then
debugText.text = "Removing ~~> " … self.myName … “\n” …debugText.text ;
self:removeSelf();
self = nil;
end
end
ball:addEventListener(“collision”, ball);
physics.addBody(ball, “dynamic”, {density=1, bounce=.3, friction=0});
ballContainer:insert(ball);
end
end
local function independentBalls()
debugText.text = “STARTING: INDEPENDENT ANIMATION\n”…debugText.text;
local ballContainer = display.newGroup();
for i=1, 20 do
local ball = display.newCircle(0, 0, 5)
ball.x = display.contentCenterX + math.random(-50,50);
ball.y = 10;
ball.myName = “indie_ball” … i;
–Ball collision event
function ball:collision(event)
if(event.other.bodyType == “dynamic”) then
debugText.text = "Removing~~> " … self.myName … “\n” … debugText.text;
self:removeSelf();
self = nil;
end
end
ball:addEventListener(“collision”, ball);
physics.addBody(ball, “kinematic”, {density=1, bounce=.3, friction=0, isSensor=true});
ballContainer:insert(ball);
–Move the individual (via transition)
transition.to(ball, {time=3000,y = display.contentHeight});
end
end
local function groupedBalls()
debugText.text = “STARTING: GROUP ANIMATION\n” …debugText.text;
local ballContainer = display.newGroup();
for i=1, 20 do
local ball = display.newCircle(0, 0, 5)
ball.x = display.contentCenterX + math.random(-50,50);
ball.y = 10;
ball.myName = “grouped_ball” … i;
–Ball collision event
function ball:collision(event)
if(event.other.bodyType == “dynamic”) then
debugText.text = "Removing~~> " … self.myName… “\n” … debugText.text;
self:removeSelf();
self = nil;
end
end
ball:addEventListener(“collision”, ball);
physics.addBody(ball, “kinematic”, {density=1, bounce=.3, friction=0, isSensor=true});
ballContainer:insert(ball);
end
–Move the container (via transition) holding our elements
transition.to(ballContainer, {time=3000,y = display.contentHeight});
end
local btnDynamic = widget.newButton({
width = 100,
height = 50,
label = “Dynamic”,
onRelease = dynamicBalls,
top = display.contentHeight - 50,
left = 0;
})
local btnIndie = widget.newButton({
width = 100,
height = 50,
label = “Independent”,
onRelease = independentBalls,
top = display.contentHeight - 50,
left = 100;
})
local btnGroup = widget.newButton({
width = 100,
height = 50,
label = “Grouped”,
onRelease = groupedBalls,
top = display.contentHeight - 50,
left = 200
})
physics.start();[/lua]
[import]uid: 152300 topic_id: 32871 reply_id: 332871[/import]