Trig Question
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?
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.
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.
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!
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.
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.)
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
I'm about 95% sure that's right, I can't check.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement