简体   繁体   中英

Corona: How to detect the direction of an object with physics

I would like to know how to detect the direction of an object with physics, I mean when is falling down. can it be by a eventListener? any idea how to do it?

I need it for know can I change the spriteSheet.

Thanks.

Try this :

local xVelocity, yVelocity
local upDown, leftRight         -- upDown = 1 for up, leftRight = 1 for left
....
-- Get speed of physics object here ( Assume normal orientation ---
    xVelocity, yVelocity = physicsObject:getLinearVelocity()
    if xVelocity > 0 then
        print( "Object goes right" )
        leftRight = 0
    end
    if xVelocity < 0 then
        print( "Object goes left" )
        leftRight = 1
    end
    if yVelocity > 0 then
        print( "Object goes down" )
        upDown = 0
    end
    if yVelocity < 0 then
        print( "Object goes up" )
        upDown = 1
    end
-----------------------------------

To find the exact angle based on an objects velocity you'd call:

 local angle = atan2(xVelocity, yVelocity)

This returns the angle in radians, which you can then convert to degrees. This allows for more precise control over the object. Daniel Shiffman wrote a great book involving many aspects of physics simulation over at http://natureofcode.com/book/ .

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