Physics pause() call displacing objects

here is the code that replicates problem

http://pastebin.com/LwaDd8uu

in this code, 3 crates are stacked.
initially you see each crate is stacked correctly. once crates are removed, physics is paused and added new crates after starting physics crates are not stacked properly. they get displaced.

sequence is
physics start -> stack crates -> remove crates -> pause physics-> start physics -> stack crates (just move x-position by 10 pixels compared to previous crates position) -> crates get displaced

if I do the same operation and use stop physics instead of pause physics crates stacked properly.

problem is physics.stop() function causing game to crash often, game got low ratings because of this crash.

local physics = require("physics")  
physics.setDrawMode("hybrid")  
physics.start(true)  
physics.setGravity(0,9.8)  
  
display.setStatusBar( display.HiddenStatusBar )  
  
local bkg = display.newImage( "bkg\_cor.png" )  
  
local grass = display.newImage("grass.png")  
grass.x = 160; grass.y = 430  
  
local grass2 = display.newImage("grass2.png") -- non-physical decorative overlay  
grass2.x = 160; grass2.y = 440  
  
physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )  
local crates = {}  
local stopit = false --make it true to stop instead of pause, you wont see problem  
local displace = 0  
  
--local dropCrates = timer.performWithDelay( 500, newCrate, 100 )  
function startagain()  
 physics.start(true)  
 if stopit then  
 local grass = display.newImage("grass.png")  
 grass.x = 160; grass.y = 430  
 physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )  
 end  
 if displace == 0 then  
 displace = 10 --change this to \> 30(since crate dimension is 30), won;t see the problem  
 else  
 displace = 0  
 end  
  
 SetCretes()  
end  
  
function CreatePhysicsProblem()  
 for i=1,#crates do  
 crates[i]:removeSelf()  
 crates[i] = nil  
 end  
 crates = nil  
 crates= {}  
 if stopit then  
 physics.stop()  
 else  
 physics.pause()  
 end  
 timer.performWithDelay( 1000, startagain )  
  
end  
  
function SetCretes()  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 405--335  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 368  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 330--402  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 timer.performWithDelay( 2000, CreatePhysicsProblem)  
end  
  
SetCretes()  
  

I placed code in pastebin (http://pastebin.com/LwaDd8uu)
edit: above code is modified taking ManyCrates example as base
[import]uid: 97420 topic_id: 21705 reply_id: 321705[/import]

I’m also having this problem, and can confirm that this is an urgent bug that needs to be fixed. Can this be moved up in the priority queue? [import]uid: 94684 topic_id: 21705 reply_id: 86203[/import]

Here is a possible work around. It makes use of the physics.setTimeStep() function. When you pause the physics engine it seems like it doesn’t remove objects unless another time-step is executed. here is the code

-----------------------------------------------------------------------------------------  
--  
-- main.lua  
--  
-----------------------------------------------------------------------------------------  
  
-- Your code here  
local physics = require("physics")  
physics.setDrawMode("hybrid")  
physics.start()  
physics.setGravity(0,9.8)  
  
display.setStatusBar( display.HiddenStatusBar )  
  
local bkg = display.newImage( "bkg\_cor.png" )  
  
local grass = display.newImage("grass.png")  
grass.x = 160; grass.y = 450  
  
local grass2 = display.newImage("grass2.png") -- non-physical decorative overlay  
grass2.x = 160; grass2.y = 440  
  
physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )  
local crates = {}  
local stopit = false --make it true to stop instead of pause, you wont see problem  
local displace = 0  
  
--local dropCrates = timer.performWithDelay( 500, newCrate, 100 )  
function startagain()  
 physics.setTimeStep(0)  
 --physics.start()  
 if stopit then  
 local grass = display.newImage("grass.png")  
 grass.x = 160; grass.y = 450  
 physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )  
 end  
 if displace == 0 then  
 displace = 0 --change this to \> 30(since crate dimension is 30), won;t see the problem  
 physics.setTimeStep(0)  
 else  
 displace = 0  
 end  
  
 SetCretes()  
end  
  
function CreatePhysicsProblem()  
 for i=1,#crates do  
 crates[i]:removeSelf()  
 crates[i] = nil  
 end  
 crates = nil  
 crates= {}  
 if stopit then  
 physics.stop()  
 else  
 --physics.pause()  
 physics.setTimeStep(0.0000000000000001) --this is like pause  
 end  
 timer.performWithDelay( 1000, startagain )  
  
end  
  
function SetCretes()  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 415--335  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 384  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 local j = display.newImageRect("crate.png",30,30);  
 crates[#crates+1] = j  
 j.x = 60+displace  
 j.y = 353--402  
 physics.addBody( j, { density=3, friction=.500, bounce=0} )  
  
 timer.performWithDelay( 2000, CreatePhysicsProblem)  
end  
  
SetCretes()  

Hope that fixes something,
M.Y. Developers
[import]uid: 55057 topic_id: 21705 reply_id: 86247[/import]

Thanks M.Y. for suggestion. I’ll surely try this solution. [import]uid: 97420 topic_id: 21705 reply_id: 86251[/import]

We have received this bug report and offered a workaround until this is resolved.

On line 37 in original post change;

setCretes()

to;

timer.performWithDelay(1, setCretes, 1)

Peach :slight_smile: [import]uid: 52491 topic_id: 21705 reply_id: 86280[/import]

@M.Y. and Peach,
thanks for your suggestions.
found a work around, I just delayed pause by 500ms after cleaning all the objects from physics. this worked fine.

I hope Ansca resolves this bug soon [import]uid: 97420 topic_id: 21705 reply_id: 86299[/import]