简体   繁体   中英

How to check if an object is accelerating downwards in corona sdk?

How can I check if whether or not an object is accelerating downwards in corona SDK?

I am using the corona SDK physics engine.

Note that object could be moving downwards but decelerating due to an upwards force which will eventually reverse its motion to be upwards. Similarly it could be moving upwards but decelerating due to a downward force that will eventually make it go down. So "accelerating downwards" is not clear question.

If you really mean moving downwards and gaining speed, you could use body.getLinearVelocity() at every enterFrame event and compare with previous:

local oldVy = 0

function enterFrame(e)
    local newVx, newVy = myBody:getLinearVelocity()
    if newVy > 0 and newVy > oldVy then
        print 'accelerating downwards'
    end
    oldVy = newVy
end

Runtime:addEventListener('enterFrame', enterFrame)

Can't check right now but positive y velocity probably means downward, otherwise you'll have to adjust the test condition. In any case one part of condition is "moving downwards" and the other part is "increasing speed in that downward direction".

Note that "accelerating" is key in your question: if you just want to know whether it is moving downwards (but could be ), then you don't need the newVy > oldVy check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM