Search In
• More options...
Find results that contain...
Find results in...
• 0

# atan2 is bothering me (zscript)

## Question

Just trying to derive an angle and speed from the player's X and Y velocities. Here is the vomit code that I have so far:

```{
airangle = atan2(Vel.Y - 0.0, Vel.X - 0.0);

if(Vel.X <= 0)
{
let lame_a = Vel.X;
}
else
{
let lame_a = Vel.X * -1.0;
}

if(Vel.Y <= 0)
{
let lame_b = Vel.Y;
}
else
{
let lame_b = Vel.Y * -1.0;
}

let lame_c = sqrt(lame_a + lame_b);
}```

## Recommended Posts

• 0

You're doing the Pythagorean theorem wrong. It's supposed to be the sum of the  square of the velocities. If you do that then you don't need either of the if blocks, since the square of any real number is always positive.

`let lame_c = sqrt(Vel.X * Vel.X + Vel.Y * Vel.Y);`

• 0

Thanks, I figured the extra steps weren't necessary, but I've been unable to think clearly at all recently...

It just occurred to me that I should've specified what my problem actually is. I'm trying to record the raw speed and direction of the player for the current tic, but I must be using atan2 incorrectly, because it seems to only ever result as 0, which is east.

• 0
Posted (edited)

I don't understand why you subtract 0.0. It doesn't seem like it should be needed.

`airangle = atan2(Vel.Y, Vel.X);`

Ought to work just as well. (Or just as poorly, I guess.)

Also this thread should have been titled "Whispers of Atan2" IMO.