using System.Collections.Generic;
using Chernobyl.Creation;
using Chernobyl.Mathematics.Vectors;
using Chernobyl.Measures;
namespace Chernobyl.Mathematics.Movement
{
///
/// A transform that uses matrices to represent the transformations.
///
public class MatrixTransform : Transform
{
///
/// Initializes a new instance of the class
/// that uses for the initial value of the
/// and the default value for the
/// list.
///
public MatrixTransform()
: this(ref Matrix4.Identity)
{ }
///
/// Initializes a new instance of the class
/// the default value for the
/// list.
///
/// The initial value of the
/// .
public MatrixTransform(ref Matrix4 localMatrix)
: this(ref localMatrix, null)
{ }
///
/// Initializes a new instance of the class
/// that uses for the initial value of the
/// .
///
/// The list to use for the
/// list or null if the default
/// should be used.
public MatrixTransform(IList childList)
: this(ref Matrix4.Identity, childList)
{ }
///
/// Initializes a new instance of the class.
///
/// The initial value of the
/// .
/// The list to use for the
/// list or null if the default
/// should be used.
public MatrixTransform(ref Matrix4 localMatrix, IList childList)
: base(childList)
{
_worldMatrix = _localMatrix = localMatrix;
}
///
/// Translates the transform in the X axis by a specified amount.
///
/// The amount to translate in the X.
public override void TranslateX(float x)
{
Translate(x, 0.0f, 0.0f);
}
///
/// Translates the transform in the Y axis by a specified amount.
///
/// The amount to translate in the Y.
public override void TranslateY(float y)
{
Translate(0.0f, y, 0.0f);
}
///
/// Translates the transform in the Z axis by a specified amount.
///
/// The amount to translate in the Z.
public override void TranslateZ(float z)
{
Translate(0.0f, 0.0f, z);
}
///
/// Translates the transform in the X and Y axis by a specified amount.
/// This method is useful for transforms that exist in 2D world.
///
/// The amount to translate in the X.
/// The amount to translate in the Y.
public override void Translate(float x, float y)
{
Translate(x, y, 0.0f);
}
///
/// Translates the transform in the X axis, Y axis
/// and Z axis by specified amounts.
///
/// The amount to translate in the X.
/// The amount to translate in the Y.
/// The amount to translate in the Z.
public override void Translate(float x, float y, float z)
{
Matrix4 translation;
Matrix4.CreateTranslation(x, y, z, out translation);
LocalMatrix *= translation;
}
///
/// Rotates this transform around a vector by a specified amount.
///
/// The vector to rotate around.
/// The amount to rotate around the vector.
public override void Rotate(Vector3 vectorToRotateAround, Radian radian)
{
Matrix4 rotation; Matrix4.Rotate(out rotation, vectorToRotateAround, radian);
LocalMatrix *= rotation;
}
///
/// Rotates this transform about the X axis a specified amount.
///
/// The amount to rotate by.
public override void Pitch(Radian amount)
{
Matrix4 rotation; Matrix4.RotateX(out rotation, amount);
LocalMatrix *= rotation;
}
///
/// Rotates this transform about the Y axis a specified amount.
///
/// The amount to rotate by.
public override void Yaw(Radian amount)
{
Matrix4 rotation; Matrix4.RotateY(out rotation, amount);
LocalMatrix *= rotation;
}
///
/// Rotates this transform about the Z axis a specified amount.
///
/// The amount to rotate by.
public override void Roll(Radian amount)
{
Matrix4 rotation; Matrix4.RotateZ(out rotation, amount);
LocalMatrix *= rotation;
}
///
/// Scales the transform along the X axis.
///
/// The amount to scale in the X axis.
public override void ScaleX(float amount)
{
LocalMatrix *= Matrix4.Scale(amount, 1, 1);
}
///
/// Scales the transform in the Y axis.
///
/// The amount to scale in the Y axis.
public override void ScaleY(float amount)
{
LocalMatrix *= Matrix4.Scale(1, amount, 1);
}
///
/// Scales the transform in the Z axis.
///
/// The amount to scale in the Z axis.
public override void ScaleZ(float amount)
{
LocalMatrix *= Matrix4.Scale(1, 1, amount);
}
///
/// Scales the transform in the X and Y axis. This is useful if the
/// transform exists in a 2D world.
///
/// The amount to scale in the X axis.
/// The amount to scale in the Y axis.
public override void Scale(float x, float y)
{
LocalMatrix *= Matrix4.Scale(x, y, 1);
}
///
/// Scales the transform uniformly over the X, Y, and Z axis.
///
/// The amount to scale in the X axis.
/// The amount to scale in the Y axis.
/// The amount to scale in the Z axis.
public override void Scale(float x, float y, float z)
{
LocalMatrix *= Matrix4.Scale(x, y, z);
}
///
/// Sets the internal local transformation to the matrix passed in.
///
/// The matrix to set the internal local
/// transformation to.
public override void SetTransformation(ref Matrix4 matrix4)
{
LocalMatrix = matrix4;
}
///
/// Gets/sets this transforms local matrix. The local matrix
/// is the matrix that has been "untouched" by the parents,
/// grandparents, etc., of this transform. In other words,
/// it is not relative to any other transform.
///
public override Matrix4 LocalMatrix
{
get
{
return _localMatrix;
}
protected set
{
_localMatrix = value;
IsUpdateNeeded = true;
}
}
///
/// Gets/sets this transform's world matrix. The world matrix
/// is the position and orientation relative to the parent of
/// this transform.
///
public override Matrix4 WorldMatrix
{
get
{
// ensure that the world matrix is up to date.
Update();
return _worldMatrix;
}
protected set { _worldMatrix = value; }
}
///
/// Basic for creating types.
///
public class Factory : DefaultConstructorFactory
{}
///
/// The backing field of the property LocalMatrix.
///
Matrix4 _localMatrix;
///
/// The backing field of the property WorldMatrix.
///
Matrix4 _worldMatrix;
}
}