If I want to move a sprite to the right and upwards in a 45 degree angle I use

 Vector2 direction = new Vector2(1,-1);

And if the sprite should move straight to right

 Vector2 direction = new Vector2(1,0);

But how would I do if I want another directions, lets say somewhere between this values? I have tested with some other values, but then the sprite either moves to fast and or in another direction than I expected!? I suspect it's about normalize!? Another thing I have problem with to understand.

I'm doing a simple asteroids game as a task in a class in C# and XNA. When the asteroids hit the window borders, I want them to bounce back in a random direction, but I can't do this before I understand how directions and Vector2 works. Help is preciated! Thanks!


2 Answers 2


You'll find that your first value is moving about 1.4 times as fast as the second, based on the lengths of the vectors:

length = sqrt( x*x + y*y )
sqrt( 1*1 + -1*-1 ) = 1.414...
sqrt( 1*1 + 0*0 ) = 1

And you're right, it's about normalizing values. All normalizing does is make the length of that vector 1 unit long - hence it's called a unit vector. On the plus side, normalizing is simple! Either languages have it build in and you can just do:

Vector2 dirNormalized = direction.Normalize();

Or, you can do it manually by dividing each component by the length:

float length = sqrt( direction.X*direction.X + direction.Y*direction.Y )
Vector2 dirNormalized = new Vector2( direction.X / length, direction.Y / length );

I would use the built-in methods. Much less hassle. ;)

If you want to specify directions at arbitrary angles, the easiest way to do this is using trigonometry:

Vector2 direction = new Vector2();
direction.X = Math.Cos( angleInRadians );
direction.Y = Math.Sin( angleInRadians );

This will give you a unit vector in the direction of angleInRadians from your left example, going counter-clockwise. If this sounds complicated, think of it as a clock hand that starts at 3 and goes counter-clockwise as the angle increases, and reaches back to 3 at 2*PI radians, or 360 degrees.

To get extra-fancy, multiply these by a 'speed' value to get a velocity vector:

float speed = 8.5f;
direction.X = speed * Math.Cos( angleInRadians );
direction.Y = speed * Math.Sin( angleInRadians );

This will give you a vector of length 8.5, with the direction specified by the angle.

  • \$\begingroup\$ +1 for explaining the unit vector, the actual math involved and listing the library function that does it all automatically, very complete answer! \$\endgroup\$
    – Roy T.
    Commented Jul 10, 2012 at 21:15

If you want a vector pointing a random direction in 2D, generate a random angle between 0 and 2pi, and calculate the vector (cos(angle), sin(angle)). This is already normalized (it has length of one unit), so you can multiply it by the desired speed of the asteroid to get the velocity vector.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .