BsTransform.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /** Sets the local position of the object. */
  22. void setPosition(const Vector3& position) { mPosition = position; }
  23. /** Gets the local position of the object. */
  24. const Vector3& getPosition() const { return mPosition; }
  25. /** Shorthand for getPosition(). */
  26. const Vector3& pos() const { return mPosition; }
  27. /** Sets the local rotation of the object. */
  28. void setRotation(const Quaternion& rotation) { mRotation = rotation; }
  29. /** Gets the local rotation of the object. */
  30. const Quaternion& getRotation() const { return mRotation; }
  31. /** Shorthand for getRotation(). */
  32. const Quaternion& rot() const { return mRotation; }
  33. /** Sets the local scale of the object. */
  34. void setScale(const Vector3& scale) { mScale = scale; }
  35. /** Gets the local scale of the object. */
  36. const Vector3& getScale() const { return mScale; }
  37. /** Shorthand for getScale(). */
  38. const Vector3& scl() const { return mScale; }
  39. /**
  40. * Converts the provided world position to a space relative to the provided parent, as sets it as the current
  41. * transform's position.
  42. */
  43. void setWorldPosition(const Vector3& position, const Transform& parent);
  44. /**
  45. * Converts the provided world rotation to a space relative to the provided parent, as sets it as the current
  46. * transform's rotation.
  47. */
  48. void setWorldRotation(const Quaternion& rotation, const Transform& parent);
  49. /**
  50. * Converts the provided world scale to a space relative to the provided parent, as sets it as the current
  51. * transform's scale.
  52. */
  53. void setWorldScale(const Vector3& scale, const Transform& parent);
  54. /** Builds the transform matrix from current properties. */
  55. Matrix4 getMatrix() const;
  56. /** Builds the inverse transform matrix from current properties. */
  57. Matrix4 getInvMatrix() const;
  58. /**
  59. * Makes the current transform relative to the provided transform. In another words, converts from a world
  60. * coordinate system to one local to the provided transform.
  61. */
  62. void makeLocal(const Transform& parent);
  63. /**
  64. * Makes the current transform absolute. In another words, converts from a local coordinate system relative to
  65. * the provided transform, to a world coordinate system.
  66. */
  67. void makeWorld(const Transform& parent);
  68. /**
  69. * Orients the object so it is looking at the provided @p location (world space) where @p up is used for
  70. * determining the location of the object's Y axis.
  71. */
  72. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  73. /** Moves the object's position by the vector offset provided along world axes. */
  74. void move(const Vector3& vec);
  75. /** Moves the object's position by the vector offset provided along it's own axes (relative to orientation). */
  76. void moveRelative(const Vector3& vec);
  77. /**
  78. * Gets the Z (forward) axis of the object.
  79. *
  80. * @return Forward axis of the object.
  81. */
  82. Vector3 getForward() const { return getRotation().rotate(-Vector3::UNIT_Z); }
  83. /**
  84. * Gets the Y (up) axis of the object.
  85. *
  86. * @return Up axis of the object.
  87. */
  88. Vector3 getUp() const { return getRotation().rotate(Vector3::UNIT_Y); }
  89. /**
  90. * Gets the X (right) axis of the object.
  91. *
  92. * @return Right axis of the object.
  93. */
  94. Vector3 getRight() const { return getRotation().rotate(Vector3::UNIT_X); }
  95. /**
  96. * Rotates the game object so it's forward axis faces the provided direction.
  97. *
  98. * @param[in] forwardDir The forward direction to face.
  99. *
  100. * @note Local forward axis is considered to be negative Z.
  101. */
  102. void setForward(const Vector3& forwardDir);
  103. /** Rotate the object around an arbitrary axis. */
  104. void rotate(const Vector3& axis, const Radian& angle);
  105. /** Rotate the object around an arbitrary axis using a Quaternion. */
  106. void rotate(const Quaternion& q);
  107. /**
  108. * Rotates around local Z axis.
  109. *
  110. * @param[in] angle Angle to rotate by.
  111. */
  112. void roll(const Radian& angle);
  113. /**
  114. * Rotates around Y axis.
  115. *
  116. * @param[in] angle Angle to rotate by.
  117. */
  118. void yaw(const Radian& angle);
  119. /**
  120. * Rotates around X axis
  121. *
  122. * @param[in] angle Angle to rotate by.
  123. */
  124. void pitch(const Radian& angle);
  125. private:
  126. Vector3 mPosition;
  127. Quaternion mRotation;
  128. Vector3 mScale;
  129. /************************************************************************/
  130. /* RTTI */
  131. /************************************************************************/
  132. public:
  133. friend class TransformRTTI;
  134. static RTTITypeBase* getRTTIStatic();
  135. RTTITypeBase* getRTTI() const override;
  136. };
  137. /** @} */
  138. }