How to transform a Point
This example demonstrates how to use the Vector3 and Matrix classes to transform a point. A matrix transform can include scaling, rotating, and translating information.
Overview
Transformation matrices are the cornerstone of 3D rendering and how we manipulate content to be drawn in a 3D environment.
Note
For a great primer on Transformation matrices, check out:
Transforming a Point with a Matrix
In MonoGame, matrices are transformed using the Vector3.Transform function with an applied matrix.
Create a Matrix by using CreateRotationY or one of the other Create methods.
Create Rotation Y will create a matrix that is rotated a number of radians around its center.
Pass the point and the Matrix to the Vector3.Transform method.
The following code example is a static function that accept a Vector3 and translate its position (or rotate it) by the amount of radians (NOT degrees) provided.
static Vector3 RotatePointOnYAxis(Vector3 point, float angle)
{
// Create a rotation matrix that represents a rotation of angle radians.
Matrix rotationMatrix = Matrix.CreateRotationY(angle);
// Apply the rotation matrix to the point.
Vector3 rotatedPoint = Vector3.Transform(point, rotationMatrix);
return rotatedPoint;
}
See Also
Matrix Creation Methods
- CreateRotationX
- CreateRotationY
- CreateRotationZ
- CreateScale
- CreateTranslation