MoveComponent.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Util/BitMask.h>
  8. #include <AnKi/Util/Enum.h>
  9. #include <AnKi/Math.h>
  10. namespace anki
  11. {
  12. /// @addtogroup scene
  13. /// @{
  14. /// Interface for movable scene nodes.
  15. class MoveComponent : public SceneComponent
  16. {
  17. ANKI_SCENE_COMPONENT(MoveComponent)
  18. public:
  19. MoveComponent(SceneNode* node);
  20. ~MoveComponent();
  21. /// Get the parent's world transform.
  22. void setIgnoreLocalTransform(Bool ignore)
  23. {
  24. m_ignoreLocalTransform = ignore;
  25. }
  26. /// Ignore parent nodes's transform.
  27. void setIgnoreParentTransform(Bool ignore)
  28. {
  29. m_ignoreParentTransform = ignore;
  30. }
  31. const Transform& getLocalTransform() const
  32. {
  33. return m_ltrf;
  34. }
  35. void setLocalTransform(const Transform& x)
  36. {
  37. m_ltrf = x;
  38. markForUpdate();
  39. }
  40. void setLocalOrigin(const Vec4& x)
  41. {
  42. m_ltrf.setOrigin(x);
  43. markForUpdate();
  44. }
  45. const Vec4& getLocalOrigin() const
  46. {
  47. return m_ltrf.getOrigin();
  48. }
  49. void setLocalRotation(const Mat3x4& x)
  50. {
  51. m_ltrf.setRotation(x);
  52. markForUpdate();
  53. }
  54. const Mat3x4& getLocalRotation() const
  55. {
  56. return m_ltrf.getRotation();
  57. }
  58. void setLocalScale(F32 x)
  59. {
  60. m_ltrf.setScale(x);
  61. markForUpdate();
  62. }
  63. F32 getLocalScale() const
  64. {
  65. return m_ltrf.getScale();
  66. }
  67. const Transform& getWorldTransform() const
  68. {
  69. return m_wtrf;
  70. }
  71. const Transform& getPreviousWorldTransform() const
  72. {
  73. return m_prevWTrf;
  74. }
  75. ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated) override;
  76. /// @name Mess with the local transform
  77. /// @{
  78. void rotateLocalX(F32 angleRad)
  79. {
  80. m_ltrf.getRotation().rotateXAxis(angleRad);
  81. markForUpdate();
  82. }
  83. void rotateLocalY(F32 angleRad)
  84. {
  85. m_ltrf.getRotation().rotateYAxis(angleRad);
  86. markForUpdate();
  87. }
  88. void rotateLocalZ(F32 angleRad)
  89. {
  90. m_ltrf.getRotation().rotateZAxis(angleRad);
  91. markForUpdate();
  92. }
  93. void moveLocalX(F32 distance)
  94. {
  95. Vec3 x_axis = m_ltrf.getRotation().getColumn(0);
  96. m_ltrf.getOrigin() += Vec4(x_axis, 0.0) * distance;
  97. markForUpdate();
  98. }
  99. void moveLocalY(F32 distance)
  100. {
  101. Vec3 y_axis = m_ltrf.getRotation().getColumn(1);
  102. m_ltrf.getOrigin() += Vec4(y_axis, 0.0) * distance;
  103. markForUpdate();
  104. }
  105. void moveLocalZ(F32 distance)
  106. {
  107. Vec3 z_axis = m_ltrf.getRotation().getColumn(2);
  108. m_ltrf.getOrigin() += Vec4(z_axis, 0.0) * distance;
  109. markForUpdate();
  110. }
  111. void scale(F32 s)
  112. {
  113. m_ltrf.getScale() *= s;
  114. markForUpdate();
  115. }
  116. void lookAtPoint(const Vec4& point)
  117. {
  118. m_ltrf.lookAt(point, Vec4(0.0f, 1.0f, 0.0f, 0.0f));
  119. markForUpdate();
  120. }
  121. /// @}
  122. private:
  123. /// The transformation in local space
  124. Transform m_ltrf = Transform::getIdentity();
  125. /// The transformation in world space (local combined with parent's transformation)
  126. Transform m_wtrf = Transform::getIdentity();
  127. /// Keep the previous transformation for checking if it moved
  128. Transform m_prevWTrf = Transform::getIdentity();
  129. Bool m_markedForUpdate : 1;
  130. Bool m_ignoreLocalTransform : 1;
  131. Bool m_ignoreParentTransform : 1;
  132. void markForUpdate()
  133. {
  134. m_markedForUpdate = true;
  135. }
  136. /// Called every frame. It updates the @a m_wtrf if @a shouldUpdateWTrf is true. Then it moves to the children.
  137. Bool updateWorldTransform(SceneNode& node);
  138. };
  139. /// @}
  140. } // end namespace anki