Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using System;
- namespace Game1
- {
- class Transform
- {
- Vector3 position;
- /// <summary>
- /// Location of the transform in world space.
- /// </summary>
- internal Vector3 Position
- {
- get
- {
- return position;
- }
- set
- {
- position = value;
- Console.WriteLine("Updating matrix due to change in positon.");
- UpdateMatrix();
- }
- }
- Quaternion rotation;
- /// <summary>
- /// The rotation in degrees around each axis.
- /// </summary>
- Vector3 blank;
- Quaternion q;
- internal Quaternion Rotation
- {
- get
- {
- matrix.Decompose(out blank, out q, out blank);
- return q;
- }
- set
- {
- rotation = value;
- Console.WriteLine("Updating matrix due to change in rotation.");
- UpdateMatrix();
- }
- }
- Vector3 scale;
- /// <summary>
- /// The amount the transform is scalled in each axis.
- /// </summary>
- internal Vector3 Scale
- {
- get
- {
- return scale;
- }
- set
- {
- scale = value;
- Console.WriteLine("Updating matrix due to change in scale.");
- UpdateMatrix();
- }
- }
- /// <summary>
- /// The transform's world matrix.
- /// </summary>
- internal Matrix Matrix
- {
- get { return matrix; }
- set { matrix = value; }
- }
- Matrix matrix;
- internal Transform()
- {
- Reset();
- }
- /// <summary>
- /// Creates a new transform at the specified
- /// position.
- /// </summary>
- internal Transform(Vector3 position)
- {
- Reset();
- this.position = position;
- }
- /// <summary>
- /// Creates a new transform at the specified
- /// position with the specified rotation.
- /// </summary>
- internal Transform(Vector3 position, Quaternion rotation)
- {
- Reset();
- this.position = position;
- this.rotation = rotation;
- UpdateMatrix();
- }
- /// <summary>
- /// Creates a new transform at the specified
- /// position with the specified rotation and
- /// scale.
- /// </summary>
- internal Transform(Vector3 position, Quaternion rotation, Vector3 scale)
- {
- Reset();
- this.position = position;
- this.rotation = rotation;
- this.scale = scale;
- UpdateMatrix();
- }
- /// <summary>
- /// Restores the transform to a default state.
- /// </summary>
- internal void Reset()
- {
- position = Vector3.Zero;
- rotation = Quaternion.Identity;
- scale = Vector3.One;
- matrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
- }
- Matrix rotationMatrix;
- /// <summary>
- /// Updates the world matrix with stored position rotation and scale values.
- /// </summary>
- private void UpdateMatrix()
- {
- // Wrap angles so they do not exceed multiple rotations.
- rotation = new Quaternion(MathHelper.WrapAngle(rotation.X), MathHelper.WrapAngle(rotation.Y), MathHelper.WrapAngle(rotation.Z), MathHelper.WrapAngle(rotation.W));
- rotationMatrix = Matrix.CreateFromQuaternion(rotation);
- Console.WriteLine("Desired Rotation Forward: " + rotationMatrix.Forward);
- // I'm not sure why we use backwards here, but it gets us desired result.
- matrix = Matrix.CreateLookAt(position, position + rotationMatrix.Forward, rotationMatrix.Up);
- Console.WriteLine("View Forward: " + matrix.Forward);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement