[1963] | 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 | |
---|
[7983] | 20 | /* |
---|
| 21 | Overview: |
---|
| 22 | |
---|
| 23 | btConeTwistConstraint can be used to simulate ragdoll joints (upper arm, leg etc). |
---|
| 24 | It is a fixed translation, 3 degree-of-freedom (DOF) rotational "joint". |
---|
| 25 | It divides the 3 rotational DOFs into swing (movement within a cone) and twist. |
---|
| 26 | Swing is divided into swing1 and swing2 which can have different limits, giving an elliptical shape. |
---|
| 27 | (Note: the cone's base isn't flat, so this ellipse is "embedded" on the surface of a sphere.) |
---|
| 28 | |
---|
| 29 | In the contraint's frame of reference: |
---|
| 30 | twist is along the x-axis, |
---|
| 31 | and swing 1 and 2 are along the z and y axes respectively. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | |
---|
[1963] | 36 | #ifndef CONETWISTCONSTRAINT_H |
---|
| 37 | #define CONETWISTCONSTRAINT_H |
---|
| 38 | |
---|
[2430] | 39 | #include "LinearMath/btVector3.h" |
---|
[1963] | 40 | #include "btJacobianEntry.h" |
---|
| 41 | #include "btTypedConstraint.h" |
---|
| 42 | |
---|
| 43 | class btRigidBody; |
---|
| 44 | |
---|
[7983] | 45 | enum btConeTwistFlags |
---|
| 46 | { |
---|
| 47 | BT_CONETWIST_FLAGS_LIN_CFM = 1, |
---|
| 48 | BT_CONETWIST_FLAGS_LIN_ERP = 2, |
---|
| 49 | BT_CONETWIST_FLAGS_ANG_CFM = 4 |
---|
| 50 | }; |
---|
[1963] | 51 | |
---|
| 52 | ///btConeTwistConstraint can be used to simulate ragdoll joints (upper arm, leg etc) |
---|
| 53 | class btConeTwistConstraint : public btTypedConstraint |
---|
| 54 | { |
---|
| 55 | #ifdef IN_PARALLELL_SOLVER |
---|
| 56 | public: |
---|
| 57 | #endif |
---|
| 58 | btJacobianEntry m_jac[3]; //3 orthogonal linear constraints |
---|
| 59 | |
---|
| 60 | btTransform m_rbAFrame; |
---|
| 61 | btTransform m_rbBFrame; |
---|
| 62 | |
---|
| 63 | btScalar m_limitSoftness; |
---|
| 64 | btScalar m_biasFactor; |
---|
| 65 | btScalar m_relaxationFactor; |
---|
| 66 | |
---|
[2882] | 67 | btScalar m_damping; |
---|
| 68 | |
---|
[1963] | 69 | btScalar m_swingSpan1; |
---|
| 70 | btScalar m_swingSpan2; |
---|
| 71 | btScalar m_twistSpan; |
---|
| 72 | |
---|
[2882] | 73 | btScalar m_fixThresh; |
---|
| 74 | |
---|
[1963] | 75 | btVector3 m_swingAxis; |
---|
| 76 | btVector3 m_twistAxis; |
---|
| 77 | |
---|
| 78 | btScalar m_kSwing; |
---|
| 79 | btScalar m_kTwist; |
---|
| 80 | |
---|
| 81 | btScalar m_twistLimitSign; |
---|
| 82 | btScalar m_swingCorrection; |
---|
| 83 | btScalar m_twistCorrection; |
---|
| 84 | |
---|
[2882] | 85 | btScalar m_twistAngle; |
---|
| 86 | |
---|
[1963] | 87 | btScalar m_accSwingLimitImpulse; |
---|
| 88 | btScalar m_accTwistLimitImpulse; |
---|
| 89 | |
---|
| 90 | bool m_angularOnly; |
---|
| 91 | bool m_solveTwistLimit; |
---|
| 92 | bool m_solveSwingLimit; |
---|
| 93 | |
---|
[2882] | 94 | bool m_useSolveConstraintObsolete; |
---|
| 95 | |
---|
| 96 | // not yet used... |
---|
| 97 | btScalar m_swingLimitRatio; |
---|
| 98 | btScalar m_twistLimitRatio; |
---|
| 99 | btVector3 m_twistAxisA; |
---|
| 100 | |
---|
| 101 | // motor |
---|
| 102 | bool m_bMotorEnabled; |
---|
| 103 | bool m_bNormalizedMotorStrength; |
---|
| 104 | btQuaternion m_qTarget; |
---|
| 105 | btScalar m_maxMotorImpulse; |
---|
| 106 | btVector3 m_accMotorImpulse; |
---|
[1963] | 107 | |
---|
[7983] | 108 | // parameters |
---|
| 109 | int m_flags; |
---|
| 110 | btScalar m_linCFM; |
---|
| 111 | btScalar m_linERP; |
---|
| 112 | btScalar m_angCFM; |
---|
| 113 | |
---|
| 114 | protected: |
---|
| 115 | |
---|
| 116 | void init(); |
---|
| 117 | |
---|
| 118 | void computeConeLimitInfo(const btQuaternion& qCone, // in |
---|
| 119 | btScalar& swingAngle, btVector3& vSwingAxis, btScalar& swingLimit); // all outs |
---|
| 120 | |
---|
| 121 | void computeTwistLimitInfo(const btQuaternion& qTwist, // in |
---|
| 122 | btScalar& twistAngle, btVector3& vTwistAxis); // all outs |
---|
| 123 | |
---|
| 124 | void adjustSwingAxisToUseEllipseNormal(btVector3& vSwingAxis) const; |
---|
| 125 | |
---|
| 126 | |
---|
[1963] | 127 | public: |
---|
| 128 | |
---|
| 129 | btConeTwistConstraint(btRigidBody& rbA,btRigidBody& rbB,const btTransform& rbAFrame, const btTransform& rbBFrame); |
---|
| 130 | |
---|
| 131 | btConeTwistConstraint(btRigidBody& rbA,const btTransform& rbAFrame); |
---|
| 132 | |
---|
| 133 | virtual void buildJacobian(); |
---|
| 134 | |
---|
[2882] | 135 | virtual void getInfo1 (btConstraintInfo1* info); |
---|
[7983] | 136 | |
---|
| 137 | void getInfo1NonVirtual(btConstraintInfo1* info); |
---|
[2882] | 138 | |
---|
| 139 | virtual void getInfo2 (btConstraintInfo2* info); |
---|
| 140 | |
---|
[7983] | 141 | void getInfo2NonVirtual(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btMatrix3x3& invInertiaWorldA,const btMatrix3x3& invInertiaWorldB); |
---|
[1963] | 142 | |
---|
[7983] | 143 | virtual void solveConstraintObsolete(btRigidBody& bodyA,btRigidBody& bodyB,btScalar timeStep); |
---|
[2882] | 144 | |
---|
[1963] | 145 | void updateRHS(btScalar timeStep); |
---|
| 146 | |
---|
| 147 | const btRigidBody& getRigidBodyA() const |
---|
| 148 | { |
---|
| 149 | return m_rbA; |
---|
| 150 | } |
---|
| 151 | const btRigidBody& getRigidBodyB() const |
---|
| 152 | { |
---|
| 153 | return m_rbB; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void setAngularOnly(bool angularOnly) |
---|
| 157 | { |
---|
| 158 | m_angularOnly = angularOnly; |
---|
| 159 | } |
---|
| 160 | |
---|
[2882] | 161 | void setLimit(int limitIndex,btScalar limitValue) |
---|
[1963] | 162 | { |
---|
[2882] | 163 | switch (limitIndex) |
---|
| 164 | { |
---|
| 165 | case 3: |
---|
| 166 | { |
---|
| 167 | m_twistSpan = limitValue; |
---|
| 168 | break; |
---|
| 169 | } |
---|
| 170 | case 4: |
---|
| 171 | { |
---|
| 172 | m_swingSpan2 = limitValue; |
---|
| 173 | break; |
---|
| 174 | } |
---|
| 175 | case 5: |
---|
| 176 | { |
---|
| 177 | m_swingSpan1 = limitValue; |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | default: |
---|
| 181 | { |
---|
| 182 | } |
---|
| 183 | }; |
---|
| 184 | } |
---|
| 185 | |
---|
[7983] | 186 | // setLimit(), a few notes: |
---|
| 187 | // _softness: |
---|
| 188 | // 0->1, recommend ~0.8->1. |
---|
| 189 | // describes % of limits where movement is free. |
---|
| 190 | // beyond this softness %, the limit is gradually enforced until the "hard" (1.0) limit is reached. |
---|
| 191 | // _biasFactor: |
---|
| 192 | // 0->1?, recommend 0.3 +/-0.3 or so. |
---|
| 193 | // strength with which constraint resists zeroth order (angular, not angular velocity) limit violation. |
---|
| 194 | // __relaxationFactor: |
---|
| 195 | // 0->1, recommend to stay near 1. |
---|
| 196 | // the lower the value, the less the constraint will fight velocities which violate the angular limits. |
---|
| 197 | void setLimit(btScalar _swingSpan1,btScalar _swingSpan2,btScalar _twistSpan, btScalar _softness = 1.f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f) |
---|
[2882] | 198 | { |
---|
[1963] | 199 | m_swingSpan1 = _swingSpan1; |
---|
| 200 | m_swingSpan2 = _swingSpan2; |
---|
| 201 | m_twistSpan = _twistSpan; |
---|
| 202 | |
---|
| 203 | m_limitSoftness = _softness; |
---|
| 204 | m_biasFactor = _biasFactor; |
---|
| 205 | m_relaxationFactor = _relaxationFactor; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | const btTransform& getAFrame() { return m_rbAFrame; }; |
---|
| 209 | const btTransform& getBFrame() { return m_rbBFrame; }; |
---|
| 210 | |
---|
| 211 | inline int getSolveTwistLimit() |
---|
| 212 | { |
---|
| 213 | return m_solveTwistLimit; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | inline int getSolveSwingLimit() |
---|
| 217 | { |
---|
| 218 | return m_solveTwistLimit; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | inline btScalar getTwistLimitSign() |
---|
| 222 | { |
---|
| 223 | return m_twistLimitSign; |
---|
| 224 | } |
---|
| 225 | |
---|
[2882] | 226 | void calcAngleInfo(); |
---|
[7983] | 227 | void calcAngleInfo2(const btTransform& transA, const btTransform& transB,const btMatrix3x3& invInertiaWorldA,const btMatrix3x3& invInertiaWorldB); |
---|
[2882] | 228 | |
---|
| 229 | inline btScalar getSwingSpan1() |
---|
| 230 | { |
---|
| 231 | return m_swingSpan1; |
---|
| 232 | } |
---|
| 233 | inline btScalar getSwingSpan2() |
---|
| 234 | { |
---|
| 235 | return m_swingSpan2; |
---|
| 236 | } |
---|
| 237 | inline btScalar getTwistSpan() |
---|
| 238 | { |
---|
| 239 | return m_twistSpan; |
---|
| 240 | } |
---|
| 241 | inline btScalar getTwistAngle() |
---|
| 242 | { |
---|
| 243 | return m_twistAngle; |
---|
| 244 | } |
---|
| 245 | bool isPastSwingLimit() { return m_solveSwingLimit; } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | void setDamping(btScalar damping) { m_damping = damping; } |
---|
| 249 | |
---|
| 250 | void enableMotor(bool b) { m_bMotorEnabled = b; } |
---|
| 251 | void setMaxMotorImpulse(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; m_bNormalizedMotorStrength = false; } |
---|
| 252 | void setMaxMotorImpulseNormalized(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; m_bNormalizedMotorStrength = true; } |
---|
| 253 | |
---|
| 254 | btScalar getFixThresh() { return m_fixThresh; } |
---|
| 255 | void setFixThresh(btScalar fixThresh) { m_fixThresh = fixThresh; } |
---|
| 256 | |
---|
| 257 | // setMotorTarget: |
---|
| 258 | // q: the desired rotation of bodyA wrt bodyB. |
---|
| 259 | // note: if q violates the joint limits, the internal target is clamped to avoid conflicting impulses (very bad for stability) |
---|
| 260 | // note: don't forget to enableMotor() |
---|
| 261 | void setMotorTarget(const btQuaternion &q); |
---|
| 262 | |
---|
| 263 | // same as above, but q is the desired rotation of frameA wrt frameB in constraint space |
---|
| 264 | void setMotorTargetInConstraintSpace(const btQuaternion &q); |
---|
| 265 | |
---|
| 266 | btVector3 GetPointForAngle(btScalar fAngleInRadians, btScalar fLength) const; |
---|
| 267 | |
---|
[7983] | 268 | ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5). |
---|
| 269 | ///If no axis is provided, it uses the default axis for this constraint. |
---|
| 270 | virtual void setParam(int num, btScalar value, int axis = -1); |
---|
| 271 | ///return the local value of parameter |
---|
| 272 | virtual btScalar getParam(int num, int axis = -1) const; |
---|
[2882] | 273 | |
---|
[7983] | 274 | virtual int calculateSerializeBufferSize() const; |
---|
[2882] | 275 | |
---|
[7983] | 276 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
| 277 | virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const; |
---|
[2882] | 278 | |
---|
[7983] | 279 | }; |
---|
[2882] | 280 | |
---|
[7983] | 281 | ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 |
---|
| 282 | struct btConeTwistConstraintData |
---|
| 283 | { |
---|
| 284 | btTypedConstraintData m_typeConstraintData; |
---|
| 285 | btTransformFloatData m_rbAFrame; |
---|
| 286 | btTransformFloatData m_rbBFrame; |
---|
[2882] | 287 | |
---|
[7983] | 288 | //limits |
---|
| 289 | float m_swingSpan1; |
---|
| 290 | float m_swingSpan2; |
---|
| 291 | float m_twistSpan; |
---|
| 292 | float m_limitSoftness; |
---|
| 293 | float m_biasFactor; |
---|
| 294 | float m_relaxationFactor; |
---|
| 295 | |
---|
| 296 | float m_damping; |
---|
| 297 | |
---|
| 298 | char m_pad[4]; |
---|
| 299 | |
---|
[1963] | 300 | }; |
---|
[7983] | 301 | |
---|
[1963] | 302 | |
---|
[7983] | 303 | |
---|
| 304 | SIMD_FORCE_INLINE int btConeTwistConstraint::calculateSerializeBufferSize() const |
---|
| 305 | { |
---|
| 306 | return sizeof(btConeTwistConstraintData); |
---|
| 307 | |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
| 312 | SIMD_FORCE_INLINE const char* btConeTwistConstraint::serialize(void* dataBuffer, btSerializer* serializer) const |
---|
| 313 | { |
---|
| 314 | btConeTwistConstraintData* cone = (btConeTwistConstraintData*) dataBuffer; |
---|
| 315 | btTypedConstraint::serialize(&cone->m_typeConstraintData,serializer); |
---|
| 316 | |
---|
| 317 | m_rbAFrame.serializeFloat(cone->m_rbAFrame); |
---|
| 318 | m_rbBFrame.serializeFloat(cone->m_rbBFrame); |
---|
| 319 | |
---|
| 320 | cone->m_swingSpan1 = float(m_swingSpan1); |
---|
| 321 | cone->m_swingSpan2 = float(m_swingSpan2); |
---|
| 322 | cone->m_twistSpan = float(m_twistSpan); |
---|
| 323 | cone->m_limitSoftness = float(m_limitSoftness); |
---|
| 324 | cone->m_biasFactor = float(m_biasFactor); |
---|
| 325 | cone->m_relaxationFactor = float(m_relaxationFactor); |
---|
| 326 | cone->m_damping = float(m_damping); |
---|
| 327 | |
---|
| 328 | return "btConeTwistConstraintData"; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | |
---|
[1963] | 332 | #endif //CONETWISTCONSTRAINT_H |
---|