Vector2 Structure
XNA Game Studio 4.0
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
- 6/17/2011
- AndrewRussell
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;
}
// 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;
}
- 11/21/2010
- crushbrain