| 1 | /* | 
|---|
| 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
| 3 | btConeTwistConstraint is Copyright (c) 2007 Starbreeze Studios | 
|---|
| 4 |  | 
|---|
| 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: | 
|---|
| 10 |  | 
|---|
| 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. | 
|---|
| 14 |  | 
|---|
| 15 | Written by: Marcus Hennix | 
|---|
| 16 | */ | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef CONETWISTCONSTRAINT_H | 
|---|
| 21 | #define CONETWISTCONSTRAINT_H | 
|---|
| 22 |  | 
|---|
| 23 | #include "LinearMath/btVector3.h" | 
|---|
| 24 | #include "btJacobianEntry.h" | 
|---|
| 25 | #include "btTypedConstraint.h" | 
|---|
| 26 |  | 
|---|
| 27 | class btRigidBody; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | ///btConeTwistConstraint can be used to simulate ragdoll joints (upper arm, leg etc) | 
|---|
| 31 | class btConeTwistConstraint : public btTypedConstraint | 
|---|
| 32 | { | 
|---|
| 33 | #ifdef IN_PARALLELL_SOLVER | 
|---|
| 34 | public: | 
|---|
| 35 | #endif | 
|---|
| 36 | btJacobianEntry m_jac[3]; //3 orthogonal linear constraints | 
|---|
| 37 |  | 
|---|
| 38 | btTransform m_rbAFrame; | 
|---|
| 39 | btTransform m_rbBFrame; | 
|---|
| 40 |  | 
|---|
| 41 | btScalar        m_limitSoftness; | 
|---|
| 42 | btScalar        m_biasFactor; | 
|---|
| 43 | btScalar        m_relaxationFactor; | 
|---|
| 44 |  | 
|---|
| 45 | btScalar        m_damping; | 
|---|
| 46 |  | 
|---|
| 47 | btScalar        m_swingSpan1; | 
|---|
| 48 | btScalar        m_swingSpan2; | 
|---|
| 49 | btScalar        m_twistSpan; | 
|---|
| 50 |  | 
|---|
| 51 | btScalar        m_fixThresh; | 
|---|
| 52 |  | 
|---|
| 53 | btVector3   m_swingAxis; | 
|---|
| 54 | btVector3       m_twistAxis; | 
|---|
| 55 |  | 
|---|
| 56 | btScalar        m_kSwing; | 
|---|
| 57 | btScalar        m_kTwist; | 
|---|
| 58 |  | 
|---|
| 59 | btScalar        m_twistLimitSign; | 
|---|
| 60 | btScalar        m_swingCorrection; | 
|---|
| 61 | btScalar        m_twistCorrection; | 
|---|
| 62 |  | 
|---|
| 63 | btScalar        m_twistAngle; | 
|---|
| 64 |  | 
|---|
| 65 | btScalar        m_accSwingLimitImpulse; | 
|---|
| 66 | btScalar        m_accTwistLimitImpulse; | 
|---|
| 67 |  | 
|---|
| 68 | bool            m_angularOnly; | 
|---|
| 69 | bool            m_solveTwistLimit; | 
|---|
| 70 | bool            m_solveSwingLimit; | 
|---|
| 71 |  | 
|---|
| 72 | bool    m_useSolveConstraintObsolete; | 
|---|
| 73 |  | 
|---|
| 74 | // not yet used... | 
|---|
| 75 | btScalar        m_swingLimitRatio; | 
|---|
| 76 | btScalar        m_twistLimitRatio; | 
|---|
| 77 | btVector3   m_twistAxisA; | 
|---|
| 78 |  | 
|---|
| 79 | // motor | 
|---|
| 80 | bool             m_bMotorEnabled; | 
|---|
| 81 | bool             m_bNormalizedMotorStrength; | 
|---|
| 82 | btQuaternion m_qTarget; | 
|---|
| 83 | btScalar         m_maxMotorImpulse; | 
|---|
| 84 | btVector3        m_accMotorImpulse; | 
|---|
| 85 |  | 
|---|
| 86 | public: | 
|---|
| 87 |  | 
|---|
| 88 | btConeTwistConstraint(btRigidBody& rbA,btRigidBody& rbB,const btTransform& rbAFrame, const btTransform& rbBFrame); | 
|---|
| 89 |  | 
|---|
| 90 | btConeTwistConstraint(btRigidBody& rbA,const btTransform& rbAFrame); | 
|---|
| 91 |  | 
|---|
| 92 | btConeTwistConstraint(); | 
|---|
| 93 |  | 
|---|
| 94 | virtual void    buildJacobian(); | 
|---|
| 95 |  | 
|---|
| 96 | virtual void getInfo1 (btConstraintInfo1* info); | 
|---|
| 97 |  | 
|---|
| 98 | virtual void getInfo2 (btConstraintInfo2* info); | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | virtual void    solveConstraintObsolete(btSolverBody& bodyA,btSolverBody& bodyB,btScalar        timeStep); | 
|---|
| 102 |  | 
|---|
| 103 | void    updateRHS(btScalar      timeStep); | 
|---|
| 104 |  | 
|---|
| 105 | const btRigidBody& getRigidBodyA() const | 
|---|
| 106 | { | 
|---|
| 107 | return m_rbA; | 
|---|
| 108 | } | 
|---|
| 109 | const btRigidBody& getRigidBodyB() const | 
|---|
| 110 | { | 
|---|
| 111 | return m_rbB; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void    setAngularOnly(bool angularOnly) | 
|---|
| 115 | { | 
|---|
| 116 | m_angularOnly = angularOnly; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void    setLimit(int limitIndex,btScalar limitValue) | 
|---|
| 120 | { | 
|---|
| 121 | switch (limitIndex) | 
|---|
| 122 | { | 
|---|
| 123 | case 3: | 
|---|
| 124 | { | 
|---|
| 125 | m_twistSpan = limitValue; | 
|---|
| 126 | break; | 
|---|
| 127 | } | 
|---|
| 128 | case 4: | 
|---|
| 129 | { | 
|---|
| 130 | m_swingSpan2 = limitValue; | 
|---|
| 131 | break; | 
|---|
| 132 | } | 
|---|
| 133 | case 5: | 
|---|
| 134 | { | 
|---|
| 135 | m_swingSpan1 = limitValue; | 
|---|
| 136 | break; | 
|---|
| 137 | } | 
|---|
| 138 | default: | 
|---|
| 139 | { | 
|---|
| 140 | } | 
|---|
| 141 | }; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | void    setLimit(btScalar _swingSpan1,btScalar _swingSpan2,btScalar _twistSpan,  btScalar _softness = 1.f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f) | 
|---|
| 145 | { | 
|---|
| 146 | m_swingSpan1 = _swingSpan1; | 
|---|
| 147 | m_swingSpan2 = _swingSpan2; | 
|---|
| 148 | m_twistSpan  = _twistSpan; | 
|---|
| 149 |  | 
|---|
| 150 | m_limitSoftness =  _softness; | 
|---|
| 151 | m_biasFactor = _biasFactor; | 
|---|
| 152 | m_relaxationFactor = _relaxationFactor; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | const btTransform& getAFrame() { return m_rbAFrame; }; | 
|---|
| 156 | const btTransform& getBFrame() { return m_rbBFrame; }; | 
|---|
| 157 |  | 
|---|
| 158 | inline int getSolveTwistLimit() | 
|---|
| 159 | { | 
|---|
| 160 | return m_solveTwistLimit; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | inline int getSolveSwingLimit() | 
|---|
| 164 | { | 
|---|
| 165 | return m_solveTwistLimit; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | inline btScalar getTwistLimitSign() | 
|---|
| 169 | { | 
|---|
| 170 | return m_twistLimitSign; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void calcAngleInfo(); | 
|---|
| 174 | void calcAngleInfo2(); | 
|---|
| 175 |  | 
|---|
| 176 | inline btScalar getSwingSpan1() | 
|---|
| 177 | { | 
|---|
| 178 | return m_swingSpan1; | 
|---|
| 179 | } | 
|---|
| 180 | inline btScalar getSwingSpan2() | 
|---|
| 181 | { | 
|---|
| 182 | return m_swingSpan2; | 
|---|
| 183 | } | 
|---|
| 184 | inline btScalar getTwistSpan() | 
|---|
| 185 | { | 
|---|
| 186 | return m_twistSpan; | 
|---|
| 187 | } | 
|---|
| 188 | inline btScalar getTwistAngle() | 
|---|
| 189 | { | 
|---|
| 190 | return m_twistAngle; | 
|---|
| 191 | } | 
|---|
| 192 | bool isPastSwingLimit() { return m_solveSwingLimit; } | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | void setDamping(btScalar damping) { m_damping = damping; } | 
|---|
| 196 |  | 
|---|
| 197 | void enableMotor(bool b) { m_bMotorEnabled = b; } | 
|---|
| 198 | void setMaxMotorImpulse(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; m_bNormalizedMotorStrength = false; } | 
|---|
| 199 | void setMaxMotorImpulseNormalized(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; m_bNormalizedMotorStrength = true; } | 
|---|
| 200 |  | 
|---|
| 201 | btScalar getFixThresh() { return m_fixThresh; } | 
|---|
| 202 | void setFixThresh(btScalar fixThresh) { m_fixThresh = fixThresh; } | 
|---|
| 203 |  | 
|---|
| 204 | // setMotorTarget: | 
|---|
| 205 | // q: the desired rotation of bodyA wrt bodyB. | 
|---|
| 206 | // note: if q violates the joint limits, the internal target is clamped to avoid conflicting impulses (very bad for stability) | 
|---|
| 207 | // note: don't forget to enableMotor() | 
|---|
| 208 | void setMotorTarget(const btQuaternion &q); | 
|---|
| 209 |  | 
|---|
| 210 | // same as above, but q is the desired rotation of frameA wrt frameB in constraint space | 
|---|
| 211 | void setMotorTargetInConstraintSpace(const btQuaternion &q); | 
|---|
| 212 |  | 
|---|
| 213 | btVector3 GetPointForAngle(btScalar fAngleInRadians, btScalar fLength) const; | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 | protected: | 
|---|
| 218 | void init(); | 
|---|
| 219 |  | 
|---|
| 220 | void computeConeLimitInfo(const btQuaternion& qCone, // in | 
|---|
| 221 | btScalar& swingAngle, btVector3& vSwingAxis, btScalar& swingLimit); // all outs | 
|---|
| 222 |  | 
|---|
| 223 | void computeTwistLimitInfo(const btQuaternion& qTwist, // in | 
|---|
| 224 | btScalar& twistAngle, btVector3& vTwistAxis); // all outs | 
|---|
| 225 |  | 
|---|
| 226 | void adjustSwingAxisToUseEllipseNormal(btVector3& vSwingAxis) const; | 
|---|
| 227 | }; | 
|---|
| 228 |  | 
|---|
| 229 | #endif //CONETWISTCONSTRAINT_H | 
|---|