JointComponent.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // All rights reserved.
  2. // Code licensed under the BSD License.
  3. // http://www.anki3d.org/LICENSE
  4. #include <AnKi/Scene/Components/JointComponent.h>
  5. #include <AnKi/Scene/Components/BodyComponent.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Physics/PhysicsWorld.h>
  8. namespace anki {
  9. JointComponent::JointComponent(SceneNode* node)
  10. : SceneComponent(node, kClassType)
  11. , m_node(node)
  12. {
  13. node->setIgnoreParentTransform(true);
  14. }
  15. JointComponent::~JointComponent()
  16. {
  17. }
  18. Bool JointComponent::isValid() const
  19. {
  20. SceneNode* parent = m_node->getParent();
  21. SceneNode* child = m_node->hasChildren() ? &m_node->getChild(0) : nullptr;
  22. BodyComponent* bodyc1 = (parent) ? parent->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
  23. BodyComponent* bodyc2 = (child) ? child->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
  24. PhysicsBody* body1 = (bodyc1) ? bodyc1->getPhysicsBody().tryGet() : nullptr;
  25. PhysicsBody* body2 = (bodyc2) ? bodyc2->getPhysicsBody().tryGet() : nullptr;
  26. return body1 && body2 && m_type < JointComponentyType::kCount;
  27. }
  28. void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  29. {
  30. SceneNode& node = *info.m_node;
  31. if(!isValid())
  32. {
  33. m_joint.reset(nullptr);
  34. return;
  35. }
  36. SceneNode* parent = node.getParent();
  37. SceneNode* child = &node.getChild(0);
  38. BodyComponent* bodyc1 = parent->tryGetFirstComponentOfType<BodyComponent>();
  39. BodyComponent* bodyc2 = child->tryGetFirstComponentOfType<BodyComponent>();
  40. PhysicsBody* body1 = bodyc1->getPhysicsBody().tryGet();
  41. PhysicsBody* body2 = bodyc2->getPhysicsBody().tryGet();
  42. const Bool parentChanged = parent && m_parentNodeUuid != parent->getUuid();
  43. const Bool childChanged = child && m_childNodeUuid != child->getUuid();
  44. if(parentChanged || childChanged || node.movedThisFrame())
  45. {
  46. // Need to re-create the joint
  47. updated = true;
  48. m_parentNodeUuid = parent->getUuid();
  49. m_childNodeUuid = child->getUuid();
  50. m_joint.reset(nullptr);
  51. }
  52. // Create new joint
  53. if(!m_joint)
  54. {
  55. updated = true;
  56. switch(m_type)
  57. {
  58. case JointComponentyType::kPoint:
  59. m_joint = PhysicsWorld::getSingleton().newPointJoint(body1, body2, node.getWorldTransform().getOrigin().xyz);
  60. break;
  61. case JointComponentyType::kHinge:
  62. m_joint = PhysicsWorld::getSingleton().newHingeJoint(body1, body2, node.getWorldTransform());
  63. break;
  64. default:
  65. ANKI_ASSERT(0);
  66. }
  67. }
  68. }
  69. } // end namespace anki