Allegro.cc Forums » Programming Questions » Angle Vectoring

Credits go to Arthur Kalliokoski for helping out!
 Angle Vectoring
 Don Freeman Member #5,110 October 2004 Ok...I'd first like to say hello again to everyone ...it's been a while since I've been able to get back here.Here's my question:How do you find the angle between two points. I am trying to find this angle so I can vector the object to the target, such as a homing missle. The structure would have at least:typedef struct _Object{ float x, y, angle, vel;}Object;I've looked at a couple of math equations, but I may be doing something wrong or I am just totally confused. To find the vector:V = VTo find the length of the vector: (Also just the distance from (0,0)-(Vx,Vy) )Vlen = sqrt(V.x*V.x+V.y*V.y);I think you use the DOT Product to find the angle, but when I try it...it doesn't seem to work for me: DotProduct = -cos(angle) = / |U| * |V|where |U| & |V| is the length of U & V respectfully.The goal is to find the angle from (player.x, player.y) to (target.x, target.y) so I can determine where to turn left / right or keep on current angle to reach target.Thanks in advance.(Please...any math problems...please explain. I get confused with some of the expressions and stuff) --"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you.""It’s so simple to be wise. Just think of something stupid to say and then don’t say it."
Arthur Kalliokoski
Second in Command
February 2005

Here's some console prog stuff to demonstrate radians to polar coords and vice versa

 1 2 // polar coordinates to rectangular 3 4 #include 5 #include 6 #include 7 8 double x,y; 9 10 double angle,radius; 11 12 int main(int argc, char **argv) 13 { 14 15 angle = strtod(argv[1],0); 16 radius = strtod(argv[2],0); 17 18 //convert degrees to radians 19 angle = angle * M_PI/180.0; 20 21 y = sin(angle) * radius; 22 x = cos(angle) * radius; 23 24 printf("\nx is %f y is %f",x,y); 25 26 return 0; 27 } //end 28 29 //rectangular coordinates to polar 30 31 #include 32 #include 33 #include 34 35 double x,y; 36 37 double x2,y2; 38 39 double angle,radius; 40 41 double tmpangle; 42 43 int main(int argc, char **argv) 44 { 45 46 x = strtod(argv[1],0); 47 y = strtod(argv[2],0); 48 49 radius = sqrt(x*x+y*y); 50 51 //if( (x*y) < 0.0) radius = radius * -1.0; 52 53 x2 = asin(fabs(x)/radius); 54 55 //this would work just as well as the above, they come out the same 56 //y2 = acos(y/radius) * 180.0/M_PI; 57 58 59 tmpangle = x2; 60 if(y<0) 61 { 62 if(x >= 0) tmpangle = M_PI * 2.0 - x2; 63 else 64 tmpangle = x2 + M_PI; 65 } 66 else 67 if(x<0) tmpangle = M_PI - x2; 68 69 x2 = tmpangle * 180.0/M_PI; 70 71 printf("\n%f degrees at %f unit distance",x2,radius); 72 73 return 0; 74 }

gcc math lib is probably necessary, -lm.

They all watch too much MSNBC... they get ideas.

 Don Freeman Member #5,110 October 2004 Thanks! I knew I was missing something...but I couldn't think of what it was! Had to convert radians to polar coords...just didn't know how! Thanks again! Now...where did I leave off with this program....::) --"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you.""It’s so simple to be wise. Just think of something stupid to say and then don’t say it."
 Krzysztof Kluczek Member #4,191 January 2004 atan2EDIT: It should work better than the code above, as atan2 handles all cases intenally. ________[ My LD48 entry ] [ My SpeedHack`04 entry ] [ TEAM ALLEGRO ] [ My TINS'05 entry - Snake ]
 Niunio Member #1,975 March 2002 Or use dot product.Amarillion has a great tutorial about sin, cos and all that stuff. It should be at pixwiki, but now bafsoft server seems down so I can't find it. Anyway, I have a copy of most of pixelate tuts so I've attached the tutorial, wich includes both atan2 and dot product examples. -----------------Current projects: Allegro.pas | MinGRo
 Arthur Kalliokoski Second in Command February 2005 atan2, huh! I don't remember why I did the above code (years ago), maybe it was so I could do it in asm as well, or maybe my compiler didn't have an atan2 then. I thought it wasn't supported by all compilers but dj info says it's ANSI. Learn something new every day. They all watch too much MSNBC... they get ideas.
 Andrei Ellman Member #3,434 April 2003 The dot product is overkill for 2 dimensions. I'd use atan2.AE. --Don't let the illegitimates turn you into carbon.
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads