Finding direction of travel in a world with wrapped edges
        Posted  
        
            by 
                crazy
            
        on Game Development
        
        See other posts from Game Development
        
            or by crazy
        
        
        
        Published on 2011-11-15T15:18:40Z
        Indexed on 
            2011/11/16
            2:10 UTC
        
        
        Read the original article
        Hit count: 398
        
c++
I need to find the shortest distance direction from one point in my 2D world to another point where the edges are wrapped (like asteroids etc). I know how to find the shortest distance but am struggling to find which direction it's in.
The shortest distance is given by:
int rows = MapY;
int cols = MapX;
int d1 = abs(S.Y - T.Y);
int d2 = abs(S.X - T.X);
int dr = min(d1, rows-d1);
int dc = min(d2, cols-d2);
double dist = sqrt((double)(dr*dr + dc*dc));
Example of the world
                   :         
                   :  T    
                   :         
    :--------------:---------
    :              :
    :           S  :
    :              :
    :              :
    :  T           :
    :              :
    :--------------:
In the diagram the edges are shown with : and -. I've shown a wrapped repeat of the world at the top right too. I want to find the direction in degrees from S to T. So the shortest distance is to the top right repeat of T. but how do I calculate the direction in degreed from S to the repeated T in the top right?
I know the positions of both S and T but I suppose I need to find the position of the repeated T however there more than 1.
The worlds coordinates system starts at 0,0 at the top left and 0 degrees for the direction could start at West.
It seems like this shouldn’t be too hard but I haven’t been able to work out a solution. I hope somone can help? Any websites would be appreciated.
© Game Development or respective owner