1 out of 1 rated this helpful - Rate this topic

Vector2 Structure

Defines a vector with two components.

Namespace: Microsoft.Xna.Framework
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

[TypeConverterAttribute("typeof(Microsoft.Xna.Framework.Design.Vector2Converter)")]
[SerializableAttribute]
public struct Vector2 : IEquatable<Vector2>
Xbox 360, Windows 7, Windows Vista, Windows XP, Windows Phone 7
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Re: Extension Methods
...or you could just rotate using Matrix.CreateRotationZ()
Rotate extension method
I think the above commenter has a bug in their rotation method. Here's mine: $0$0 $0 $0 $0$0 $0
public static Vector2 Rotate(this Vector2 v, float angle)
{
float cos = (float)Math.Cos(angle);
float sin = (float)Math.Sin(angle);
return new Vector2(v.X * cos - v.Y * sin, v.X * sin + v.Y * cos);
}
$0
$0$0 $0
Extension Methods: Projection and Rotation
It may be useful to add these methods to the Vector2 structure using extension methods. Read more about extension methods here: http://msdn.microsoft.com/en-us/library/bb383977.aspx

// Rotates your vector2 around another one by the specified number of radians.
public static Vector2 RotateAround(this Vector2 source, Vector2 target, float radians)
 {
 Vector2 fromAround = source - target;
 float postRotationX = (float)(fromAround.X * Math.Cos(radians) - fromAround.Y * Math.Sin(radians));
 float postRotationY = (float)(fromAround.X * Math.Sin(radians) - fromAround.Y * Math.Cos(radians));
 return target + new Vector2(postRotationX, postRotationY);
}

// Projects your vector2 onto another specified vector2.
public static Vector2 ProjectOnto(thisVector2 source, Vector2 target)
 {
 return (Vector2.Dot(source, target) / target.LengthSquared()) * target;
 }