Search code examples
luacoronasdkphysics-engine

How to determine when bodies in display stop moving in lua


I have some bodies moving in display using physics. I want to determine when bodies stop moving so as to call a function to take an action. I tried a function like so:

function ball:stopMove ()
    if condition then
        print("Game Over!!!")
    end
end

This do not get invoked. how can I do this correctly?


Solution

  • You can here to see how to implement your own callbacks. Basically you can create a proxy table that acts as a filter, screening for certain actions and acting on those.

    If you don't want to do that, assuming you created a physics body, the function you are looking for is object:getLinearVelocity. If the object's x and y velocity are zero, the body has stopped moving. You would, of course, need to check for this every update.

    Alternatively, you can calculate this yourself if you aren't using a physics body. All you need to do is keep track of the object's last x and y positions, and if the values are the same, the object has not moved since the lasted update.