Advertisement
Guest User

Transform.cs

a guest
Mar 26th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using System;
  3.  
  4. namespace Game1
  5. {
  6.     class Transform
  7.     {
  8.         Vector3 position;
  9.         /// <summary>
  10.         /// Location of the transform in world space.
  11.         /// </summary>
  12.         internal Vector3 Position
  13.         {
  14.             get
  15.             {
  16.                 return position;
  17.             }
  18.             set
  19.             {
  20.                 position = value;
  21.                 Console.WriteLine("Updating matrix due to change in positon.");
  22.                 UpdateMatrix();
  23.             }
  24.         }
  25.  
  26.         Quaternion rotation;
  27.         /// <summary>
  28.         /// The rotation in degrees around each axis.
  29.         /// </summary>
  30.         Vector3 blank;
  31.         Quaternion q;
  32.         internal Quaternion Rotation
  33.         {
  34.             get
  35.             {                
  36.                 matrix.Decompose(out blank, out q, out blank);
  37.                 return q;
  38.             }
  39.             set
  40.             {
  41.                 rotation = value;
  42.                 Console.WriteLine("Updating matrix due to change in rotation.");
  43.                 UpdateMatrix();
  44.             }
  45.         }
  46.  
  47.         Vector3 scale;
  48.         /// <summary>
  49.         /// The amount the transform is scalled in each axis.
  50.         /// </summary>
  51.         internal Vector3 Scale
  52.         {
  53.             get
  54.             {
  55.                 return scale;
  56.             }
  57.             set
  58.             {
  59.                 scale = value;
  60.                 Console.WriteLine("Updating matrix due to change in scale.");
  61.                 UpdateMatrix();
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// The transform's world matrix.
  67.         /// </summary>
  68.         internal Matrix Matrix
  69.         {
  70.             get { return matrix; }
  71.             set { matrix = value; }
  72.         }
  73.         Matrix matrix;
  74.  
  75.         internal Transform()
  76.         {
  77.             Reset();
  78.         }
  79.         /// <summary>
  80.         /// Creates a new transform at the specified
  81.         /// position.
  82.         /// </summary>
  83.         internal Transform(Vector3 position)
  84.         {
  85.             Reset();
  86.             this.position = position;
  87.         }
  88.         /// <summary>
  89.         /// Creates a new transform at the specified
  90.         /// position with the specified rotation.
  91.         /// </summary>
  92.         internal Transform(Vector3 position, Quaternion rotation)
  93.         {
  94.             Reset();
  95.             this.position = position;
  96.             this.rotation = rotation;
  97.             UpdateMatrix();
  98.         }
  99.         /// <summary>
  100.         /// Creates a new transform at the specified
  101.         /// position with the specified rotation and
  102.         /// scale.
  103.         /// </summary>
  104.         internal Transform(Vector3 position, Quaternion rotation, Vector3 scale)
  105.         {
  106.             Reset();
  107.             this.position = position;
  108.             this.rotation = rotation;
  109.             this.scale = scale;
  110.             UpdateMatrix();
  111.         }
  112.         /// <summary>
  113.         /// Restores the transform to a default state.
  114.         /// </summary>
  115.         internal void Reset()
  116.         {
  117.             position = Vector3.Zero;
  118.             rotation = Quaternion.Identity;
  119.             scale = Vector3.One;
  120.             matrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
  121.         }
  122.         Matrix rotationMatrix;
  123.         /// <summary>
  124.         /// Updates the world matrix with stored position rotation and scale values.
  125.         /// </summary>
  126.         private void UpdateMatrix()
  127.         {
  128.             // Wrap angles so they do not exceed multiple rotations.
  129.             rotation = new Quaternion(MathHelper.WrapAngle(rotation.X), MathHelper.WrapAngle(rotation.Y), MathHelper.WrapAngle(rotation.Z), MathHelper.WrapAngle(rotation.W));
  130.             rotationMatrix = Matrix.CreateFromQuaternion(rotation);
  131.             Console.WriteLine("Desired Rotation Forward: " + rotationMatrix.Forward);
  132.             // I'm not sure why we use backwards here, but it gets us desired result.
  133.             matrix = Matrix.CreateLookAt(position, position + rotationMatrix.Forward, rotationMatrix.Up);
  134.             Console.WriteLine("View Forward: " + matrix.Forward);
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement