JointComponent.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. {
  12. node->setIgnoreParentTransform(true);
  13. }
  14. JointComponent::~JointComponent()
  15. {
  16. }
  17. void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  18. {
  19. SceneNode& node = *info.m_node;
  20. SceneNode* parent = node.getParent();
  21. SceneNode* child = node.hasChildren() ? &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. if(!body1 || !body2 || m_type == JointType::kCount)
  27. {
  28. m_joint.reset(nullptr);
  29. return;
  30. }
  31. const Bool parentChanged = parent && m_parentNodeUuid != parent->getUuid();
  32. const Bool childChanged = child && m_childNodeUuid != child->getUuid();
  33. if(parentChanged || childChanged || node.movedThisFrame())
  34. {
  35. // Need to re-create the joint
  36. updated = true;
  37. m_parentNodeUuid = parent->getUuid();
  38. m_childNodeUuid = child->getUuid();
  39. m_joint.reset(nullptr);
  40. }
  41. // Create new joint
  42. if(!m_joint)
  43. {
  44. updated = true;
  45. switch(m_type)
  46. {
  47. case JointType::kPoint:
  48. m_joint = PhysicsWorld::getSingleton().newPointJoint(body1, body2, node.getWorldTransform().getOrigin().xyz());
  49. break;
  50. case JointType::kHinge:
  51. m_joint = PhysicsWorld::getSingleton().newHingeJoint(body1, body2, node.getWorldTransform());
  52. break;
  53. default:
  54. ANKI_ASSERT(0);
  55. }
  56. }
  57. }
  58. } // end namespace anki