| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Scene/Components/JointComponent.h>
- #include <AnKi/Scene/Components/BodyComponent.h>
- #include <AnKi/Scene/SceneGraph.h>
- #include <AnKi/Physics2/PhysicsWorld.h>
- namespace anki {
- JointComponent::JointComponent(SceneNode* node)
- : SceneComponent(node, kClassType)
- {
- node->setIgnoreParentTransform(true);
- }
- JointComponent::~JointComponent()
- {
- }
- Error JointComponent::update([[maybe_unused]] SceneComponentUpdateInfo& info, Bool& updated)
- {
- SceneNode& node = *info.m_node;
- SceneNode* parent = node.getParent();
- SceneNode* child = node.hasChildren() ? &node.getChild(0) : nullptr;
- BodyComponent* bodyc1 = (parent) ? parent->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
- BodyComponent* bodyc2 = (child) ? child->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
- v2::PhysicsBody* body1 = (bodyc1) ? bodyc1->getPhysicsBody().tryGet() : nullptr;
- v2::PhysicsBody* body2 = (bodyc2) ? bodyc2->getPhysicsBody().tryGet() : nullptr;
- if(!body1 || !body2 || m_type == JointType::kCount)
- {
- m_joint.reset(nullptr);
- return Error::kNone;
- }
- const Bool parentChanged = parent && m_parentNodeUuid != parent->getUuid();
- const Bool childChanged = child && m_childNodeUuid != child->getUuid();
- if(parentChanged || childChanged || node.movedThisFrame())
- {
- // Need to re-create the joint
- updated = true;
- m_parentNodeUuid = parent->getUuid();
- m_childNodeUuid = child->getUuid();
- m_joint.reset(nullptr);
- }
- // Create new joint
- if(!m_joint)
- {
- updated = true;
- switch(m_type)
- {
- case JointType::kPoint:
- m_joint = v2::PhysicsWorld::getSingleton().newPointJoint(body1, body2, node.getWorldTransform().getOrigin().xyz());
- break;
- case JointType::kHinge:
- m_joint = v2::PhysicsWorld::getSingleton().newHingeJoint(body1, body2, node.getWorldTransform());
- break;
- default:
- ANKI_ASSERT(0);
- }
- }
- return Error::kNone;
- }
- } // end namespace anki
|