phantom physics

Why does obj2 bounce (turning a small square) as if obj1 still exists?

display.setStatusBar( display.HiddenStatusBar ) display.setDefault( "background", 1,1,1 ) require("physics") local widget = require( "widget" ) physics.start() physics.setGravity(0,10) local shape1={-150,-180, 150,-180, 150,180, -150,180} local shape2={-220,-10, 220,-10, 220,10, -220,10} local shape3={-10,-10, 10,-10, 10,10, -10,10} --small rectangle local obj1=display.newPolygon( 375,300 , shape1 ) -- bouncing effect --local obj1=display.newPolygon( 455,165 , shape2 ) -- no bouncing effect local obj2=display.newPolygon( 355,155 , shape2 ) local obj3=display.newPolygon( 134,129 , shape3 ) obj1.fill = {0,1,0,.5} obj2.fill = {0,1,0,.5} obj3.fill = {0,1,0,.5} local function cut( event ) if ( "ended" == event.phase ) then physics.addBody( obj1) obj1:removeSelf() -- delete from memory obj1 physics.addBody( obj2 ) -- the rectangle behaves as if obj1 still exists physics.addBody( obj3 ) -- the square rotates from contact with the obj2 end end local button1 = widget.newButton( { fontSize=29, left = 10, top = 10, id = "button1", label = "start", onEvent = cut } )

The “bouncing effect” does occur in both cases, it’s just more subtle with the more narrow obj1. You can see it better if you use physics.setDrawMode( “hybrid” ).

Corona is frame-based and my guess is that since you create and remove those objects within a few lines of each other, the physics engine doesn’t have the time to adjust to your changes. It could be that the engine cannot create and remove an object during the same cycle, but instead it does the removal at the very next possible cycle, so all three objects exist together for that one cycle before obj1 is removed.

Also, when requiring physics, you should use:
 

local physics = require("physics")

Xedur@Spyric’s guess is correct - the “cleanup” of object “destruction” is deferred until end-of-frame, so “obj1” kinda-sorta still exists at the moment that you add obj2 and obj3 (and physics lib doing overlap fixup on insertion is one of those rare things that does happen immediately).  So your sequence is something like this:

1.  lua interpreter cycle begins

2.  queued touch event fires button

3.  create physics body obj1

4.  request that obj1 be removed from the display

5.  create physics body obj2 (will collide and resolve vs obj1, thus offsetting/rotating it)

6.  create physics body obj3 (will collide and resolve vs obj2, thus offsetting/rotating it)

7.  lua interpreter cycle concludes

8.  scene is processed for rendering (including removal/cleanup/whatever)

perhaps the easiest way to “prove” that to yourself is via the much-abused 1ms timer:

local function cut( event ) if ( "ended" == event.phase ) then physics.addBody( obj1) obj1:removeSelf() -- do the rest on the NEXT frame (ie, after obj1 has been fully disposed of) timer.performWithDelay(1, function() physics.addBody( obj2 ) physics.addBody( obj3 ) end) end end

The “bouncing effect” does occur in both cases, it’s just more subtle with the more narrow obj1. You can see it better if you use physics.setDrawMode( “hybrid” ).

Corona is frame-based and my guess is that since you create and remove those objects within a few lines of each other, the physics engine doesn’t have the time to adjust to your changes. It could be that the engine cannot create and remove an object during the same cycle, but instead it does the removal at the very next possible cycle, so all three objects exist together for that one cycle before obj1 is removed.

Also, when requiring physics, you should use:
 

local physics = require("physics")

Xedur@Spyric’s guess is correct - the “cleanup” of object “destruction” is deferred until end-of-frame, so “obj1” kinda-sorta still exists at the moment that you add obj2 and obj3 (and physics lib doing overlap fixup on insertion is one of those rare things that does happen immediately).  So your sequence is something like this:

1.  lua interpreter cycle begins

2.  queued touch event fires button

3.  create physics body obj1

4.  request that obj1 be removed from the display

5.  create physics body obj2 (will collide and resolve vs obj1, thus offsetting/rotating it)

6.  create physics body obj3 (will collide and resolve vs obj2, thus offsetting/rotating it)

7.  lua interpreter cycle concludes

8.  scene is processed for rendering (including removal/cleanup/whatever)

perhaps the easiest way to “prove” that to yourself is via the much-abused 1ms timer:

local function cut( event ) if ( "ended" == event.phase ) then physics.addBody( obj1) obj1:removeSelf() -- do the rest on the NEXT frame (ie, after obj1 has been fully disposed of) timer.performWithDelay(1, function() physics.addBody( obj2 ) physics.addBody( obj3 ) end) end end