Getting the True Bearing between 2 points. [Still Not Working]

Started by
12 comments, last by j_k 18 years, 5 months ago
How can I find the true bearing between 2 points? Ie; A . _ _ _ _ _ . B The true bearing of A from B would be 270o, but how can I work that out in code, given the coordinates of both? I've searched all over the forums and found nothing that helps me. [Edited by - Zmurf on November 4, 2005 11:59:13 PM]
If it's possible for us to conceive it, than it must be possible.
Advertisement
90 - ARCTAN((y2-y1)/(x2-x1))

I think that works.
Young Doc: No wonder this circuit failed. It says "Made in Japan".Marty McFly: What do you mean, Doc? All the best stuff is made in Japan.Young Doc: Unbelievable.
Heres the code i'm using, but it isn't working ><

double CLimb::PointDir(double x1, double y1, double x2, double y2){	double rise, run, dir;	rise = y2 - y1;	run = x2 - x1;    if (x1 > x2)		dir = 90 - atan(rise/run);	else		dir = 90 + atan(rise/run);	dir *= RADTODEG;		if (dir >= 360)		dir = dir - 360;	if (dir < 0)		dir = 360 - abs(dir);	return dir;}


But i'm getting weird results, it's facing the opposite direction and and only seems to turn in a 90 degree area.
If it's possible for us to conceive it, than it must be possible.
If you are using C/C++, use the function atan2(). It is much superior to atan() in this case.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:A . _ _ _ _ _ . B

The true bearing of A from B would be 270o, but how can I work that out in code, given the coordinates of both?
Given the usual convention of 0 degrees being along the +x axis, the solution would be:
float x = A.x-B.x;float y = A.y-B.y;float angle = atan2(y,x);
From your example it looks like you want a different orientation for 0 degrees, so you'll have to add some (integer) multiple of 90 to your result, or swap and/or negate the arguments to atan2().

For this particular problem you should prefer atan2() over atan() as it has better behavior over the range of inputs. Also, remember that c++ math functions work with radians, so you'll need to convert back and forth if you're using degrees.
Well, for one in C/C++ the trig functions are in radians so I'm surprised you get turned within 90 degrees since atan is going to return values between -pi/2 and pi/2, i.e. ~+/-1.57. You have code for radians to degrees, buy you have that "90 - atan" in there before you get to it. Second you can use atan2 to get the full 0 to 2pi range so you don't need the if statements.
Keys to success: Ability, ambition and opportunity.
ok that works, thanks
If it's possible for us to conceive it, than it must be possible.
hmm, my new code isn't working just right.

double PointDir(double x1, double y1, double x2, double y2){	double dir, x, y;	x = x2 - x1;	y = y2 - y1;	dir = atan2(y, x) * RADTODEG;	if (dir> 360)		dir = dir - 360;	if (dir < 0)		dir = 360 - abs(dir);	return dir;}


Since atan2 gets the angle from the origin, i adjust the position its finding as if (x1, y1) were at the origin and looking to (x2, y2) so then it should return the correct direciton from xy1 to xy2. but for some reson it doesn't, instead its doing this:

A ....... 0....
...............
...............
....B..........X

A should be direction to X, but instead its facing to B.

It's kinda confusing but it's hard to explain.
If it's possible for us to conceive it, than it must be possible.
I'm not sure what your problem is here, you've got the direction vector from A to B being the vector X, your diagram appears to be correct. The direction vector in this case is the length and direction of how to get to point B from point A.
Well i have a line, and i want the line to always point to the mouse, the line starts from A and heads to be, but the mouse is at X. The line should go from A -> X but it doesn't, i need to know how to fix my code so it does this.
If it's possible for us to conceive it, than it must be possible.

This topic is closed to new replies.

Advertisement