MoveComponent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef ANKI_SCENE_MOVE_COMPONENT_H
  2. #define ANKI_SCENE_MOVE_COMPONENT_H
  3. #include "anki/util/Bitset.h"
  4. #include "anki/Math.h"
  5. #include "anki/core/Timestamp.h"
  6. #include "anki/scene/Common.h"
  7. namespace anki {
  8. /// @addtogroup Scene
  9. /// @{
  10. /// Interface for movable scene nodes
  11. class MoveComponent:
  12. public SceneHierarchicalObject<MoveComponent>,
  13. public Bitset<U8>
  14. {
  15. public:
  16. typedef SceneHierarchicalObject<MoveComponent> Base;
  17. enum MoveComponentFlag
  18. {
  19. MF_NONE = 0,
  20. /// Get the parent's world transform
  21. MF_IGNORE_LOCAL_TRANSFORM = 1 << 1,
  22. /// If dirty then is marked for update
  23. MF_MARKED_FOR_UPDATE = 1 << 3,
  24. };
  25. /// @name Constructors & destructor
  26. /// @{
  27. /// The one and only constructor
  28. /// @param node The scene node to steal it's allocators
  29. /// @param flags The flags
  30. MoveComponent(SceneNode* node, U32 flags = MF_NONE);
  31. ~MoveComponent();
  32. /// @}
  33. /// @name Accessors
  34. /// @{
  35. const Transform& getLocalTransform() const
  36. {
  37. return lTrf;
  38. }
  39. void setLocalTransform(const Transform& x)
  40. {
  41. lTrf = x;
  42. markForUpdate();
  43. }
  44. void setLocalOrigin(const Vec3& x)
  45. {
  46. lTrf.setOrigin(x);
  47. markForUpdate();
  48. }
  49. void setLocalRotation(const Mat3& x)
  50. {
  51. lTrf.setRotation(x);
  52. markForUpdate();
  53. }
  54. void setLocalScale(F32 x)
  55. {
  56. lTrf.setScale(x);
  57. markForUpdate();
  58. }
  59. const Transform& getWorldTransform() const
  60. {
  61. return wTrf;
  62. }
  63. const Transform& getPrevWorldTransform() const
  64. {
  65. return prevWTrf;
  66. }
  67. Timestamp getTimestamp() const
  68. {
  69. return timestamp;
  70. }
  71. /// @}
  72. /// @name Mess with the local transform
  73. /// @{
  74. void rotateLocalX(F32 angDegrees)
  75. {
  76. lTrf.getRotation().rotateXAxis(angDegrees);
  77. markForUpdate();
  78. }
  79. void rotateLocalY(F32 angDegrees)
  80. {
  81. lTrf.getRotation().rotateYAxis(angDegrees);
  82. markForUpdate();
  83. }
  84. void rotateLocalZ(F32 angDegrees)
  85. {
  86. lTrf.getRotation().rotateZAxis(angDegrees);
  87. markForUpdate();
  88. }
  89. void moveLocalX(F32 distance)
  90. {
  91. Vec3 x_axis = lTrf.getRotation().getColumn(0);
  92. lTrf.getOrigin() += x_axis * distance;
  93. markForUpdate();
  94. }
  95. void moveLocalY(F32 distance)
  96. {
  97. Vec3 y_axis = lTrf.getRotation().getColumn(1);
  98. lTrf.getOrigin() += y_axis * distance;
  99. markForUpdate();
  100. }
  101. void moveLocalZ(F32 distance)
  102. {
  103. Vec3 z_axis = lTrf.getRotation().getColumn(2);
  104. lTrf.getOrigin() += z_axis * distance;
  105. markForUpdate();
  106. }
  107. void scale(F32 s)
  108. {
  109. lTrf.getScale() *= s;
  110. markForUpdate();
  111. }
  112. /// @}
  113. /// This is called by the @a update() method only when the object had
  114. /// actually moved. It's overridable
  115. virtual void moveUpdate()
  116. {}
  117. /// Update self and children world transform recursively, if root node.
  118. /// Need to call this at every frame.
  119. /// @note Don't update if child because we start from roots and go to
  120. /// children and we don't want a child to be updated before the
  121. /// parent
  122. void update();
  123. private:
  124. /// The transformation in local space
  125. Transform lTrf = Transform::getIdentity();
  126. /// The transformation in world space (local combined with parent's
  127. /// transformation)
  128. Transform wTrf = Transform::getIdentity();
  129. /// Keep the previous transformation for checking if it moved
  130. Transform prevWTrf = Transform::getIdentity();
  131. /// The frame where it was last moved
  132. Timestamp timestamp = getGlobTimestamp();
  133. /// Called every frame. It updates the @a wTrf if @a shouldUpdateWTrf
  134. /// is true. Then it moves to the children.
  135. void updateWorldTransform();
  136. void markForUpdate()
  137. {
  138. enableBits(MF_MARKED_FOR_UPDATE);
  139. }
  140. };
  141. /// @}
  142. } // end namespace anki
  143. #endif