Euclidean distante = sqrt (dx * dx + dy * dy), for your distance you need to define dx as min(abs(x2-x1),screenwidth-abs(x2-x1)) same thing for dy. Usualy the direction vector is Pt - Pm (Position vector target - Position vector missile). In your case you can define Direction vector x component as if (x2-x1)>=0 DirX= min(x2-x1 , width -(x2-x1)) else DirX= max(x2-x1 , width + (x2-x1)) [![enter image description here][1]][1] In the same way (using HEIGTH) you can DirY. [1]: https://i.sstatic.net/FNEI7.png