
Calculating the direction fron one point to another
I've been trying to add a function to my game engine that calculates the direction from one point to another:
float points_direction(float x1, float y1, float x2, float y2)
But I can't figure it out since the only angle I have is the 90 degree one formed by the two legs. I guess the angle we're trying to find is opposite/adjacent or something... But the only numbers we know are the vertical and horizontal legs' lengths and the length of the hypotenuse using the distance formula. I need the 2 nonright angles. Can anyone help me?

If you want the distance between the two points you don't need to know any angles. The distance between them (which is the hypotenuse) is...
a^2 = b^2 + c^2, where a is the hypotenuse, and b and c are the two other lengths. Therefore
a = sqrt ( (x2  x1)^2 + (y2  y1)^2).
The title of this thread suggests you want to find the angle from point1 to point 2, in which case you need trigonometry.
tan(theta) = opp/adj = (y2  y1) / (x2  x1)

Sorry, I meant to say 'direction'. Does "tan(theta) = opp/adj = (y2  y1) / (x2  x1)" mean that opposite/adjacent equals the measurement of the angle? Or do you have to tan^1 it to find the angle, since you're finding the tan with that formula.

you have to tan^1 it, i was just quoting the standard formula :)

which is atan, meaing arctan, from before the 1 for inverse caught on.

::brain melts:: okay, so... can someone give me some example code? I'm having trouble understanding all this. Sorry.
All I need is how to find the angle with the two sides given, I just need to see how this all works.
Or would the following work?
float points_direction(float x1, float y1, float x2, float y2)
{
float adjacent = x1  x2;
float opposite = y1  y2;
float angle = atan(opposite/adjacent);
return angle;
}
Last edited by Algorien; 05012005 at 09:24 PM.

I see that the math of it all is covered ...
But if you want to approximate direction, if y2 is larger than y1 you are going "up" or "northward", if y2 is smaller than y1 then you are going "southward"; if x2 is larger than x1 you are going "eastward", if x2 is smaller than x1 you are going "westward". You can see that you've broken the view into four quadrants, so by mixing the four indicators you can tell if you are going north, south, east, west, northwesterly, southwesterly, south easterly, and north easterly.
But, seriously ...

Okay, so shoud I change my calculations based on the different quadrants?

The result of your equation will refer to quadrants, because your result will be negative ("southerly") or positive (northerly), and will range from certain values to certain other values to tell you if you are "westerly" or "easterly" ...
remember that you can also use radians to compute these values and then convert the answer to degrees  that will give you a better "direction"  you will be able to feed your program an actual compass heading based on the result of your calculation in radians ...,
Here is a link to help you with the conversion and will help brush up your trigonometry:
http://www.hyperad.com/tutoring/mat...%20Angles.html
Last edited by nspils; 05032005 at 11:53 PM.

okay, thank you. I'm going to read that site and I think I have it. Thank you everyone.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
