Hi,
I’m seeing some strange behaviour when trying to change linear velocity after ball collision.
I’m using a zero gravity and a ball moving at contant speed. Speed is aplied using a linear impulse.
Here is my physics setup:
[lua]
physics:start()
physics.setReportCollisionsInContentCoordinates( true )
physics.setGravity(0, 0)
[/lua]
And my ball display object with dynamic body:
[lua]
local bo = display.newImageRect(“images/rubkuglica.png”, ball.size * 2, ball.size * 2)
func:scaleImage(bo)
bo.x = _W / 2
bo.y = _H / 2
stage:insert(bo)
local diameter = ball.size * 2
if (diameter > bo.width) then
diameter = bo.width
end
if (diameter > bo.height) then
diameter = bo.height
end
physics.addBody( bo, “dynamic”, { density=0, friction=0, bounce=1, radius=diameter/2 } )
bo:setFillColor( color[1], color[2], color[3] )
bo.colorIndex = colorIndex
bo.isBall = true
bo.collision = collisionFunction
bo.linearDamping = 0
bo.angularDamping = 0
bo.angularVelocity = 0
bo.gravityScale = 0
bo.isFixedRotation = true
bo:addEventListener( “collision”, bo )
[/lua]
Here is how I move the ball at constant speed:
[lua]
ball.object:applyLinearImpulse( ball.startX * ball.object.mass, ball.startY * ball.object.mass, ball.object.x, ball.object.y )
[/lua]
The strange thing is when I try to stop the ball by setting linear velocity to zero, it does not stop, it coninues to move with a slower speed.
[lua]
timer.performWithDelay(20, function()
local vx, vy = collision.ball.object:getLinearVelocity()
collision.ball.object:setLinearVelocity(0, 0)
print(“Change velocity!!!”)
end)
[/lua]
The even stranger thing happens when I set linear velocity to current velocity * -3. Then the velocity changes to the opposite value but the change of direction is very lagy and ugly.
[lua]
timer.performWithDelay(20, function()
local vx, vy = collision.ball.object:getLinearVelocity()
collision.ball.object:setLinearVelocity(vx * -3, vy * -3)
print(“Change velocity!!!”)
end)
[/lua]
Am I missing something or is Box2D that bad for zero gravity games? Tnx for help.
Daniel