MoveComponent.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <AnKi/Scene/Components/MoveComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. namespace anki {
  8. ANKI_SCENE_COMPONENT_STATICS(MoveComponent)
  9. MoveComponent::MoveComponent(SceneNode* node)
  10. : SceneComponent(node, getStaticClassId())
  11. , m_ignoreLocalTransform(false)
  12. , m_ignoreParentTransform(false)
  13. {
  14. markForUpdate();
  15. }
  16. MoveComponent::~MoveComponent()
  17. {
  18. }
  19. Error MoveComponent::update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated)
  20. {
  21. updated = updateWorldTransform(node);
  22. return Error::NONE;
  23. }
  24. Bool MoveComponent::updateWorldTransform(SceneNode& node)
  25. {
  26. m_prevWTrf = m_wtrf;
  27. const Bool dirty = m_markedForUpdate;
  28. // If dirty then update world transform
  29. if(dirty)
  30. {
  31. const SceneNode* parent = node.getParent();
  32. if(parent)
  33. {
  34. const MoveComponent* parentMove = parent->tryGetFirstComponentOfType<MoveComponent>();
  35. if(parentMove == nullptr)
  36. {
  37. // Parent not movable
  38. m_wtrf = m_ltrf;
  39. }
  40. else if(m_ignoreParentTransform)
  41. {
  42. m_wtrf = m_ltrf;
  43. }
  44. else if(m_ignoreLocalTransform)
  45. {
  46. m_wtrf = parentMove->getWorldTransform();
  47. }
  48. else
  49. {
  50. m_wtrf = parentMove->getWorldTransform().combineTransformations(m_ltrf);
  51. }
  52. }
  53. else
  54. {
  55. // No parent
  56. m_wtrf = m_ltrf;
  57. }
  58. // Now it's a good time to cleanse parent
  59. m_markedForUpdate = false;
  60. }
  61. // If this is dirty then make children dirty as well. Don't walk the whole tree because you will re-walk it later
  62. if(dirty)
  63. {
  64. const Error err = node.visitChildrenMaxDepth(1, [](SceneNode& childNode) -> Error {
  65. childNode.iterateComponentsOfType<MoveComponent>([](MoveComponent& mov) {
  66. mov.markForUpdate();
  67. });
  68. return Error::NONE;
  69. });
  70. (void)err;
  71. }
  72. return dirty;
  73. }
  74. } // end namespace anki