2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2018 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 ///This file was written by Erwin Coumans
18 #include "btMultiBodySphericalJointMotor.h"
19 #include "btMultiBody.h"
20 #include "btMultiBodyLinkCollider.h"
21 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
22 #include "LinearMath/btTransformUtil.h"
23 #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
25 btMultiBodySphericalJointMotor::btMultiBodySphericalJointMotor(btMultiBody* body, int link, btScalar maxMotorImpulse)
26 : btMultiBodyConstraint(body, body, link, body->getLink(link).m_parent, 3, true, MULTIBODY_CONSTRAINT_SPHERICAL_MOTOR),
27 m_desiredVelocity(0, 0, 0),
28 m_desiredPosition(0,0,0,1),
29 m_use_multi_dof_params(false),
33 m_rhsClamp(SIMD_INFINITY),
34 m_maxAppliedImpulseMultiDof(maxMotorImpulse, maxMotorImpulse, maxMotorImpulse),
35 m_damping(1.0, 1.0, 1.0)
38 m_maxAppliedImpulse = maxMotorImpulse;
42 void btMultiBodySphericalJointMotor::finalizeMultiDof()
44 allocateJacobiansMultiDof();
45 // note: we rely on the fact that data.m_jacobians are
46 // always initialized to zero by the Constraint ctor
48 unsigned int offset = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
50 // row 0: the lower bound
51 // row 0: the lower bound
52 jacobianA(0)[offset] = 1;
54 m_numDofsFinalized = m_jacSizeBoth;
58 btMultiBodySphericalJointMotor::~btMultiBodySphericalJointMotor()
62 int btMultiBodySphericalJointMotor::getIslandIdA() const
64 if (this->m_linkA < 0)
66 btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
68 return col->getIslandTag();
72 if (m_bodyA->getLink(m_linkA).m_collider)
74 return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
80 int btMultiBodySphericalJointMotor::getIslandIdB() const
84 btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
86 return col->getIslandTag();
90 if (m_bodyB->getLink(m_linkB).m_collider)
92 return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
98 void btMultiBodySphericalJointMotor::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
99 btMultiBodyJacobianData& data,
100 const btContactSolverInfo& infoGlobal)
102 // only positions need to be updated -- data.m_jacobians and force
103 // directions were set in the ctor and never change.
105 if (m_numDofsFinalized != m_jacSizeBoth)
111 if (m_numDofsFinalized != m_jacSizeBoth)
115 if (m_maxAppliedImpulse == 0.f)
118 const btScalar posError = 0;
119 const btVector3 dummy(0, 0, 0);
122 btVector3 axis[3] = { btVector3(1, 0, 0), btVector3(0, 1, 0), btVector3(0, 0, 1) };
124 btQuaternion desiredQuat = m_desiredPosition;
125 btQuaternion currentQuat(m_bodyA->getJointPosMultiDof(m_linkA)[0],
126 m_bodyA->getJointPosMultiDof(m_linkA)[1],
127 m_bodyA->getJointPosMultiDof(m_linkA)[2],
128 m_bodyA->getJointPosMultiDof(m_linkA)[3]);
130 btQuaternion relRot = currentQuat.inverse() * desiredQuat;
132 btGeneric6DofSpring2Constraint::matrixToEulerXYZ(btMatrix3x3(relRot), angleDiff);
136 for (int row = 0; row < getNumRows(); row++)
138 btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
142 btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
143 btScalar desiredVelocity = this->m_desiredVelocity[row];
145 double kd = m_use_multi_dof_params ? m_kd[row % 3] : m_kd[0];
146 btScalar velocityError = (desiredVelocity - currentVelocity) * kd;
148 btMatrix3x3 frameAworld;
149 frameAworld.setIdentity();
150 frameAworld = m_bodyA->localFrameToWorld(m_linkA, frameAworld);
151 btScalar posError = 0;
153 btAssert(m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::eSpherical);
154 switch (m_bodyA->getLink(m_linkA).m_jointType)
156 case btMultibodyLink::eSpherical:
158 btVector3 constraintNormalAng = frameAworld.getColumn(row % 3);
159 double kp = m_use_multi_dof_params ? m_kp[row % 3] : m_kp[0];
160 posError = kp*angleDiff[row % 3];
161 double max_applied_impulse = m_use_multi_dof_params ? m_maxAppliedImpulseMultiDof[row % 3] : m_maxAppliedImpulse;
162 fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
163 btVector3(0,0,0), dummy, dummy,
166 -max_applied_impulse, max_applied_impulse, true,
169 constraintRow.m_orgConstraint = this;
170 constraintRow.m_orgDofIndex = row;