MoveComponent.cpp 1.9 KB

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