DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Calculating the direction fron one point to another

  1. #1
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72

    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 non-right angles. Can anyone help me?
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    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)

  3. #3
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    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.
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    you have to tan^-1 it, i was just quoting the standard formula :)

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    which is atan, meaing arc-tan, from before the -1 for inverse caught on.

  6. #6
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    ::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; 05-01-2005 at 09:24 PM.
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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 ...

  8. #8
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    Okay, so shoud I change my calculations based on the different quadrants?
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.hyper-ad.com/tutoring/mat...%20Angles.html
    Last edited by nspils; 05-03-2005 at 11:53 PM.

  10. #10
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    okay, thank you. I'm going to read that site and I think I have it. Thank you everyone.
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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