Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • 0
Sign in to follow this  
Blastfrog

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);
}

 

Share this post


Link to post

3 answers to this question

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

 

Share this post


Link to post
  • 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.

Share this post


Link to post
  • 0

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.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×