MoveComponent.h 3.3 KB

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