Advertisement
Guest User

Transform.h

a guest
Apr 6th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. //NOTE:
  2. /*
  3.     Basically Unity's style.
  4.     World members mutable  to allow change in const function
  5. */
  6.  
  7. #pragma once
  8. #include "Math/Matrix.h"
  9. #include "Math/Vector.h"
  10. #include "Math/Quaternion.h"
  11. #include "Core/Types.h"
  12.  
  13. enum class Space
  14. {
  15.     Self,
  16.     World
  17. };
  18.  
  19. class Transform
  20. {
  21. private:
  22.     Matrix4 m_LocalMatrix       = Matrix4::Identity();
  23.     Vector3 m_LocalPosition     = Vector3(0,0,0);
  24.     Quaternion m_LocalRotation  = Quaternion();
  25.     Vector3 m_LocalScale        = Vector3(1,1,1);
  26.  
  27.     mutable Matrix4 m_World             = Matrix4::Identity();
  28.     mutable Vector3 m_WorldPosition     = Vector3(0, 0, 0);
  29.     mutable Quaternion m_WorldRotation  = Quaternion();
  30.     mutable Vector3 m_WorldScale        = Vector3(1, 1, 1);
  31.     mutable Vector3 m_Euler             = Vector3::Zero;
  32.     mutable bool m_HashChanged          = false;
  33.     bool m_UniformScale                 = true;
  34.  
  35.     Transform* m_Parent = nullptr;
  36.     std::vector<Transform*> m_Children;
  37.  
  38.     //--Propertise--
  39. public:
  40.  
  41.     //-------------World-------------
  42.  
  43.     // Pos
  44.     Vector3 Position()const;
  45.     void SetPosition(const Vector3& worldPosition);
  46.  
  47.     // Rot
  48.     Quaternion Rotation()const;
  49.     void SetRotation(const Quaternion& worldRotation);
  50.     void SetRotation(const Vector3& worldRotation);
  51.  
  52.     // Scale
  53.     Vector3 Scale()const;
  54.     void SetScale(const Vector3& scale);
  55.  
  56.     //-------------Directions-------------
  57.  
  58.     Vector3 Right()const;
  59.     Vector3 Up()const;
  60.     Vector3 Forward()const;
  61.     Matrix4 World()const;
  62.     Matrix4 WorldToLocalMatrix()const; // Its just the inverse
  63.  
  64.     //-------------Local-------------
  65.  
  66.     // Pos
  67.     Vector3 LocalPosition()const;
  68.     void SetLocalPosition(const Vector3& position);
  69.  
  70.     // Rotation
  71.     Quaternion LocalRotation()const;
  72.     void SetLocalRotation(const Quaternion& quat);
  73.     void SetLocalRotation(const Vector3& euler);
  74.  
  75.     // Scale
  76.     Vector3 LocalScale()const;
  77.     void SetLocalScale(const Vector3& scale);
  78.  
  79.     //-------------Hierarchy-------------
  80.  
  81.     Transform* Parent()const;
  82.     void SetParent(Transform* parent);
  83.     Transform* GetChild(Uint32 index);
  84.     void DetatchChildren();
  85.     Uint32 ChildCount();
  86.  
  87.     //-------------Methods-------------
  88. public:
  89.     void Translate(const Vector3& translation, Space relative = Space::Self);
  90.     void Rotate(const Quaternion& quat, Space relative = Space::Self);
  91.     void Rotate(const Vector3& euler, Space relative = Space::Self);
  92.     void LookAt(const Transform target, Vector3 worldUp = Vector3::Up);
  93.     void OnGui();
  94.  
  95. private:
  96.     void HasChanged()const;
  97.     void UpdateTransform()const; // Updates the world matrix stuff.
  98. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement