Making sprite face angle of movement and direction?

Hi, I have a set of bullet looking sprites which move to different parts of the screen, they curretly point up but Id like it so they point towards where they are going, here’s my code, also I would like the speed to remain as the x,y params below:

 x = math.random(-100,100) y = math.random(-100,100) smile = display.newImageRect("Button.png", 45, 45); smile.x = display.contentCenterX smile.y = display.contentCenterY physics.addBody( smile, "dynamic" ) smile:setLinearVelocity( x, y , x,y )

The sprite would be something like the link below, so ideally if it went up itd face up, same for down, left, right, other angles etc.

http://images.clipartpanda.com/chevron-clipart-68041_119_w1-8_s_lg.gif

Here’s a simple angle snippet:

 local function angleBetween ( srcObj, dstObj ) if srcObj == nil or dstObj == nil then return true else local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y local angleBetween = math.deg( math.atan( yDist/xDist ) ) if ( srcObj.x \< dstObj.x ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end print("angleBetween = "..tostring(angleBetween)) return angleBetween - 90 end end local testRect1 = display.newRect(display.contentCenterX,display.contentCenterY, 20,40) local testRect2 = display.newRect(display.screenOriginX,display.screenOriginY, 20,40) testRect2:setFillColor(1,0,0) -- red local angle = math.abs(angleBetween(testRect1,testRect2)) testRect1.rotation = angle

I think you want to get the angle between two objects (player, enemy) and that is theory here.

Thanks, doesnt seem to be what I want in my scenario though :frowning: Another way I’m looking at it is by getting the linearvelocity and then trying to conver that into degrees

 smile:setLinearVelocity( x, y ) x, y = smile:getLinearVelocity() local angle = math.atan2( x,y ) \* 180

Although my shoddy maths renders this useless :slight_smile: Any ideas?

Hi @never3eden,

I personally use the following function, fairly similar to the one Alex provided:

[lua]

local function angleBetween( srcX, srcY, dstX, dstY )

    local angle = ( math.deg( math.atan2( dstY-srcY, dstX-srcX ) )+90 )

    return angle % 360

end

[/lua]

Now, since it takes X and Y positions, it’s not “perfect” for your usage, but since you’re outright setting a linear velocity, I think you can just provide it with the difference between the conceptual points like this, where “x” and “y” are the values you’re planning to pass to the :setLinearVelocity() call:

[lua]

local angle = angleBetween( smile.x, smile.y, smile.x+x, smile.y+y )

[/lua]

Best regards,

Brent

Brent is way ahead of me, but I had a syntax error and corrected it. it should work as intended now. You don’t need the angle formula to interact with your physics logic, FYI.

Thanks to both of you, honestly you guys are geniuses !

Brent, not sure if you have seen my previous effort :

https://play.google.com/store/apps/details?id=com.nevereden.keepcounting

You were the one to actually give me the right piece of code which ensured each number doesn’t repeat on the grid and that they don’t overlap the same co ordinates.

Here’s a simple angle snippet:

 local function angleBetween ( srcObj, dstObj ) if srcObj == nil or dstObj == nil then return true else local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y local angleBetween = math.deg( math.atan( yDist/xDist ) ) if ( srcObj.x \< dstObj.x ) then angleBetween = angleBetween+90 else angleBetween = angleBetween-90 end print("angleBetween = "..tostring(angleBetween)) return angleBetween - 90 end end local testRect1 = display.newRect(display.contentCenterX,display.contentCenterY, 20,40) local testRect2 = display.newRect(display.screenOriginX,display.screenOriginY, 20,40) testRect2:setFillColor(1,0,0) -- red local angle = math.abs(angleBetween(testRect1,testRect2)) testRect1.rotation = angle

I think you want to get the angle between two objects (player, enemy) and that is theory here.

Thanks, doesnt seem to be what I want in my scenario though :frowning: Another way I’m looking at it is by getting the linearvelocity and then trying to conver that into degrees

 smile:setLinearVelocity( x, y ) x, y = smile:getLinearVelocity() local angle = math.atan2( x,y ) \* 180

Although my shoddy maths renders this useless :slight_smile: Any ideas?

Hi @never3eden,

I personally use the following function, fairly similar to the one Alex provided:

[lua]

local function angleBetween( srcX, srcY, dstX, dstY )

    local angle = ( math.deg( math.atan2( dstY-srcY, dstX-srcX ) )+90 )

    return angle % 360

end

[/lua]

Now, since it takes X and Y positions, it’s not “perfect” for your usage, but since you’re outright setting a linear velocity, I think you can just provide it with the difference between the conceptual points like this, where “x” and “y” are the values you’re planning to pass to the :setLinearVelocity() call:

[lua]

local angle = angleBetween( smile.x, smile.y, smile.x+x, smile.y+y )

[/lua]

Best regards,

Brent

Brent is way ahead of me, but I had a syntax error and corrected it. it should work as intended now. You don’t need the angle formula to interact with your physics logic, FYI.

Thanks to both of you, honestly you guys are geniuses !

Brent, not sure if you have seen my previous effort :

https://play.google.com/store/apps/details?id=com.nevereden.keepcounting

You were the one to actually give me the right piece of code which ensured each number doesn’t repeat on the grid and that they don’t overlap the same co ordinates.