# Figuring out which direction my object is facing on a 2D plane?

My friend and I were messing around in XNA 4.0 making a 2D racing game. Kind of like this one: Ivan “Ironman” Stewart’s Super Off Road. The problem we are having is knowing which direction the car is facing to move it appropriately. We could track the direction by a enum value North, South, East, West but we don't want to do that for a number of reasons.

We were wondering if there was a way to accomplish this via math. Maybe by having an anchor point designated at the hood of the car and having the car always move towards that spot and then move that anchor point. We aren't sure. Or maybe there is a way using a 2D Vector.

I figured since we hit a hard spot, we should ask the coding community for help!

Just to be clear. I'm not looking for code; I just want to discuss some concepts of 2D movement in all directions without having to track a direction enum. I know that can't be the only way to do it.

• This place is not meant for discussions. We only handout answers that are useful to you and everyone else facing the same problem. – Sidar Jan 28 '13 at 20:37
• Sidar, if you want to hand me the answer/write the code for me go ahead. The game's input device is a keyboard using WASD to move. But on a serious note thanks for the response I'll look into your method. – DuxClarus Jan 28 '13 at 22:57
• No one will write your code either. I was just saying that the format on GDSE is not meant to support discussions. – Sidar Jan 29 '13 at 14:44

Vector :

Vectors describe the magnitude and direction. If you just want the direction you simply normalize it. This causes the vector to shrink to fit into the unit circle. Meaning that it has magnitude/length of 1.

From rotation angles:

To get the x and y components for your direction:

//(pi/180) - > degrees to radians. (180/pi) - > radians to degrees
x = cos(angle*(pi/180));
y = sin(angle*(pi/180));


This is similar to a vector after being normalized.

You simple multiply these values/vectors to change your velocity ( or whatever that requires the direction) with a speed. So that the x and y components extend to the right length respectively.

• I agree the vector that represents your velocity should show you which direction you are moving. – Jackie Jan 28 '13 at 21:23