0
\$\begingroup\$

I am developing a game that is being played on a board with the top first tile having the coordinates [0,0], the next one has [0,1], second row has [1,0]... and so on (most likely this would be a standard but im not sure...).

When an object is to move around the board, it should first rotate to a specific angle - meaning if it is moving north North - it should point to the default angle 0, NE is 45, E is 90, and so on.

How would I get this angle based on where the object is moving, but using 1 formula?

So if an object is to move NW (up and left), on the grid that would mean the destination is [y-1, x-1] in reference to where it is now [y,x] ( [row, column] ).

I know I could use 8 statements and say if destination == [y-1, x-1] then angle = 315, but I was wondering if there is a specific formula for this? Im guessing yes but I never loved trigonometry...

\$\endgroup\$
1
\$\begingroup\$

Your movement vector is obtained substracting source position to target position

and the angle usually in radians is

  Vector2 V = Target - Source; 
  angle = Math.Atan2(V.Y, V.X)
\$\endgroup\$
  • \$\begingroup\$ This isn't really a single formula, since you'd still need to use some kind of lookup table to get the Target and Source angles. \$\endgroup\$ – MichaelHouse Aug 6 '13 at 0:11
  • \$\begingroup\$ Target and source are not angles... are cell positions.. you dont need a lookup table.. you need to know where you go ;) \$\endgroup\$ – Blau Aug 6 '13 at 0:29
  • \$\begingroup\$ Ah I see. I'm not sure why I thought it was something different. \$\endgroup\$ – MichaelHouse Aug 6 '13 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.