Log In  

I'm trying to animate a bullet going from one place to another. First I get the angle from the source to the destination by doing

local angle = atan2(startx - endx, starty - endy)

then I move the bullet from the source to the destination by using that angle and incrementing the distance from the source

bulletx = startx + dist * cos(angle)
bullety = starty + dist * sin(angle)

but the angle is all messed up - the bullet goes in completely the wrong direction.

As far as I can tell the units of atan2, sin and cos are the same so I don't think converting between radians and degrees is the problem. I googled getting the angle between two points and getting a position from an angle but every example I found in other languages seemed consistent with what I'm doing...

Can anyone point out what I'm doing wrong?

P#36521 2017-01-23 22:21 ( Edited 2017-01-24 11:39)

2

I think you intended:

local angle = atan2(endx - startx, endy - starty)

More information on Pico-8 trig: http://pico-8.wikia.com/wiki/Atan2

P#36540 2017-01-24 00:28 ( Edited 2017-01-24 05:28)
1

That plus fixing an operator precedence mistake I made (a + b c instead of (a + b) c) fixed it, thanks!

P#36555 2017-01-24 06:39 ( Edited 2017-01-24 11:39)

[Please log in to post a comment]