[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 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 | /* |
---|
| 16 | 2007-09-09 |
---|
| 17 | Refactored by Francisco Le?n |
---|
| 18 | email: projectileman@yahoo.com |
---|
| 19 | http://gimpact.sf.net |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "btGeneric6DofConstraint.h" |
---|
| 23 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 24 | #include "LinearMath/btTransformUtil.h" |
---|
| 25 | #include <new> |
---|
| 26 | |
---|
| 27 | |
---|
[2882] | 28 | #define D6_USE_OBSOLETE_METHOD false |
---|
| 29 | //----------------------------------------------------------------------------- |
---|
| 30 | |
---|
| 31 | btGeneric6DofConstraint::btGeneric6DofConstraint() |
---|
| 32 | :btTypedConstraint(D6_CONSTRAINT_TYPE), |
---|
| 33 | m_useLinearReferenceFrameA(true), |
---|
| 34 | m_useSolveConstraintObsolete(D6_USE_OBSOLETE_METHOD) |
---|
| 35 | { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | //----------------------------------------------------------------------------- |
---|
| 39 | |
---|
| 40 | btGeneric6DofConstraint::btGeneric6DofConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, bool useLinearReferenceFrameA) |
---|
| 41 | : btTypedConstraint(D6_CONSTRAINT_TYPE, rbA, rbB) |
---|
| 42 | , m_frameInA(frameInA) |
---|
| 43 | , m_frameInB(frameInB), |
---|
| 44 | m_useLinearReferenceFrameA(useLinearReferenceFrameA), |
---|
| 45 | m_useSolveConstraintObsolete(D6_USE_OBSOLETE_METHOD) |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | } |
---|
| 49 | //----------------------------------------------------------------------------- |
---|
| 50 | |
---|
| 51 | |
---|
[1963] | 52 | #define GENERIC_D6_DISABLE_WARMSTARTING 1 |
---|
| 53 | |
---|
[2882] | 54 | //----------------------------------------------------------------------------- |
---|
| 55 | |
---|
[1963] | 56 | btScalar btGetMatrixElem(const btMatrix3x3& mat, int index); |
---|
| 57 | btScalar btGetMatrixElem(const btMatrix3x3& mat, int index) |
---|
| 58 | { |
---|
| 59 | int i = index%3; |
---|
| 60 | int j = index/3; |
---|
| 61 | return mat[i][j]; |
---|
| 62 | } |
---|
| 63 | |
---|
[2882] | 64 | //----------------------------------------------------------------------------- |
---|
| 65 | |
---|
[1963] | 66 | ///MatrixToEulerXYZ from http://www.geometrictools.com/LibFoundation/Mathematics/Wm4Matrix3.inl.html |
---|
| 67 | bool matrixToEulerXYZ(const btMatrix3x3& mat,btVector3& xyz); |
---|
| 68 | bool matrixToEulerXYZ(const btMatrix3x3& mat,btVector3& xyz) |
---|
| 69 | { |
---|
[2882] | 70 | // // rot = cy*cz -cy*sz sy |
---|
| 71 | // // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx |
---|
| 72 | // // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy |
---|
| 73 | // |
---|
[1963] | 74 | |
---|
[2882] | 75 | btScalar fi = btGetMatrixElem(mat,2); |
---|
| 76 | if (fi < btScalar(1.0f)) |
---|
| 77 | { |
---|
| 78 | if (fi > btScalar(-1.0f)) |
---|
[1963] | 79 | { |
---|
[2882] | 80 | xyz[0] = btAtan2(-btGetMatrixElem(mat,5),btGetMatrixElem(mat,8)); |
---|
| 81 | xyz[1] = btAsin(btGetMatrixElem(mat,2)); |
---|
| 82 | xyz[2] = btAtan2(-btGetMatrixElem(mat,1),btGetMatrixElem(mat,0)); |
---|
| 83 | return true; |
---|
[1963] | 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
[2882] | 87 | // WARNING. Not unique. XA - ZA = -atan2(r10,r11) |
---|
| 88 | xyz[0] = -btAtan2(btGetMatrixElem(mat,3),btGetMatrixElem(mat,4)); |
---|
| 89 | xyz[1] = -SIMD_HALF_PI; |
---|
| 90 | xyz[2] = btScalar(0.0); |
---|
| 91 | return false; |
---|
[1963] | 92 | } |
---|
[2882] | 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | // WARNING. Not unique. XAngle + ZAngle = atan2(r10,r11) |
---|
| 97 | xyz[0] = btAtan2(btGetMatrixElem(mat,3),btGetMatrixElem(mat,4)); |
---|
| 98 | xyz[1] = SIMD_HALF_PI; |
---|
| 99 | xyz[2] = 0.0; |
---|
| 100 | } |
---|
[1963] | 101 | return false; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | //////////////////////////// btRotationalLimitMotor //////////////////////////////////// |
---|
| 105 | |
---|
| 106 | int btRotationalLimitMotor::testLimitValue(btScalar test_value) |
---|
| 107 | { |
---|
| 108 | if(m_loLimit>m_hiLimit) |
---|
| 109 | { |
---|
| 110 | m_currentLimit = 0;//Free from violation |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if (test_value < m_loLimit) |
---|
| 115 | { |
---|
| 116 | m_currentLimit = 1;//low limit violation |
---|
| 117 | m_currentLimitError = test_value - m_loLimit; |
---|
| 118 | return 1; |
---|
| 119 | } |
---|
| 120 | else if (test_value> m_hiLimit) |
---|
| 121 | { |
---|
| 122 | m_currentLimit = 2;//High limit violation |
---|
| 123 | m_currentLimitError = test_value - m_hiLimit; |
---|
| 124 | return 2; |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | m_currentLimit = 0;//Free from violation |
---|
| 128 | return 0; |
---|
[2882] | 129 | |
---|
[1963] | 130 | } |
---|
| 131 | |
---|
[2882] | 132 | //----------------------------------------------------------------------------- |
---|
[1963] | 133 | |
---|
| 134 | btScalar btRotationalLimitMotor::solveAngularLimits( |
---|
[2882] | 135 | btScalar timeStep,btVector3& axis,btScalar jacDiagABInv, |
---|
| 136 | btRigidBody * body0, btSolverBody& bodyA, btRigidBody * body1, btSolverBody& bodyB) |
---|
[1963] | 137 | { |
---|
[2882] | 138 | if (needApplyTorques()==false) return 0.0f; |
---|
[1963] | 139 | |
---|
[2882] | 140 | btScalar target_velocity = m_targetVelocity; |
---|
| 141 | btScalar maxMotorForce = m_maxMotorForce; |
---|
[1963] | 142 | |
---|
| 143 | //current error correction |
---|
[2882] | 144 | if (m_currentLimit!=0) |
---|
| 145 | { |
---|
| 146 | target_velocity = -m_ERP*m_currentLimitError/(timeStep); |
---|
| 147 | maxMotorForce = m_maxLimitForce; |
---|
| 148 | } |
---|
[1963] | 149 | |
---|
[2882] | 150 | maxMotorForce *= timeStep; |
---|
[1963] | 151 | |
---|
[2882] | 152 | // current velocity difference |
---|
[1963] | 153 | |
---|
[2882] | 154 | btVector3 angVelA; |
---|
| 155 | bodyA.getAngularVelocity(angVelA); |
---|
| 156 | btVector3 angVelB; |
---|
| 157 | bodyB.getAngularVelocity(angVelB); |
---|
[1963] | 158 | |
---|
[2882] | 159 | btVector3 vel_diff; |
---|
| 160 | vel_diff = angVelA-angVelB; |
---|
[1963] | 161 | |
---|
| 162 | |
---|
[2882] | 163 | |
---|
| 164 | btScalar rel_vel = axis.dot(vel_diff); |
---|
| 165 | |
---|
[1963] | 166 | // correction velocity |
---|
[2882] | 167 | btScalar motor_relvel = m_limitSoftness*(target_velocity - m_damping*rel_vel); |
---|
[1963] | 168 | |
---|
| 169 | |
---|
[2882] | 170 | if ( motor_relvel < SIMD_EPSILON && motor_relvel > -SIMD_EPSILON ) |
---|
| 171 | { |
---|
| 172 | return 0.0f;//no need for applying force |
---|
| 173 | } |
---|
[1963] | 174 | |
---|
| 175 | |
---|
| 176 | // correction impulse |
---|
[2882] | 177 | btScalar unclippedMotorImpulse = (1+m_bounce)*motor_relvel*jacDiagABInv; |
---|
[1963] | 178 | |
---|
| 179 | // clip correction impulse |
---|
[2882] | 180 | btScalar clippedMotorImpulse; |
---|
[1963] | 181 | |
---|
[2882] | 182 | ///@todo: should clip against accumulated impulse |
---|
| 183 | if (unclippedMotorImpulse>0.0f) |
---|
| 184 | { |
---|
| 185 | clippedMotorImpulse = unclippedMotorImpulse > maxMotorForce? maxMotorForce: unclippedMotorImpulse; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
| 189 | clippedMotorImpulse = unclippedMotorImpulse < -maxMotorForce ? -maxMotorForce: unclippedMotorImpulse; |
---|
| 190 | } |
---|
[1963] | 191 | |
---|
| 192 | |
---|
| 193 | // sort with accumulated impulses |
---|
[2882] | 194 | btScalar lo = btScalar(-1e30); |
---|
| 195 | btScalar hi = btScalar(1e30); |
---|
[1963] | 196 | |
---|
[2882] | 197 | btScalar oldaccumImpulse = m_accumulatedImpulse; |
---|
| 198 | btScalar sum = oldaccumImpulse + clippedMotorImpulse; |
---|
| 199 | m_accumulatedImpulse = sum > hi ? btScalar(0.) : sum < lo ? btScalar(0.) : sum; |
---|
[1963] | 200 | |
---|
[2882] | 201 | clippedMotorImpulse = m_accumulatedImpulse - oldaccumImpulse; |
---|
[1963] | 202 | |
---|
[2882] | 203 | btVector3 motorImp = clippedMotorImpulse * axis; |
---|
[1963] | 204 | |
---|
[2882] | 205 | //body0->applyTorqueImpulse(motorImp); |
---|
| 206 | //body1->applyTorqueImpulse(-motorImp); |
---|
[1963] | 207 | |
---|
[2882] | 208 | bodyA.applyImpulse(btVector3(0,0,0), body0->getInvInertiaTensorWorld()*axis,clippedMotorImpulse); |
---|
| 209 | bodyB.applyImpulse(btVector3(0,0,0), body1->getInvInertiaTensorWorld()*axis,-clippedMotorImpulse); |
---|
[1963] | 210 | |
---|
| 211 | |
---|
[2882] | 212 | return clippedMotorImpulse; |
---|
[1963] | 213 | |
---|
| 214 | |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | //////////////////////////// End btRotationalLimitMotor //////////////////////////////////// |
---|
| 218 | |
---|
[2882] | 219 | |
---|
| 220 | |
---|
| 221 | |
---|
[1963] | 222 | //////////////////////////// btTranslationalLimitMotor //////////////////////////////////// |
---|
[2882] | 223 | |
---|
| 224 | |
---|
| 225 | int btTranslationalLimitMotor::testLimitValue(int limitIndex, btScalar test_value) |
---|
[1963] | 226 | { |
---|
[2882] | 227 | btScalar loLimit = m_lowerLimit[limitIndex]; |
---|
| 228 | btScalar hiLimit = m_upperLimit[limitIndex]; |
---|
| 229 | if(loLimit > hiLimit) |
---|
| 230 | { |
---|
| 231 | m_currentLimit[limitIndex] = 0;//Free from violation |
---|
| 232 | m_currentLimitError[limitIndex] = btScalar(0.f); |
---|
| 233 | return 0; |
---|
| 234 | } |
---|
[1963] | 235 | |
---|
[2882] | 236 | if (test_value < loLimit) |
---|
| 237 | { |
---|
| 238 | m_currentLimit[limitIndex] = 2;//low limit violation |
---|
| 239 | m_currentLimitError[limitIndex] = test_value - loLimit; |
---|
| 240 | return 2; |
---|
| 241 | } |
---|
| 242 | else if (test_value> hiLimit) |
---|
| 243 | { |
---|
| 244 | m_currentLimit[limitIndex] = 1;//High limit violation |
---|
| 245 | m_currentLimitError[limitIndex] = test_value - hiLimit; |
---|
| 246 | return 1; |
---|
| 247 | }; |
---|
[1963] | 248 | |
---|
[2882] | 249 | m_currentLimit[limitIndex] = 0;//Free from violation |
---|
| 250 | m_currentLimitError[limitIndex] = btScalar(0.f); |
---|
| 251 | return 0; |
---|
| 252 | } // btTranslationalLimitMotor::testLimitValue() |
---|
[1963] | 253 | |
---|
[2882] | 254 | //----------------------------------------------------------------------------- |
---|
[1963] | 255 | |
---|
[2882] | 256 | btScalar btTranslationalLimitMotor::solveLinearAxis( |
---|
| 257 | btScalar timeStep, |
---|
| 258 | btScalar jacDiagABInv, |
---|
| 259 | btRigidBody& body1,btSolverBody& bodyA,const btVector3 &pointInA, |
---|
| 260 | btRigidBody& body2,btSolverBody& bodyB,const btVector3 &pointInB, |
---|
| 261 | int limit_index, |
---|
| 262 | const btVector3 & axis_normal_on_a, |
---|
| 263 | const btVector3 & anchorPos) |
---|
| 264 | { |
---|
[1963] | 265 | |
---|
[2882] | 266 | ///find relative velocity |
---|
| 267 | // btVector3 rel_pos1 = pointInA - body1.getCenterOfMassPosition(); |
---|
| 268 | // btVector3 rel_pos2 = pointInB - body2.getCenterOfMassPosition(); |
---|
| 269 | btVector3 rel_pos1 = anchorPos - body1.getCenterOfMassPosition(); |
---|
| 270 | btVector3 rel_pos2 = anchorPos - body2.getCenterOfMassPosition(); |
---|
[1963] | 271 | |
---|
[2882] | 272 | btVector3 vel1; |
---|
| 273 | bodyA.getVelocityInLocalPointObsolete(rel_pos1,vel1); |
---|
| 274 | btVector3 vel2; |
---|
| 275 | bodyB.getVelocityInLocalPointObsolete(rel_pos2,vel2); |
---|
| 276 | btVector3 vel = vel1 - vel2; |
---|
[1963] | 277 | |
---|
[2882] | 278 | btScalar rel_vel = axis_normal_on_a.dot(vel); |
---|
[1963] | 279 | |
---|
| 280 | |
---|
| 281 | |
---|
[2882] | 282 | /// apply displacement correction |
---|
[1963] | 283 | |
---|
[2882] | 284 | //positional error (zeroth order error) |
---|
| 285 | btScalar depth = -(pointInA - pointInB).dot(axis_normal_on_a); |
---|
| 286 | btScalar lo = btScalar(-1e30); |
---|
| 287 | btScalar hi = btScalar(1e30); |
---|
[1963] | 288 | |
---|
[2882] | 289 | btScalar minLimit = m_lowerLimit[limit_index]; |
---|
| 290 | btScalar maxLimit = m_upperLimit[limit_index]; |
---|
[1963] | 291 | |
---|
[2882] | 292 | //handle the limits |
---|
| 293 | if (minLimit < maxLimit) |
---|
| 294 | { |
---|
| 295 | { |
---|
| 296 | if (depth > maxLimit) |
---|
| 297 | { |
---|
| 298 | depth -= maxLimit; |
---|
| 299 | lo = btScalar(0.); |
---|
[1963] | 300 | |
---|
[2882] | 301 | } |
---|
| 302 | else |
---|
| 303 | { |
---|
| 304 | if (depth < minLimit) |
---|
| 305 | { |
---|
| 306 | depth -= minLimit; |
---|
| 307 | hi = btScalar(0.); |
---|
| 308 | } |
---|
| 309 | else |
---|
| 310 | { |
---|
| 311 | return 0.0f; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | } |
---|
[1963] | 316 | |
---|
[2882] | 317 | btScalar normalImpulse= m_limitSoftness*(m_restitution*depth/timeStep - m_damping*rel_vel) * jacDiagABInv; |
---|
[1963] | 318 | |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | |
---|
[2882] | 322 | btScalar oldNormalImpulse = m_accumulatedImpulse[limit_index]; |
---|
| 323 | btScalar sum = oldNormalImpulse + normalImpulse; |
---|
| 324 | m_accumulatedImpulse[limit_index] = sum > hi ? btScalar(0.) : sum < lo ? btScalar(0.) : sum; |
---|
| 325 | normalImpulse = m_accumulatedImpulse[limit_index] - oldNormalImpulse; |
---|
[1963] | 326 | |
---|
[2882] | 327 | btVector3 impulse_vector = axis_normal_on_a * normalImpulse; |
---|
| 328 | //body1.applyImpulse( impulse_vector, rel_pos1); |
---|
| 329 | //body2.applyImpulse(-impulse_vector, rel_pos2); |
---|
[1963] | 330 | |
---|
[2882] | 331 | btVector3 ftorqueAxis1 = rel_pos1.cross(axis_normal_on_a); |
---|
| 332 | btVector3 ftorqueAxis2 = rel_pos2.cross(axis_normal_on_a); |
---|
| 333 | bodyA.applyImpulse(axis_normal_on_a*body1.getInvMass(), body1.getInvInertiaTensorWorld()*ftorqueAxis1,normalImpulse); |
---|
| 334 | bodyB.applyImpulse(axis_normal_on_a*body2.getInvMass(), body2.getInvInertiaTensorWorld()*ftorqueAxis2,-normalImpulse); |
---|
[1963] | 335 | |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | |
---|
[2882] | 339 | return normalImpulse; |
---|
| 340 | } |
---|
[1963] | 341 | |
---|
[2882] | 342 | //////////////////////////// btTranslationalLimitMotor //////////////////////////////////// |
---|
| 343 | |
---|
[1963] | 344 | void btGeneric6DofConstraint::calculateAngleInfo() |
---|
| 345 | { |
---|
| 346 | btMatrix3x3 relative_frame = m_calculatedTransformA.getBasis().inverse()*m_calculatedTransformB.getBasis(); |
---|
| 347 | matrixToEulerXYZ(relative_frame,m_calculatedAxisAngleDiff); |
---|
| 348 | // in euler angle mode we do not actually constrain the angular velocity |
---|
[2882] | 349 | // along the axes axis[0] and axis[2] (although we do use axis[1]) : |
---|
| 350 | // |
---|
| 351 | // to get constrain w2-w1 along ...not |
---|
| 352 | // ------ --------------------- ------ |
---|
| 353 | // d(angle[0])/dt = 0 ax[1] x ax[2] ax[0] |
---|
| 354 | // d(angle[1])/dt = 0 ax[1] |
---|
| 355 | // d(angle[2])/dt = 0 ax[0] x ax[1] ax[2] |
---|
| 356 | // |
---|
| 357 | // constraining w2-w1 along an axis 'a' means that a'*(w2-w1)=0. |
---|
| 358 | // to prove the result for angle[0], write the expression for angle[0] from |
---|
| 359 | // GetInfo1 then take the derivative. to prove this for angle[2] it is |
---|
| 360 | // easier to take the euler rate expression for d(angle[2])/dt with respect |
---|
| 361 | // to the components of w and set that to 0. |
---|
[1963] | 362 | btVector3 axis0 = m_calculatedTransformB.getBasis().getColumn(0); |
---|
| 363 | btVector3 axis2 = m_calculatedTransformA.getBasis().getColumn(2); |
---|
| 364 | |
---|
| 365 | m_calculatedAxis[1] = axis2.cross(axis0); |
---|
| 366 | m_calculatedAxis[0] = m_calculatedAxis[1].cross(axis2); |
---|
| 367 | m_calculatedAxis[2] = axis0.cross(m_calculatedAxis[1]); |
---|
| 368 | |
---|
[2882] | 369 | m_calculatedAxis[0].normalize(); |
---|
| 370 | m_calculatedAxis[1].normalize(); |
---|
| 371 | m_calculatedAxis[2].normalize(); |
---|
[1963] | 372 | |
---|
| 373 | } |
---|
| 374 | |
---|
[2882] | 375 | //----------------------------------------------------------------------------- |
---|
| 376 | |
---|
[1963] | 377 | void btGeneric6DofConstraint::calculateTransforms() |
---|
| 378 | { |
---|
[2882] | 379 | m_calculatedTransformA = m_rbA.getCenterOfMassTransform() * m_frameInA; |
---|
| 380 | m_calculatedTransformB = m_rbB.getCenterOfMassTransform() * m_frameInB; |
---|
| 381 | calculateLinearInfo(); |
---|
| 382 | calculateAngleInfo(); |
---|
[1963] | 383 | } |
---|
| 384 | |
---|
[2882] | 385 | //----------------------------------------------------------------------------- |
---|
[1963] | 386 | |
---|
| 387 | void btGeneric6DofConstraint::buildLinearJacobian( |
---|
[2882] | 388 | btJacobianEntry & jacLinear,const btVector3 & normalWorld, |
---|
| 389 | const btVector3 & pivotAInW,const btVector3 & pivotBInW) |
---|
[1963] | 390 | { |
---|
[2882] | 391 | new (&jacLinear) btJacobianEntry( |
---|
[1963] | 392 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 393 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 394 | pivotAInW - m_rbA.getCenterOfMassPosition(), |
---|
| 395 | pivotBInW - m_rbB.getCenterOfMassPosition(), |
---|
| 396 | normalWorld, |
---|
| 397 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 398 | m_rbA.getInvMass(), |
---|
| 399 | m_rbB.getInvInertiaDiagLocal(), |
---|
| 400 | m_rbB.getInvMass()); |
---|
| 401 | } |
---|
| 402 | |
---|
[2882] | 403 | //----------------------------------------------------------------------------- |
---|
| 404 | |
---|
[1963] | 405 | void btGeneric6DofConstraint::buildAngularJacobian( |
---|
[2882] | 406 | btJacobianEntry & jacAngular,const btVector3 & jointAxisW) |
---|
[1963] | 407 | { |
---|
[2882] | 408 | new (&jacAngular) btJacobianEntry(jointAxisW, |
---|
[1963] | 409 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 410 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 411 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 412 | m_rbB.getInvInertiaDiagLocal()); |
---|
| 413 | |
---|
| 414 | } |
---|
| 415 | |
---|
[2882] | 416 | //----------------------------------------------------------------------------- |
---|
| 417 | |
---|
[1963] | 418 | bool btGeneric6DofConstraint::testAngularLimitMotor(int axis_index) |
---|
| 419 | { |
---|
[2882] | 420 | btScalar angle = m_calculatedAxisAngleDiff[axis_index]; |
---|
| 421 | //test limits |
---|
| 422 | m_angularLimits[axis_index].testLimitValue(angle); |
---|
| 423 | return m_angularLimits[axis_index].needApplyTorques(); |
---|
[1963] | 424 | } |
---|
| 425 | |
---|
[2882] | 426 | //----------------------------------------------------------------------------- |
---|
| 427 | |
---|
[1963] | 428 | void btGeneric6DofConstraint::buildJacobian() |
---|
| 429 | { |
---|
[2882] | 430 | if (m_useSolveConstraintObsolete) |
---|
| 431 | { |
---|
[1963] | 432 | |
---|
[2882] | 433 | // Clear accumulated impulses for the next simulation step |
---|
| 434 | m_linearLimits.m_accumulatedImpulse.setValue(btScalar(0.), btScalar(0.), btScalar(0.)); |
---|
| 435 | int i; |
---|
| 436 | for(i = 0; i < 3; i++) |
---|
| 437 | { |
---|
| 438 | m_angularLimits[i].m_accumulatedImpulse = btScalar(0.); |
---|
| 439 | } |
---|
| 440 | //calculates transform |
---|
| 441 | calculateTransforms(); |
---|
[1963] | 442 | |
---|
[2882] | 443 | // const btVector3& pivotAInW = m_calculatedTransformA.getOrigin(); |
---|
| 444 | // const btVector3& pivotBInW = m_calculatedTransformB.getOrigin(); |
---|
| 445 | calcAnchorPos(); |
---|
| 446 | btVector3 pivotAInW = m_AnchorPos; |
---|
| 447 | btVector3 pivotBInW = m_AnchorPos; |
---|
[1963] | 448 | |
---|
[2882] | 449 | // not used here |
---|
| 450 | // btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition(); |
---|
| 451 | // btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition(); |
---|
[1963] | 452 | |
---|
[2882] | 453 | btVector3 normalWorld; |
---|
| 454 | //linear part |
---|
| 455 | for (i=0;i<3;i++) |
---|
| 456 | { |
---|
| 457 | if (m_linearLimits.isLimited(i)) |
---|
| 458 | { |
---|
| 459 | if (m_useLinearReferenceFrameA) |
---|
| 460 | normalWorld = m_calculatedTransformA.getBasis().getColumn(i); |
---|
| 461 | else |
---|
| 462 | normalWorld = m_calculatedTransformB.getBasis().getColumn(i); |
---|
[1963] | 463 | |
---|
[2882] | 464 | buildLinearJacobian( |
---|
| 465 | m_jacLinear[i],normalWorld , |
---|
| 466 | pivotAInW,pivotBInW); |
---|
[1963] | 467 | |
---|
[2882] | 468 | } |
---|
| 469 | } |
---|
[1963] | 470 | |
---|
[2882] | 471 | // angular part |
---|
| 472 | for (i=0;i<3;i++) |
---|
| 473 | { |
---|
| 474 | //calculates error angle |
---|
| 475 | if (testAngularLimitMotor(i)) |
---|
| 476 | { |
---|
| 477 | normalWorld = this->getAxis(i); |
---|
| 478 | // Create angular atom |
---|
| 479 | buildAngularJacobian(m_jacAng[i],normalWorld); |
---|
| 480 | } |
---|
| 481 | } |
---|
[1963] | 482 | |
---|
[2882] | 483 | } |
---|
| 484 | } |
---|
[1963] | 485 | |
---|
[2882] | 486 | //----------------------------------------------------------------------------- |
---|
| 487 | |
---|
| 488 | void btGeneric6DofConstraint::getInfo1 (btConstraintInfo1* info) |
---|
| 489 | { |
---|
| 490 | if (m_useSolveConstraintObsolete) |
---|
| 491 | { |
---|
| 492 | info->m_numConstraintRows = 0; |
---|
| 493 | info->nub = 0; |
---|
| 494 | } else |
---|
| 495 | { |
---|
| 496 | //prepare constraint |
---|
| 497 | calculateTransforms(); |
---|
| 498 | info->m_numConstraintRows = 0; |
---|
| 499 | info->nub = 6; |
---|
| 500 | int i; |
---|
| 501 | //test linear limits |
---|
| 502 | for(i = 0; i < 3; i++) |
---|
| 503 | { |
---|
| 504 | if(m_linearLimits.needApplyForce(i)) |
---|
| 505 | { |
---|
| 506 | info->m_numConstraintRows++; |
---|
| 507 | info->nub--; |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | //test angular limits |
---|
| 511 | for (i=0;i<3 ;i++ ) |
---|
| 512 | { |
---|
| 513 | if(testAngularLimitMotor(i)) |
---|
| 514 | { |
---|
| 515 | info->m_numConstraintRows++; |
---|
| 516 | info->nub--; |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | } |
---|
[1963] | 520 | } |
---|
| 521 | |
---|
[2882] | 522 | //----------------------------------------------------------------------------- |
---|
[1963] | 523 | |
---|
[2882] | 524 | void btGeneric6DofConstraint::getInfo2 (btConstraintInfo2* info) |
---|
[1963] | 525 | { |
---|
[2882] | 526 | btAssert(!m_useSolveConstraintObsolete); |
---|
| 527 | int row = setLinearLimits(info); |
---|
| 528 | setAngularLimits(info, row); |
---|
| 529 | } |
---|
[1963] | 530 | |
---|
[2882] | 531 | //----------------------------------------------------------------------------- |
---|
[1963] | 532 | |
---|
[2882] | 533 | int btGeneric6DofConstraint::setLinearLimits(btConstraintInfo2* info) |
---|
| 534 | { |
---|
| 535 | btGeneric6DofConstraint * d6constraint = this; |
---|
| 536 | int row = 0; |
---|
| 537 | //solve linear limits |
---|
| 538 | btRotationalLimitMotor limot; |
---|
| 539 | for (int i=0;i<3 ;i++ ) |
---|
| 540 | { |
---|
| 541 | if(m_linearLimits.needApplyForce(i)) |
---|
| 542 | { // re-use rotational motor code |
---|
| 543 | limot.m_bounce = btScalar(0.f); |
---|
| 544 | limot.m_currentLimit = m_linearLimits.m_currentLimit[i]; |
---|
| 545 | limot.m_currentLimitError = m_linearLimits.m_currentLimitError[i]; |
---|
| 546 | limot.m_damping = m_linearLimits.m_damping; |
---|
| 547 | limot.m_enableMotor = m_linearLimits.m_enableMotor[i]; |
---|
| 548 | limot.m_ERP = m_linearLimits.m_restitution; |
---|
| 549 | limot.m_hiLimit = m_linearLimits.m_upperLimit[i]; |
---|
| 550 | limot.m_limitSoftness = m_linearLimits.m_limitSoftness; |
---|
| 551 | limot.m_loLimit = m_linearLimits.m_lowerLimit[i]; |
---|
| 552 | limot.m_maxLimitForce = btScalar(0.f); |
---|
| 553 | limot.m_maxMotorForce = m_linearLimits.m_maxMotorForce[i]; |
---|
| 554 | limot.m_targetVelocity = m_linearLimits.m_targetVelocity[i]; |
---|
| 555 | btVector3 axis = m_calculatedTransformA.getBasis().getColumn(i); |
---|
| 556 | row += get_limit_motor_info2(&limot, &m_rbA, &m_rbB, info, row, axis, 0); |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | return row; |
---|
| 560 | } |
---|
[1963] | 561 | |
---|
[2882] | 562 | //----------------------------------------------------------------------------- |
---|
[1963] | 563 | |
---|
[2882] | 564 | int btGeneric6DofConstraint::setAngularLimits(btConstraintInfo2 *info, int row_offset) |
---|
| 565 | { |
---|
| 566 | btGeneric6DofConstraint * d6constraint = this; |
---|
| 567 | int row = row_offset; |
---|
| 568 | //solve angular limits |
---|
| 569 | for (int i=0;i<3 ;i++ ) |
---|
| 570 | { |
---|
| 571 | if(d6constraint->getRotationalLimitMotor(i)->needApplyTorques()) |
---|
| 572 | { |
---|
| 573 | btVector3 axis = d6constraint->getAxis(i); |
---|
| 574 | row += get_limit_motor_info2( |
---|
| 575 | d6constraint->getRotationalLimitMotor(i), |
---|
| 576 | &m_rbA, |
---|
| 577 | &m_rbB, |
---|
| 578 | info,row,axis,1); |
---|
| 579 | } |
---|
| 580 | } |
---|
[1963] | 581 | |
---|
[2882] | 582 | return row; |
---|
| 583 | } |
---|
[1963] | 584 | |
---|
[2882] | 585 | //----------------------------------------------------------------------------- |
---|
[1963] | 586 | |
---|
[2882] | 587 | void btGeneric6DofConstraint::solveConstraintObsolete(btSolverBody& bodyA,btSolverBody& bodyB,btScalar timeStep) |
---|
| 588 | { |
---|
| 589 | if (m_useSolveConstraintObsolete) |
---|
| 590 | { |
---|
[1963] | 591 | |
---|
| 592 | |
---|
[2882] | 593 | m_timeStep = timeStep; |
---|
[1963] | 594 | |
---|
[2882] | 595 | //calculateTransforms(); |
---|
[1963] | 596 | |
---|
[2882] | 597 | int i; |
---|
[1963] | 598 | |
---|
[2882] | 599 | // linear |
---|
| 600 | |
---|
| 601 | btVector3 pointInA = m_calculatedTransformA.getOrigin(); |
---|
| 602 | btVector3 pointInB = m_calculatedTransformB.getOrigin(); |
---|
| 603 | |
---|
| 604 | btScalar jacDiagABInv; |
---|
| 605 | btVector3 linear_axis; |
---|
| 606 | for (i=0;i<3;i++) |
---|
| 607 | { |
---|
| 608 | if (m_linearLimits.isLimited(i)) |
---|
| 609 | { |
---|
| 610 | jacDiagABInv = btScalar(1.) / m_jacLinear[i].getDiagonal(); |
---|
| 611 | |
---|
| 612 | if (m_useLinearReferenceFrameA) |
---|
| 613 | linear_axis = m_calculatedTransformA.getBasis().getColumn(i); |
---|
| 614 | else |
---|
| 615 | linear_axis = m_calculatedTransformB.getBasis().getColumn(i); |
---|
| 616 | |
---|
| 617 | m_linearLimits.solveLinearAxis( |
---|
| 618 | m_timeStep, |
---|
| 619 | jacDiagABInv, |
---|
| 620 | m_rbA,bodyA,pointInA, |
---|
| 621 | m_rbB,bodyB,pointInB, |
---|
| 622 | i,linear_axis, m_AnchorPos); |
---|
| 623 | |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | // angular |
---|
| 628 | btVector3 angular_axis; |
---|
| 629 | btScalar angularJacDiagABInv; |
---|
| 630 | for (i=0;i<3;i++) |
---|
| 631 | { |
---|
| 632 | if (m_angularLimits[i].needApplyTorques()) |
---|
| 633 | { |
---|
| 634 | |
---|
| 635 | // get axis |
---|
| 636 | angular_axis = getAxis(i); |
---|
| 637 | |
---|
| 638 | angularJacDiagABInv = btScalar(1.) / m_jacAng[i].getDiagonal(); |
---|
| 639 | |
---|
| 640 | m_angularLimits[i].solveAngularLimits(m_timeStep,angular_axis,angularJacDiagABInv, &m_rbA,bodyA,&m_rbB,bodyB); |
---|
| 641 | } |
---|
| 642 | } |
---|
| 643 | } |
---|
[1963] | 644 | } |
---|
| 645 | |
---|
[2882] | 646 | //----------------------------------------------------------------------------- |
---|
| 647 | |
---|
[1963] | 648 | void btGeneric6DofConstraint::updateRHS(btScalar timeStep) |
---|
| 649 | { |
---|
[2882] | 650 | (void)timeStep; |
---|
[1963] | 651 | |
---|
| 652 | } |
---|
| 653 | |
---|
[2882] | 654 | //----------------------------------------------------------------------------- |
---|
| 655 | |
---|
[1963] | 656 | btVector3 btGeneric6DofConstraint::getAxis(int axis_index) const |
---|
| 657 | { |
---|
[2882] | 658 | return m_calculatedAxis[axis_index]; |
---|
[1963] | 659 | } |
---|
| 660 | |
---|
[2882] | 661 | //----------------------------------------------------------------------------- |
---|
| 662 | |
---|
[1963] | 663 | btScalar btGeneric6DofConstraint::getAngle(int axis_index) const |
---|
| 664 | { |
---|
[2882] | 665 | return m_calculatedAxisAngleDiff[axis_index]; |
---|
[1963] | 666 | } |
---|
| 667 | |
---|
[2882] | 668 | //----------------------------------------------------------------------------- |
---|
| 669 | |
---|
[1963] | 670 | void btGeneric6DofConstraint::calcAnchorPos(void) |
---|
| 671 | { |
---|
| 672 | btScalar imA = m_rbA.getInvMass(); |
---|
| 673 | btScalar imB = m_rbB.getInvMass(); |
---|
| 674 | btScalar weight; |
---|
| 675 | if(imB == btScalar(0.0)) |
---|
| 676 | { |
---|
| 677 | weight = btScalar(1.0); |
---|
| 678 | } |
---|
| 679 | else |
---|
| 680 | { |
---|
| 681 | weight = imA / (imA + imB); |
---|
| 682 | } |
---|
| 683 | const btVector3& pA = m_calculatedTransformA.getOrigin(); |
---|
| 684 | const btVector3& pB = m_calculatedTransformB.getOrigin(); |
---|
| 685 | m_AnchorPos = pA * weight + pB * (btScalar(1.0) - weight); |
---|
| 686 | return; |
---|
| 687 | } // btGeneric6DofConstraint::calcAnchorPos() |
---|
| 688 | |
---|
[2882] | 689 | //----------------------------------------------------------------------------- |
---|
| 690 | |
---|
| 691 | void btGeneric6DofConstraint::calculateLinearInfo() |
---|
| 692 | { |
---|
| 693 | m_calculatedLinearDiff = m_calculatedTransformB.getOrigin() - m_calculatedTransformA.getOrigin(); |
---|
| 694 | m_calculatedLinearDiff = m_calculatedTransformA.getBasis().inverse() * m_calculatedLinearDiff; |
---|
| 695 | for(int i = 0; i < 3; i++) |
---|
| 696 | { |
---|
| 697 | m_linearLimits.testLimitValue(i, m_calculatedLinearDiff[i]); |
---|
| 698 | } |
---|
| 699 | } // btGeneric6DofConstraint::calculateLinearInfo() |
---|
| 700 | |
---|
| 701 | //----------------------------------------------------------------------------- |
---|
| 702 | |
---|
| 703 | int btGeneric6DofConstraint::get_limit_motor_info2( |
---|
| 704 | btRotationalLimitMotor * limot, |
---|
| 705 | btRigidBody * body0, btRigidBody * body1, |
---|
| 706 | btConstraintInfo2 *info, int row, btVector3& ax1, int rotational) |
---|
| 707 | { |
---|
| 708 | int srow = row * info->rowskip; |
---|
| 709 | int powered = limot->m_enableMotor; |
---|
| 710 | int limit = limot->m_currentLimit; |
---|
| 711 | if (powered || limit) |
---|
| 712 | { // if the joint is powered, or has joint limits, add in the extra row |
---|
| 713 | btScalar *J1 = rotational ? info->m_J1angularAxis : info->m_J1linearAxis; |
---|
| 714 | btScalar *J2 = rotational ? info->m_J2angularAxis : 0; |
---|
| 715 | J1[srow+0] = ax1[0]; |
---|
| 716 | J1[srow+1] = ax1[1]; |
---|
| 717 | J1[srow+2] = ax1[2]; |
---|
| 718 | if(rotational) |
---|
| 719 | { |
---|
| 720 | J2[srow+0] = -ax1[0]; |
---|
| 721 | J2[srow+1] = -ax1[1]; |
---|
| 722 | J2[srow+2] = -ax1[2]; |
---|
| 723 | } |
---|
| 724 | if((!rotational) && limit) |
---|
| 725 | { |
---|
| 726 | btVector3 ltd; // Linear Torque Decoupling vector |
---|
| 727 | btVector3 c = m_calculatedTransformB.getOrigin() - body0->getCenterOfMassPosition(); |
---|
| 728 | ltd = c.cross(ax1); |
---|
| 729 | info->m_J1angularAxis[srow+0] = ltd[0]; |
---|
| 730 | info->m_J1angularAxis[srow+1] = ltd[1]; |
---|
| 731 | info->m_J1angularAxis[srow+2] = ltd[2]; |
---|
| 732 | |
---|
| 733 | c = m_calculatedTransformB.getOrigin() - body1->getCenterOfMassPosition(); |
---|
| 734 | ltd = -c.cross(ax1); |
---|
| 735 | info->m_J2angularAxis[srow+0] = ltd[0]; |
---|
| 736 | info->m_J2angularAxis[srow+1] = ltd[1]; |
---|
| 737 | info->m_J2angularAxis[srow+2] = ltd[2]; |
---|
| 738 | } |
---|
| 739 | // if we're limited low and high simultaneously, the joint motor is |
---|
| 740 | // ineffective |
---|
| 741 | if (limit && (limot->m_loLimit == limot->m_hiLimit)) powered = 0; |
---|
| 742 | info->m_constraintError[srow] = btScalar(0.f); |
---|
| 743 | if (powered) |
---|
| 744 | { |
---|
| 745 | info->cfm[srow] = 0.0f; |
---|
| 746 | if(!limit) |
---|
| 747 | { |
---|
| 748 | info->m_constraintError[srow] += limot->m_targetVelocity; |
---|
| 749 | info->m_lowerLimit[srow] = -limot->m_maxMotorForce; |
---|
| 750 | info->m_upperLimit[srow] = limot->m_maxMotorForce; |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | if(limit) |
---|
| 754 | { |
---|
| 755 | btScalar k = info->fps * limot->m_ERP; |
---|
| 756 | if(!rotational) |
---|
| 757 | { |
---|
| 758 | info->m_constraintError[srow] += k * limot->m_currentLimitError; |
---|
| 759 | } |
---|
| 760 | else |
---|
| 761 | { |
---|
| 762 | info->m_constraintError[srow] += -k * limot->m_currentLimitError; |
---|
| 763 | } |
---|
| 764 | info->cfm[srow] = 0.0f; |
---|
| 765 | if (limot->m_loLimit == limot->m_hiLimit) |
---|
| 766 | { // limited low and high simultaneously |
---|
| 767 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 768 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 769 | } |
---|
| 770 | else |
---|
| 771 | { |
---|
| 772 | if (limit == 1) |
---|
| 773 | { |
---|
| 774 | info->m_lowerLimit[srow] = 0; |
---|
| 775 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 776 | } |
---|
| 777 | else |
---|
| 778 | { |
---|
| 779 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 780 | info->m_upperLimit[srow] = 0; |
---|
| 781 | } |
---|
| 782 | // deal with bounce |
---|
| 783 | if (limot->m_bounce > 0) |
---|
| 784 | { |
---|
| 785 | // calculate joint velocity |
---|
| 786 | btScalar vel; |
---|
| 787 | if (rotational) |
---|
| 788 | { |
---|
| 789 | vel = body0->getAngularVelocity().dot(ax1); |
---|
| 790 | if (body1) |
---|
| 791 | vel -= body1->getAngularVelocity().dot(ax1); |
---|
| 792 | } |
---|
| 793 | else |
---|
| 794 | { |
---|
| 795 | vel = body0->getLinearVelocity().dot(ax1); |
---|
| 796 | if (body1) |
---|
| 797 | vel -= body1->getLinearVelocity().dot(ax1); |
---|
| 798 | } |
---|
| 799 | // only apply bounce if the velocity is incoming, and if the |
---|
| 800 | // resulting c[] exceeds what we already have. |
---|
| 801 | if (limit == 1) |
---|
| 802 | { |
---|
| 803 | if (vel < 0) |
---|
| 804 | { |
---|
| 805 | btScalar newc = -limot->m_bounce* vel; |
---|
| 806 | if (newc > info->m_constraintError[srow]) |
---|
| 807 | info->m_constraintError[srow] = newc; |
---|
| 808 | } |
---|
| 809 | } |
---|
| 810 | else |
---|
| 811 | { |
---|
| 812 | if (vel > 0) |
---|
| 813 | { |
---|
| 814 | btScalar newc = -limot->m_bounce * vel; |
---|
| 815 | if (newc < info->m_constraintError[srow]) |
---|
| 816 | info->m_constraintError[srow] = newc; |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | } |
---|
| 820 | } |
---|
| 821 | } |
---|
| 822 | return 1; |
---|
| 823 | } |
---|
| 824 | else return 0; |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | //----------------------------------------------------------------------------- |
---|
| 828 | //----------------------------------------------------------------------------- |
---|
| 829 | //----------------------------------------------------------------------------- |
---|