Results 1 to 4 of 4

Thread: Finding the heading (NSEW) between 2 points?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Finding the heading (NSEW) between 2 points?

    A 2D sprite has an x/y location, x/y direction, and x/y velocity.
    During each update call, its location = (location + (direction * velocity)).
    This enables it to travel in any and all directions at varying speeds.
    Using this information, how would you find the angle (in degrees) between it's current location, and it's next location?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Finding the heading (NSEW) between 2 points?

    The homework is strong in this one...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: Finding the heading (NSEW) between 2 points?

    The productivity is strong in that comment...

    And yes, I've done my homework. I've calculated the normalized dot product and found it's inverse cosign, which gives me the angle in radians. If I converted that value to degrees, it would only be between 0 and 90 and then it would reset to 0. So this is an issue with quadrants, which I don't know how to solve. Thus, why I came here to find answers or other possible algorithms.

    Thanks for the help.

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Finding the heading (NSEW) between 2 points?

    The inverse cosine will give you angles between 0 and 180, not 0 and 90, degrees. The most direct solution is the "atan2" function; there's a Wikipedia page on it. You can also extend your inverse cosine method to handle quadrant issues without too much trouble by simply branching if the y component of the difference is negative. I can give more details if you need, but they're so simple I won't write them unless asked.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width