Search code examples
luacoronasdkphysics

How to remove all physical bodies from physical


I'm working on a corona project and I now want to remove all bodies from the physics.

I see it has only a method to remove a body but not all

physics.removeBody()

I need to remove all, like that

 physics.removeAllBodies()

Who can give me a way to do that.

Thank you


Solution

  • The only way to do that is to add each display body to a physicsBodies table whenever you add to physics:

    local physicsDisplayObjects = {}
    
    ...
    
    function scene:createScene(event)
        ...
        local displayObject1 = ... -- display object
        physics.addBody(displayObject1, ...) -- transform it into a physics body
        table.insert(physicsDisplayObjects, displayObject1)
        ...
    end
    
    ...
    
    function something()
        ...
        for i,obj in ipairs(physicsDisplayObjects) do 
            physics.removeBody(obj) -- revert obj into regular display object
        end
        physicsDisplayObjects = {} -- clear
        ...
    end
    
    ...