Best way to remove Box2d physics body

Hi,

i am starting to learn Corona SDK + Lua and making a little game, where the collision and player movement

works with the Box2d physics. When the player hits an object, i want the object to disappear. Until now, after collision i managed to remove the display object of the hit target, but the physics body still remains there, even with physics.removeBody(object).

Any help?

greets, Alex

physics.removeBody(object) is definitely the right function call to remove a physics body. If you remove the display object, the physics body should be removed anyway.

However, there are problems with removing an object DURING a physics collision, the only way around it afaik is to set a timer to wait 1ms and then remove the object.

E.g (I’ll leave out the bulk off the collision function setup, I presume you have that working already).

function mycollision(event) if ( event.phase == "began" ) --check to see if this is a removable object if event.other.name == "enemyBullet" then --if so, set a timer to remove it timer.performWithDelay(1, function() event.other:removeSelf() event.other = nil end, 1) end end end

physics.removeBody(object) is definitely the right function call to remove a physics body. If you remove the display object, the physics body should be removed anyway.

However, there are problems with removing an object DURING a physics collision, the only way around it afaik is to set a timer to wait 1ms and then remove the object.

E.g (I’ll leave out the bulk off the collision function setup, I presume you have that working already).

function mycollision(event) if ( event.phase == "began" ) --check to see if this is a removable object if event.other.name == "enemyBullet" then --if so, set a timer to remove it timer.performWithDelay(1, function() event.other:removeSelf() event.other = nil end, 1) end end end