benLIVE
benLIVE

Reputation: 597

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.

Upvotes: 0

Views: 780

Answers (2)

dburdan
dburdan

Reputation: 459

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/.

Upvotes: 2

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3992

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
-----------------------------------

Upvotes: 2

Related Questions