Jump to content
  • Advertisement
Sign in to follow this  
BlackDragon777

Trig Question

This topic is 4465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I hae two objects. One is a blue arrow who's position will never change. Another is a red arrow who's position will change. While the red arrow's position is changing, I want the blue arrow to point towards it. So I need to figure out what angle to rotate the blue arrow to be pointing at the red arrow. How do I do this?

Share this post


Link to post
Share on other sites
Advertisement
Well if both objects can be represented using points(x,y) you could just measure the angle for the line created between the two points.

This is from DooM.Net:

int Angle = (int)Direct3D.Geometry.RadianToDegree( (float)Math.Atan2( V2.Y - V1.Y, V2.X - V1.X ) );

V1 and V2 are just points.

I make no guarantees that this is correct though, as I've had to make my fair share of math forum help topics.

Share this post


Link to post
Share on other sites
Hmmm... That works but how could you write the function atan2 for a programming language that doesn't have that function built in? Say like C++, if I were to write it from scratch. Thanks in advance!

Share this post


Link to post
Share on other sites
Quote:
Original post by BlackDragon777
Hmmm... That works but how could you write the function atan2 for a programming language that doesn't have that function built in? Say like C++, if I were to write it from scratch. Thanks in advance!


Please tell me you're not writing everything(including basic math fucntions) from "scratch"!?

Include the header cmath( #include <cmath> ). You should now have access to a function called atan2 that takes two doubles as arguments.

Share this post


Link to post
Share on other sites
I've never done this so this might not be the best way to implement it, but if I would take a shot I would approximate atan with a bunch of Taylor series of atan. (I.e. an approximation of atan by an nth degree polynomial.)

Share this post


Link to post
Share on other sites
Basically you have to check the quadrant of the x and y values after you calculate the arctangent. for instance, say you have the point x = 3, y =2. arctan( 2/3) is about 33.69 degrees. The result is in the first quadrant so the answer is correct. If you have x = -3, y = 2 then arctan(2/-3) then you should have the angle -33.69 degrees. The result should be in the second quadrant. The angle should be 146.31. This is 180 degrees move than -33.69. Indeed, if the result is in the 2nd quadrant the angle is 180 + arctan(y,x) If you have x = -3, y = -2 then arctan(-2/-3) is again 33.69. The result should be in the third quadrant and the real angle should again be 213.69 degrees. This is again 180 degrees more than the result given by arctan so if the result should be in quadrant 3 the angle is 180 + arctan(y,x). Finally, if x = 3, y = -2 arctan(-2/3) = -33.69 degrees. the angle Should be 326.31 degrees which is 360 more than the result given by arctan.

Therefore
float atan2(float x, float y) {
if(x < 0) { return arctan(y/x) + PI; }
if(y < 0) { return arctan(y/x) + 2*PI; {
return arctan(y/x);
}


I'm about 95% sure that's right, I can't check.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!