Object wont stay still

Hi.

I have physics ball body. I apply force to it every frame when game is running and I want it to stay still when game is paused. But when I set its forces and velocity to 0, it still keeps moving very slowly to the same direction where it was going (like 1 pixel per second).

You can test the behavior with following code:
[lua]local physics = require(“physics”)
local running = false
physics.start()
physics.setGravity( 0, 0)
local playBall= display.newCircle(200, 580,20)
physics.addBody(playBall, {density=4.14, friction=0.3, bounce=0.2, radius=20})

local everyFrame = function(event)
if running then
playBall:applyForce(0,-4.8, playBall.x, playBall.y)
end
end

Runtime:addEventListener(“enterFrame”, everyFrame)

local startStop = display.newRoundedRect(390, 400, 100, 50, 10)
local runEvent = function(event)
if event.phase == “ended” then
if not running then
running = true
else
running = false
playBall:setLinearVelocity(0,0)
playBall.angularVelocity = 0
playBall.collisionBit = 0
playBall.x = 200; playBall.y = 580
end
end
end
startStop:addEventListener(“touch”, runEvent)[/lua]
[import]uid: 56131 topic_id: 14905 reply_id: 314905[/import]

Hey there,

I’m not certain why this is happening, will try to take a closer look this evening.

Until then, this code (yours with two lines added) will do what you want, I believe :slight_smile:

[lua]local physics = require(“physics”)
local running = false
physics.start()
physics.setGravity( 0, 0)
local playBall= display.newCircle(200, 100,20)
physics.addBody(playBall, {density=4.14, friction=0.3, bounce=0.2, radius=20})

local everyFrame = function(event)
if running then
playBall:applyForce(0,-4.8, playBall.x, playBall.y)
end
end

Runtime:addEventListener(“enterFrame”, everyFrame)

local startStop = display.newRoundedRect(100, 100, 100, 50, 10)
local runEvent = function(event)
if event.phase == “ended” then
if not running then
running = true
playBall.bodyType = “dynamic”
else
running = false
playBall:setLinearVelocity(0,0)
playBall.angularVelocity = 0
playBall.bodyType = “static”
playBall.collisionBit = 0
playBall.x = 200; playBall.y = 100
end
end
end
startStop:addEventListener(“touch”, runEvent)[/lua] [import]uid: 52491 topic_id: 14905 reply_id: 55039[/import]

if you are pausing the game, will just pausing the physics not work for you?

http://developer.anscamobile.com/reference/index/physicspause [import]uid: 31262 topic_id: 14905 reply_id: 55081[/import]

Not in my situation, because I need physics in paused mode too. Maybe it would be more correct to say that I want to reset my level. Changing the body type works, thanks for that, but it would be still great to know why my original code was wrong. [import]uid: 56131 topic_id: 14905 reply_id: 55087[/import]

Peach pellen, have you discovered anything yet? [import]uid: 56131 topic_id: 14905 reply_id: 56124[/import]

Merciless bumb, any chance to get help? [import]uid: 56131 topic_id: 14905 reply_id: 60635[/import]

i was pausing my objects with a toggle button with physics.pause and physics.start.
and noticed it still had some momentumn. so i tried appying linearimpulse to opposite direction
but it did not work quite right.

so i settled for setting the object as “STATIC” and it stopped.

[import]uid: 11094 topic_id: 14905 reply_id: 62803[/import]