Jump to content
  • Advertisement
Sign in to follow this  
Zmurf

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

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!