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)
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 | |
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 <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <math.h> |
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.
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....::)
atan2
EDIT: It should work better than the code above, as atan2 handles all cases intenally.
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.
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.
The dot product is overkill for 2 dimensions. I'd use atan2.
AE.