Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Angle Vectoring

Credits go to Arthur Kalliokoski for helping out!
This thread is locked; no one can reply to it. rss feed Print
Angle Vectoring
Don Freeman
Member #5,110
October 2004
avatar

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 = V<x = player.x - target.x, y = player.y - target.y>
To 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) = <Ux*Vx+Uy*Vy> / |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
avatar

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

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

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
avatar

atan2
EDIT: It should work better than the code above, as atan2 handles all cases intenally. :)

Niunio
Member #1,975
March 2002
avatar

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
avatar

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: