BsTransform.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Math/BsMatrix4.h"
  6. #include "Math/BsVector3.h"
  7. #include "Math/BsQuaternion.h"
  8. #include "Reflection/BsRTTIType.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Scene
  12. * @{
  13. */
  14. /**
  15. * Contains information about 3D object's position, rotation and scale, and provides methods to manipulate it.
  16. */
  17. class BS_CORE_EXPORT Transform : public IReflectable
  18. {
  19. public:
  20. Transform();
  21. Transform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  22. /** Sets the local position of the object. */
  23. void setPosition(const Vector3& position) { mPosition = position; }
  24. /** Gets the local position of the object. */
  25. const Vector3& getPosition() const { return mPosition; }
  26. /** Shorthand for getPosition(). */
  27. const Vector3& pos() const { return mPosition; }
  28. /** Sets the local rotation of the object. */
  29. void setRotation(const Quaternion& rotation) { mRotation = rotation; }
  30. /** Gets the local rotation of the object. */
  31. const Quaternion& getRotation() const { return mRotation; }
  32. /** Shorthand for getRotation(). */
  33. const Quaternion& rot() const { return mRotation; }
  34. /** Sets the local scale of the object. */
  35. void setScale(const Vector3& scale) { mScale = scale; }
  36. /** Gets the local scale of the object. */
  37. const Vector3& getScale() const { return mScale; }
  38. /** Shorthand for getScale(). */
  39. const Vector3& scl() const { return mScale; }
  40. /**
  41. * Converts the provided world position to a space relative to the provided parent, and sets it as the current
  42. * transform's position.
  43. */
  44. void setWorldPosition(const Vector3& position, const Transform& parent);
  45. /**
  46. * Converts the provided world rotation to a space relative to the provided parent, and sets it as the current
  47. * transform's rotation.
  48. */
  49. void setWorldRotation(const Quaternion& rotation, const Transform& parent);
  50. /**
  51. * Converts the provided world scale to a space relative to the provided parent, and sets it as the current
  52. * transform's scale.
  53. */
  54. void setWorldScale(const Vector3& scale, const Transform& parent);
  55. /** Builds the transform matrix from current translation, rotation and scale properties. */
  56. Matrix4 getMatrix() const;
  57. /** Builds the inverse transform matrix from current translation, rotation and scale properties. */
  58. Matrix4 getInvMatrix() const;
  59. /**
  60. * Makes the current transform relative to the provided transform. In another words, converts from a world
  61. * coordinate system to one local to the provided transform.
  62. */
  63. void makeLocal(const Transform& parent);
  64. /**
  65. * Makes the current transform absolute. In another words, converts from a local coordinate system relative to
  66. * the provided transform, to a world coordinate system.
  67. */
  68. void makeWorld(const Transform& parent);
  69. /**
  70. * Orients the object so it is looking at the provided @p location (world space) where @p up is used for
  71. * determining the location of the object's Y axis.
  72. */
  73. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  74. /** Moves the object's position by the vector offset provided along world axes. */
  75. void move(const Vector3& vec);
  76. /** Moves the object's position by the vector offset provided along it's own axes (relative to orientation). */
  77. void moveRelative(const Vector3& vec);
  78. /**
  79. * Gets the Z (forward) axis of the object.
  80. *
  81. * @return Forward axis of the object.
  82. */
  83. Vector3 getForward() const { return getRotation().rotate(-Vector3::UNIT_Z); }
  84. /**
  85. * Gets the Y (up) axis of the object.
  86. *
  87. * @return Up axis of the object.
  88. */
  89. Vector3 getUp() const { return getRotation().rotate(Vector3::UNIT_Y); }
  90. /**
  91. * Gets the X (right) axis of the object.
  92. *
  93. * @return Right axis of the object.
  94. */
  95. Vector3 getRight() const { return getRotation().rotate(Vector3::UNIT_X); }
  96. /**
  97. * Rotates the game object so it's forward axis faces the provided direction.
  98. *
  99. * @param[in] forwardDir The forward direction to face.
  100. *
  101. * @note Local forward axis is considered to be negative Z.
  102. */
  103. void setForward(const Vector3& forwardDir);
  104. /** Rotate the object around an arbitrary axis. */
  105. void rotate(const Vector3& axis, const Radian& angle);
  106. /** Rotate the object around an arbitrary axis using a Quaternion. */
  107. void rotate(const Quaternion& q);
  108. /**
  109. * Rotates around local Z axis.
  110. *
  111. * @param[in] angle Angle to rotate by.
  112. */
  113. void roll(const Radian& angle);
  114. /**
  115. * Rotates around Y axis.
  116. *
  117. * @param[in] angle Angle to rotate by.
  118. */
  119. void yaw(const Radian& angle);
  120. /**
  121. * Rotates around X axis
  122. *
  123. * @param[in] angle Angle to rotate by.
  124. */
  125. void pitch(const Radian& angle);
  126. private:
  127. Vector3 mPosition;
  128. Quaternion mRotation;
  129. Vector3 mScale;
  130. /************************************************************************/
  131. /* RTTI */
  132. /************************************************************************/
  133. public:
  134. friend class TransformRTTI;
  135. static RTTITypeBase* getRTTIStatic();
  136. RTTITypeBase* getRTTI() const override;
  137. };
  138. /** @} */
  139. }