[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 | |
---|
| 17 | #include "btHingeConstraint.h" |
---|
| 18 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 19 | #include "LinearMath/btTransformUtil.h" |
---|
| 20 | #include "LinearMath/btMinMax.h" |
---|
| 21 | #include <new> |
---|
[2882] | 22 | #include "btSolverBody.h" |
---|
[1963] | 23 | |
---|
| 24 | |
---|
[8351] | 25 | |
---|
| 26 | //#define HINGE_USE_OBSOLETE_SOLVER false |
---|
[2882] | 27 | #define HINGE_USE_OBSOLETE_SOLVER false |
---|
| 28 | |
---|
[8351] | 29 | #define HINGE_USE_FRAME_OFFSET true |
---|
[2882] | 30 | |
---|
[8351] | 31 | #ifndef __SPU__ |
---|
[2882] | 32 | |
---|
[1963] | 33 | |
---|
[2882] | 34 | |
---|
[8351] | 35 | |
---|
| 36 | |
---|
[1963] | 37 | btHingeConstraint::btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB, |
---|
[8351] | 38 | const btVector3& axisInA,const btVector3& axisInB, bool useReferenceFrameA) |
---|
[1963] | 39 | :btTypedConstraint(HINGE_CONSTRAINT_TYPE, rbA,rbB), |
---|
| 40 | m_angularOnly(false), |
---|
[2882] | 41 | m_enableAngularMotor(false), |
---|
| 42 | m_useSolveConstraintObsolete(HINGE_USE_OBSOLETE_SOLVER), |
---|
[8351] | 43 | m_useOffsetForConstraintFrame(HINGE_USE_FRAME_OFFSET), |
---|
| 44 | m_useReferenceFrameA(useReferenceFrameA), |
---|
| 45 | m_flags(0) |
---|
[8393] | 46 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 47 | ,m_limit() |
---|
| 48 | #endif |
---|
[1963] | 49 | { |
---|
| 50 | m_rbAFrame.getOrigin() = pivotInA; |
---|
| 51 | |
---|
| 52 | // since no frame is given, assume this to be zero angle and just pick rb transform axis |
---|
| 53 | btVector3 rbAxisA1 = rbA.getCenterOfMassTransform().getBasis().getColumn(0); |
---|
| 54 | |
---|
| 55 | btVector3 rbAxisA2; |
---|
| 56 | btScalar projection = axisInA.dot(rbAxisA1); |
---|
| 57 | if (projection >= 1.0f - SIMD_EPSILON) { |
---|
| 58 | rbAxisA1 = -rbA.getCenterOfMassTransform().getBasis().getColumn(2); |
---|
| 59 | rbAxisA2 = rbA.getCenterOfMassTransform().getBasis().getColumn(1); |
---|
| 60 | } else if (projection <= -1.0f + SIMD_EPSILON) { |
---|
| 61 | rbAxisA1 = rbA.getCenterOfMassTransform().getBasis().getColumn(2); |
---|
| 62 | rbAxisA2 = rbA.getCenterOfMassTransform().getBasis().getColumn(1); |
---|
| 63 | } else { |
---|
| 64 | rbAxisA2 = axisInA.cross(rbAxisA1); |
---|
| 65 | rbAxisA1 = rbAxisA2.cross(axisInA); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | m_rbAFrame.getBasis().setValue( rbAxisA1.getX(),rbAxisA2.getX(),axisInA.getX(), |
---|
| 69 | rbAxisA1.getY(),rbAxisA2.getY(),axisInA.getY(), |
---|
| 70 | rbAxisA1.getZ(),rbAxisA2.getZ(),axisInA.getZ() ); |
---|
| 71 | |
---|
| 72 | btQuaternion rotationArc = shortestArcQuat(axisInA,axisInB); |
---|
| 73 | btVector3 rbAxisB1 = quatRotate(rotationArc,rbAxisA1); |
---|
| 74 | btVector3 rbAxisB2 = axisInB.cross(rbAxisB1); |
---|
| 75 | |
---|
| 76 | m_rbBFrame.getOrigin() = pivotInB; |
---|
[2882] | 77 | m_rbBFrame.getBasis().setValue( rbAxisB1.getX(),rbAxisB2.getX(),axisInB.getX(), |
---|
| 78 | rbAxisB1.getY(),rbAxisB2.getY(),axisInB.getY(), |
---|
| 79 | rbAxisB1.getZ(),rbAxisB2.getZ(),axisInB.getZ() ); |
---|
[1963] | 80 | |
---|
[8393] | 81 | #ifndef _BT_USE_CENTER_LIMIT_ |
---|
[1963] | 82 | //start with free |
---|
[8351] | 83 | m_lowerLimit = btScalar(1.0f); |
---|
| 84 | m_upperLimit = btScalar(-1.0f); |
---|
[1963] | 85 | m_biasFactor = 0.3f; |
---|
| 86 | m_relaxationFactor = 1.0f; |
---|
| 87 | m_limitSoftness = 0.9f; |
---|
| 88 | m_solveLimit = false; |
---|
[8393] | 89 | #endif |
---|
[2882] | 90 | m_referenceSign = m_useReferenceFrameA ? btScalar(-1.f) : btScalar(1.f); |
---|
[1963] | 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
[8351] | 94 | |
---|
| 95 | btHingeConstraint::btHingeConstraint(btRigidBody& rbA,const btVector3& pivotInA,const btVector3& axisInA, bool useReferenceFrameA) |
---|
[2882] | 96 | :btTypedConstraint(HINGE_CONSTRAINT_TYPE, rbA), m_angularOnly(false), m_enableAngularMotor(false), |
---|
| 97 | m_useSolveConstraintObsolete(HINGE_USE_OBSOLETE_SOLVER), |
---|
[8351] | 98 | m_useOffsetForConstraintFrame(HINGE_USE_FRAME_OFFSET), |
---|
| 99 | m_useReferenceFrameA(useReferenceFrameA), |
---|
| 100 | m_flags(0) |
---|
[8393] | 101 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 102 | ,m_limit() |
---|
| 103 | #endif |
---|
[1963] | 104 | { |
---|
| 105 | |
---|
| 106 | // since no frame is given, assume this to be zero angle and just pick rb transform axis |
---|
| 107 | // fixed axis in worldspace |
---|
| 108 | btVector3 rbAxisA1, rbAxisA2; |
---|
| 109 | btPlaneSpace1(axisInA, rbAxisA1, rbAxisA2); |
---|
| 110 | |
---|
| 111 | m_rbAFrame.getOrigin() = pivotInA; |
---|
| 112 | m_rbAFrame.getBasis().setValue( rbAxisA1.getX(),rbAxisA2.getX(),axisInA.getX(), |
---|
| 113 | rbAxisA1.getY(),rbAxisA2.getY(),axisInA.getY(), |
---|
| 114 | rbAxisA1.getZ(),rbAxisA2.getZ(),axisInA.getZ() ); |
---|
| 115 | |
---|
[2882] | 116 | btVector3 axisInB = rbA.getCenterOfMassTransform().getBasis() * axisInA; |
---|
[1963] | 117 | |
---|
| 118 | btQuaternion rotationArc = shortestArcQuat(axisInA,axisInB); |
---|
| 119 | btVector3 rbAxisB1 = quatRotate(rotationArc,rbAxisA1); |
---|
| 120 | btVector3 rbAxisB2 = axisInB.cross(rbAxisB1); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | m_rbBFrame.getOrigin() = rbA.getCenterOfMassTransform()(pivotInA); |
---|
| 124 | m_rbBFrame.getBasis().setValue( rbAxisB1.getX(),rbAxisB2.getX(),axisInB.getX(), |
---|
| 125 | rbAxisB1.getY(),rbAxisB2.getY(),axisInB.getY(), |
---|
| 126 | rbAxisB1.getZ(),rbAxisB2.getZ(),axisInB.getZ() ); |
---|
| 127 | |
---|
[8393] | 128 | #ifndef _BT_USE_CENTER_LIMIT_ |
---|
[1963] | 129 | //start with free |
---|
[8351] | 130 | m_lowerLimit = btScalar(1.0f); |
---|
| 131 | m_upperLimit = btScalar(-1.0f); |
---|
[1963] | 132 | m_biasFactor = 0.3f; |
---|
| 133 | m_relaxationFactor = 1.0f; |
---|
| 134 | m_limitSoftness = 0.9f; |
---|
| 135 | m_solveLimit = false; |
---|
[8393] | 136 | #endif |
---|
[2882] | 137 | m_referenceSign = m_useReferenceFrameA ? btScalar(-1.f) : btScalar(1.f); |
---|
[1963] | 138 | } |
---|
| 139 | |
---|
[2882] | 140 | |
---|
[8351] | 141 | |
---|
[1963] | 142 | btHingeConstraint::btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, |
---|
[2882] | 143 | const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA) |
---|
[1963] | 144 | :btTypedConstraint(HINGE_CONSTRAINT_TYPE, rbA,rbB),m_rbAFrame(rbAFrame),m_rbBFrame(rbBFrame), |
---|
| 145 | m_angularOnly(false), |
---|
[2882] | 146 | m_enableAngularMotor(false), |
---|
| 147 | m_useSolveConstraintObsolete(HINGE_USE_OBSOLETE_SOLVER), |
---|
[8351] | 148 | m_useOffsetForConstraintFrame(HINGE_USE_FRAME_OFFSET), |
---|
| 149 | m_useReferenceFrameA(useReferenceFrameA), |
---|
| 150 | m_flags(0) |
---|
[8393] | 151 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 152 | ,m_limit() |
---|
| 153 | #endif |
---|
[1963] | 154 | { |
---|
[8393] | 155 | #ifndef _BT_USE_CENTER_LIMIT_ |
---|
[1963] | 156 | //start with free |
---|
[8351] | 157 | m_lowerLimit = btScalar(1.0f); |
---|
| 158 | m_upperLimit = btScalar(-1.0f); |
---|
[1963] | 159 | m_biasFactor = 0.3f; |
---|
| 160 | m_relaxationFactor = 1.0f; |
---|
| 161 | m_limitSoftness = 0.9f; |
---|
| 162 | m_solveLimit = false; |
---|
[8393] | 163 | #endif |
---|
[2882] | 164 | m_referenceSign = m_useReferenceFrameA ? btScalar(-1.f) : btScalar(1.f); |
---|
[1963] | 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[8351] | 168 | |
---|
[2882] | 169 | btHingeConstraint::btHingeConstraint(btRigidBody& rbA, const btTransform& rbAFrame, bool useReferenceFrameA) |
---|
[1963] | 170 | :btTypedConstraint(HINGE_CONSTRAINT_TYPE, rbA),m_rbAFrame(rbAFrame),m_rbBFrame(rbAFrame), |
---|
| 171 | m_angularOnly(false), |
---|
[2882] | 172 | m_enableAngularMotor(false), |
---|
| 173 | m_useSolveConstraintObsolete(HINGE_USE_OBSOLETE_SOLVER), |
---|
[8351] | 174 | m_useOffsetForConstraintFrame(HINGE_USE_FRAME_OFFSET), |
---|
| 175 | m_useReferenceFrameA(useReferenceFrameA), |
---|
| 176 | m_flags(0) |
---|
[8393] | 177 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 178 | ,m_limit() |
---|
| 179 | #endif |
---|
[1963] | 180 | { |
---|
| 181 | ///not providing rigidbody B means implicitly using worldspace for body B |
---|
| 182 | |
---|
| 183 | m_rbBFrame.getOrigin() = m_rbA.getCenterOfMassTransform()(m_rbAFrame.getOrigin()); |
---|
[8393] | 184 | #ifndef _BT_USE_CENTER_LIMIT_ |
---|
[1963] | 185 | //start with free |
---|
[8351] | 186 | m_lowerLimit = btScalar(1.0f); |
---|
| 187 | m_upperLimit = btScalar(-1.0f); |
---|
[1963] | 188 | m_biasFactor = 0.3f; |
---|
| 189 | m_relaxationFactor = 1.0f; |
---|
| 190 | m_limitSoftness = 0.9f; |
---|
| 191 | m_solveLimit = false; |
---|
[8393] | 192 | #endif |
---|
[2882] | 193 | m_referenceSign = m_useReferenceFrameA ? btScalar(-1.f) : btScalar(1.f); |
---|
[1963] | 194 | } |
---|
| 195 | |
---|
[2882] | 196 | |
---|
[8351] | 197 | |
---|
[1963] | 198 | void btHingeConstraint::buildJacobian() |
---|
| 199 | { |
---|
[2882] | 200 | if (m_useSolveConstraintObsolete) |
---|
[1963] | 201 | { |
---|
[2882] | 202 | m_appliedImpulse = btScalar(0.); |
---|
[8351] | 203 | m_accMotorImpulse = btScalar(0.); |
---|
[1963] | 204 | |
---|
[2882] | 205 | if (!m_angularOnly) |
---|
[1963] | 206 | { |
---|
[2882] | 207 | btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*m_rbAFrame.getOrigin(); |
---|
| 208 | btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*m_rbBFrame.getOrigin(); |
---|
| 209 | btVector3 relPos = pivotBInW - pivotAInW; |
---|
[1963] | 210 | |
---|
[2882] | 211 | btVector3 normal[3]; |
---|
| 212 | if (relPos.length2() > SIMD_EPSILON) |
---|
| 213 | { |
---|
| 214 | normal[0] = relPos.normalized(); |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | normal[0].setValue(btScalar(1.0),0,0); |
---|
| 219 | } |
---|
[1963] | 220 | |
---|
[2882] | 221 | btPlaneSpace1(normal[0], normal[1], normal[2]); |
---|
| 222 | |
---|
| 223 | for (int i=0;i<3;i++) |
---|
| 224 | { |
---|
| 225 | new (&m_jac[i]) btJacobianEntry( |
---|
[1963] | 226 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 227 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 228 | pivotAInW - m_rbA.getCenterOfMassPosition(), |
---|
| 229 | pivotBInW - m_rbB.getCenterOfMassPosition(), |
---|
| 230 | normal[i], |
---|
| 231 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 232 | m_rbA.getInvMass(), |
---|
| 233 | m_rbB.getInvInertiaDiagLocal(), |
---|
| 234 | m_rbB.getInvMass()); |
---|
[2882] | 235 | } |
---|
[1963] | 236 | } |
---|
| 237 | |
---|
[2882] | 238 | //calculate two perpendicular jointAxis, orthogonal to hingeAxis |
---|
| 239 | //these two jointAxis require equal angular velocities for both bodies |
---|
[1963] | 240 | |
---|
[2882] | 241 | //this is unused for now, it's a todo |
---|
| 242 | btVector3 jointAxis0local; |
---|
| 243 | btVector3 jointAxis1local; |
---|
[1963] | 244 | |
---|
[2882] | 245 | btPlaneSpace1(m_rbAFrame.getBasis().getColumn(2),jointAxis0local,jointAxis1local); |
---|
[1963] | 246 | |
---|
[2882] | 247 | btVector3 jointAxis0 = getRigidBodyA().getCenterOfMassTransform().getBasis() * jointAxis0local; |
---|
| 248 | btVector3 jointAxis1 = getRigidBodyA().getCenterOfMassTransform().getBasis() * jointAxis1local; |
---|
| 249 | btVector3 hingeAxisWorld = getRigidBodyA().getCenterOfMassTransform().getBasis() * m_rbAFrame.getBasis().getColumn(2); |
---|
| 250 | |
---|
| 251 | new (&m_jacAng[0]) btJacobianEntry(jointAxis0, |
---|
| 252 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 253 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 254 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 255 | m_rbB.getInvInertiaDiagLocal()); |
---|
[1963] | 256 | |
---|
[2882] | 257 | new (&m_jacAng[1]) btJacobianEntry(jointAxis1, |
---|
| 258 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 259 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 260 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 261 | m_rbB.getInvInertiaDiagLocal()); |
---|
[1963] | 262 | |
---|
[2882] | 263 | new (&m_jacAng[2]) btJacobianEntry(hingeAxisWorld, |
---|
| 264 | m_rbA.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 265 | m_rbB.getCenterOfMassTransform().getBasis().transpose(), |
---|
| 266 | m_rbA.getInvInertiaDiagLocal(), |
---|
| 267 | m_rbB.getInvInertiaDiagLocal()); |
---|
[1963] | 268 | |
---|
[2882] | 269 | // clear accumulator |
---|
| 270 | m_accLimitImpulse = btScalar(0.); |
---|
[1963] | 271 | |
---|
[2882] | 272 | // test angular limit |
---|
[8351] | 273 | testLimit(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform()); |
---|
[1963] | 274 | |
---|
[2882] | 275 | //Compute K = J*W*J' for hinge axis |
---|
| 276 | btVector3 axisA = getRigidBodyA().getCenterOfMassTransform().getBasis() * m_rbAFrame.getBasis().getColumn(2); |
---|
| 277 | m_kHinge = 1.0f / (getRigidBodyA().computeAngularImpulseDenominator(axisA) + |
---|
| 278 | getRigidBodyB().computeAngularImpulseDenominator(axisA)); |
---|
| 279 | |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
[8351] | 284 | #endif //__SPU__ |
---|
| 285 | |
---|
| 286 | |
---|
[2882] | 287 | void btHingeConstraint::getInfo1(btConstraintInfo1* info) |
---|
| 288 | { |
---|
| 289 | if (m_useSolveConstraintObsolete) |
---|
[1963] | 290 | { |
---|
[2882] | 291 | info->m_numConstraintRows = 0; |
---|
| 292 | info->nub = 0; |
---|
| 293 | } |
---|
| 294 | else |
---|
| 295 | { |
---|
| 296 | info->m_numConstraintRows = 5; // Fixed 3 linear + 2 angular |
---|
| 297 | info->nub = 1; |
---|
[8351] | 298 | //always add the row, to avoid computation (data is not available yet) |
---|
[2882] | 299 | //prepare constraint |
---|
[8351] | 300 | testLimit(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform()); |
---|
[2882] | 301 | if(getSolveLimit() || getEnableAngularMotor()) |
---|
[1963] | 302 | { |
---|
[2882] | 303 | info->m_numConstraintRows++; // limit 3rd anguar as well |
---|
| 304 | info->nub--; |
---|
[1963] | 305 | } |
---|
[8351] | 306 | |
---|
[1963] | 307 | } |
---|
[8351] | 308 | } |
---|
[1963] | 309 | |
---|
[8351] | 310 | void btHingeConstraint::getInfo1NonVirtual(btConstraintInfo1* info) |
---|
| 311 | { |
---|
| 312 | if (m_useSolveConstraintObsolete) |
---|
| 313 | { |
---|
| 314 | info->m_numConstraintRows = 0; |
---|
| 315 | info->nub = 0; |
---|
| 316 | } |
---|
| 317 | else |
---|
| 318 | { |
---|
| 319 | //always add the 'limit' row, to avoid computation (data is not available yet) |
---|
| 320 | info->m_numConstraintRows = 6; // Fixed 3 linear + 2 angular |
---|
| 321 | info->nub = 0; |
---|
| 322 | } |
---|
| 323 | } |
---|
[1963] | 324 | |
---|
[2882] | 325 | void btHingeConstraint::getInfo2 (btConstraintInfo2* info) |
---|
| 326 | { |
---|
[8351] | 327 | if(m_useOffsetForConstraintFrame) |
---|
| 328 | { |
---|
| 329 | getInfo2InternalUsingFrameOffset(info, m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform(),m_rbA.getAngularVelocity(),m_rbB.getAngularVelocity()); |
---|
| 330 | } |
---|
| 331 | else |
---|
| 332 | { |
---|
| 333 | getInfo2Internal(info, m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform(),m_rbA.getAngularVelocity(),m_rbB.getAngularVelocity()); |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | void btHingeConstraint::getInfo2NonVirtual (btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB) |
---|
| 339 | { |
---|
| 340 | ///the regular (virtual) implementation getInfo2 already performs 'testLimit' during getInfo1, so we need to do it now |
---|
| 341 | testLimit(transA,transB); |
---|
| 342 | |
---|
| 343 | getInfo2Internal(info,transA,transB,angVelA,angVelB); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | void btHingeConstraint::getInfo2Internal(btConstraintInfo2* info, const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB) |
---|
| 348 | { |
---|
| 349 | |
---|
[2882] | 350 | btAssert(!m_useSolveConstraintObsolete); |
---|
[8351] | 351 | int i, skip = info->rowskip; |
---|
[2882] | 352 | // transforms in world space |
---|
[8351] | 353 | btTransform trA = transA*m_rbAFrame; |
---|
| 354 | btTransform trB = transB*m_rbBFrame; |
---|
[2882] | 355 | // pivot point |
---|
| 356 | btVector3 pivotAInW = trA.getOrigin(); |
---|
| 357 | btVector3 pivotBInW = trB.getOrigin(); |
---|
[8351] | 358 | #if 0 |
---|
| 359 | if (0) |
---|
| 360 | { |
---|
| 361 | for (i=0;i<6;i++) |
---|
| 362 | { |
---|
| 363 | info->m_J1linearAxis[i*skip]=0; |
---|
| 364 | info->m_J1linearAxis[i*skip+1]=0; |
---|
| 365 | info->m_J1linearAxis[i*skip+2]=0; |
---|
| 366 | |
---|
| 367 | info->m_J1angularAxis[i*skip]=0; |
---|
| 368 | info->m_J1angularAxis[i*skip+1]=0; |
---|
| 369 | info->m_J1angularAxis[i*skip+2]=0; |
---|
| 370 | |
---|
| 371 | info->m_J2angularAxis[i*skip]=0; |
---|
| 372 | info->m_J2angularAxis[i*skip+1]=0; |
---|
| 373 | info->m_J2angularAxis[i*skip+2]=0; |
---|
| 374 | |
---|
| 375 | info->m_constraintError[i*skip]=0.f; |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | #endif //#if 0 |
---|
[2882] | 379 | // linear (all fixed) |
---|
[8351] | 380 | |
---|
| 381 | if (!m_angularOnly) |
---|
[2882] | 382 | { |
---|
[8351] | 383 | info->m_J1linearAxis[0] = 1; |
---|
| 384 | info->m_J1linearAxis[skip + 1] = 1; |
---|
| 385 | info->m_J1linearAxis[2 * skip + 2] = 1; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | btVector3 a1 = pivotAInW - transA.getOrigin(); |
---|
| 392 | { |
---|
[2882] | 393 | btVector3* angular0 = (btVector3*)(info->m_J1angularAxis); |
---|
[8351] | 394 | btVector3* angular1 = (btVector3*)(info->m_J1angularAxis + skip); |
---|
| 395 | btVector3* angular2 = (btVector3*)(info->m_J1angularAxis + 2 * skip); |
---|
[2882] | 396 | btVector3 a1neg = -a1; |
---|
| 397 | a1neg.getSkewSymmetricMatrix(angular0,angular1,angular2); |
---|
| 398 | } |
---|
[8351] | 399 | btVector3 a2 = pivotBInW - transB.getOrigin(); |
---|
[2882] | 400 | { |
---|
| 401 | btVector3* angular0 = (btVector3*)(info->m_J2angularAxis); |
---|
[8351] | 402 | btVector3* angular1 = (btVector3*)(info->m_J2angularAxis + skip); |
---|
| 403 | btVector3* angular2 = (btVector3*)(info->m_J2angularAxis + 2 * skip); |
---|
[2882] | 404 | a2.getSkewSymmetricMatrix(angular0,angular1,angular2); |
---|
| 405 | } |
---|
| 406 | // linear RHS |
---|
| 407 | btScalar k = info->fps * info->erp; |
---|
[8351] | 408 | if (!m_angularOnly) |
---|
| 409 | { |
---|
| 410 | for(i = 0; i < 3; i++) |
---|
| 411 | { |
---|
| 412 | info->m_constraintError[i * skip] = k * (pivotBInW[i] - pivotAInW[i]); |
---|
| 413 | } |
---|
| 414 | } |
---|
[2882] | 415 | // make rotations around X and Y equal |
---|
| 416 | // the hinge axis should be the only unconstrained |
---|
| 417 | // rotational axis, the angular velocity of the two bodies perpendicular to |
---|
| 418 | // the hinge axis should be equal. thus the constraint equations are |
---|
| 419 | // p*w1 - p*w2 = 0 |
---|
| 420 | // q*w1 - q*w2 = 0 |
---|
| 421 | // where p and q are unit vectors normal to the hinge axis, and w1 and w2 |
---|
| 422 | // are the angular velocity vectors of the two bodies. |
---|
| 423 | // get hinge axis (Z) |
---|
| 424 | btVector3 ax1 = trA.getBasis().getColumn(2); |
---|
| 425 | // get 2 orthos to hinge axis (X, Y) |
---|
| 426 | btVector3 p = trA.getBasis().getColumn(0); |
---|
| 427 | btVector3 q = trA.getBasis().getColumn(1); |
---|
| 428 | // set the two hinge angular rows |
---|
| 429 | int s3 = 3 * info->rowskip; |
---|
| 430 | int s4 = 4 * info->rowskip; |
---|
| 431 | |
---|
| 432 | info->m_J1angularAxis[s3 + 0] = p[0]; |
---|
| 433 | info->m_J1angularAxis[s3 + 1] = p[1]; |
---|
| 434 | info->m_J1angularAxis[s3 + 2] = p[2]; |
---|
| 435 | info->m_J1angularAxis[s4 + 0] = q[0]; |
---|
| 436 | info->m_J1angularAxis[s4 + 1] = q[1]; |
---|
| 437 | info->m_J1angularAxis[s4 + 2] = q[2]; |
---|
| 438 | |
---|
| 439 | info->m_J2angularAxis[s3 + 0] = -p[0]; |
---|
| 440 | info->m_J2angularAxis[s3 + 1] = -p[1]; |
---|
| 441 | info->m_J2angularAxis[s3 + 2] = -p[2]; |
---|
| 442 | info->m_J2angularAxis[s4 + 0] = -q[0]; |
---|
| 443 | info->m_J2angularAxis[s4 + 1] = -q[1]; |
---|
| 444 | info->m_J2angularAxis[s4 + 2] = -q[2]; |
---|
| 445 | // compute the right hand side of the constraint equation. set relative |
---|
| 446 | // body velocities along p and q to bring the hinge back into alignment. |
---|
| 447 | // if ax1,ax2 are the unit length hinge axes as computed from body1 and |
---|
| 448 | // body2, we need to rotate both bodies along the axis u = (ax1 x ax2). |
---|
| 449 | // if `theta' is the angle between ax1 and ax2, we need an angular velocity |
---|
| 450 | // along u to cover angle erp*theta in one step : |
---|
| 451 | // |angular_velocity| = angle/time = erp*theta / stepsize |
---|
| 452 | // = (erp*fps) * theta |
---|
| 453 | // angular_velocity = |angular_velocity| * (ax1 x ax2) / |ax1 x ax2| |
---|
| 454 | // = (erp*fps) * theta * (ax1 x ax2) / sin(theta) |
---|
| 455 | // ...as ax1 and ax2 are unit length. if theta is smallish, |
---|
| 456 | // theta ~= sin(theta), so |
---|
| 457 | // angular_velocity = (erp*fps) * (ax1 x ax2) |
---|
| 458 | // ax1 x ax2 is in the plane space of ax1, so we project the angular |
---|
| 459 | // velocity to p and q to find the right hand side. |
---|
| 460 | btVector3 ax2 = trB.getBasis().getColumn(2); |
---|
| 461 | btVector3 u = ax1.cross(ax2); |
---|
| 462 | info->m_constraintError[s3] = k * u.dot(p); |
---|
| 463 | info->m_constraintError[s4] = k * u.dot(q); |
---|
| 464 | // check angular limits |
---|
| 465 | int nrow = 4; // last filled row |
---|
| 466 | int srow; |
---|
| 467 | btScalar limit_err = btScalar(0.0); |
---|
| 468 | int limit = 0; |
---|
| 469 | if(getSolveLimit()) |
---|
| 470 | { |
---|
[8393] | 471 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 472 | limit_err = m_limit.getCorrection() * m_referenceSign; |
---|
| 473 | #else |
---|
| 474 | limit_err = m_correction * m_referenceSign; |
---|
| 475 | #endif |
---|
| 476 | limit = (limit_err > btScalar(0.0)) ? 1 : 2; |
---|
| 477 | |
---|
[2882] | 478 | } |
---|
| 479 | // if the hinge has joint limits or motor, add in the extra row |
---|
| 480 | int powered = 0; |
---|
| 481 | if(getEnableAngularMotor()) |
---|
| 482 | { |
---|
| 483 | powered = 1; |
---|
| 484 | } |
---|
| 485 | if(limit || powered) |
---|
| 486 | { |
---|
| 487 | nrow++; |
---|
| 488 | srow = nrow * info->rowskip; |
---|
| 489 | info->m_J1angularAxis[srow+0] = ax1[0]; |
---|
| 490 | info->m_J1angularAxis[srow+1] = ax1[1]; |
---|
| 491 | info->m_J1angularAxis[srow+2] = ax1[2]; |
---|
| 492 | |
---|
| 493 | info->m_J2angularAxis[srow+0] = -ax1[0]; |
---|
| 494 | info->m_J2angularAxis[srow+1] = -ax1[1]; |
---|
| 495 | info->m_J2angularAxis[srow+2] = -ax1[2]; |
---|
| 496 | |
---|
| 497 | btScalar lostop = getLowerLimit(); |
---|
| 498 | btScalar histop = getUpperLimit(); |
---|
| 499 | if(limit && (lostop == histop)) |
---|
| 500 | { // the joint motor is ineffective |
---|
| 501 | powered = 0; |
---|
| 502 | } |
---|
| 503 | info->m_constraintError[srow] = btScalar(0.0f); |
---|
[8351] | 504 | btScalar currERP = (m_flags & BT_HINGE_FLAGS_ERP_STOP) ? m_stopERP : info->erp; |
---|
[2882] | 505 | if(powered) |
---|
| 506 | { |
---|
[8351] | 507 | if(m_flags & BT_HINGE_FLAGS_CFM_NORM) |
---|
| 508 | { |
---|
| 509 | info->cfm[srow] = m_normalCFM; |
---|
| 510 | } |
---|
| 511 | btScalar mot_fact = getMotorFactor(m_hingeAngle, lostop, histop, m_motorTargetVelocity, info->fps * currERP); |
---|
[2882] | 512 | info->m_constraintError[srow] += mot_fact * m_motorTargetVelocity * m_referenceSign; |
---|
| 513 | info->m_lowerLimit[srow] = - m_maxMotorImpulse; |
---|
| 514 | info->m_upperLimit[srow] = m_maxMotorImpulse; |
---|
| 515 | } |
---|
| 516 | if(limit) |
---|
| 517 | { |
---|
[8351] | 518 | k = info->fps * currERP; |
---|
[2882] | 519 | info->m_constraintError[srow] += k * limit_err; |
---|
[8351] | 520 | if(m_flags & BT_HINGE_FLAGS_CFM_STOP) |
---|
| 521 | { |
---|
| 522 | info->cfm[srow] = m_stopCFM; |
---|
| 523 | } |
---|
[2882] | 524 | if(lostop == histop) |
---|
| 525 | { |
---|
| 526 | // limited low and high simultaneously |
---|
| 527 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 528 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 529 | } |
---|
| 530 | else if(limit == 1) |
---|
| 531 | { // low limit |
---|
| 532 | info->m_lowerLimit[srow] = 0; |
---|
| 533 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 534 | } |
---|
| 535 | else |
---|
| 536 | { // high limit |
---|
| 537 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 538 | info->m_upperLimit[srow] = 0; |
---|
| 539 | } |
---|
| 540 | // bounce (we'll use slider parameter abs(1.0 - m_dampingLimAng) for that) |
---|
[8393] | 541 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 542 | btScalar bounce = m_limit.getRelaxationFactor(); |
---|
| 543 | #else |
---|
[2882] | 544 | btScalar bounce = m_relaxationFactor; |
---|
[8393] | 545 | #endif |
---|
[2882] | 546 | if(bounce > btScalar(0.0)) |
---|
| 547 | { |
---|
[8351] | 548 | btScalar vel = angVelA.dot(ax1); |
---|
| 549 | vel -= angVelB.dot(ax1); |
---|
[2882] | 550 | // only apply bounce if the velocity is incoming, and if the |
---|
| 551 | // resulting c[] exceeds what we already have. |
---|
| 552 | if(limit == 1) |
---|
| 553 | { // low limit |
---|
| 554 | if(vel < 0) |
---|
| 555 | { |
---|
| 556 | btScalar newc = -bounce * vel; |
---|
| 557 | if(newc > info->m_constraintError[srow]) |
---|
| 558 | { |
---|
| 559 | info->m_constraintError[srow] = newc; |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | } |
---|
| 563 | else |
---|
| 564 | { // high limit - all those computations are reversed |
---|
| 565 | if(vel > 0) |
---|
| 566 | { |
---|
| 567 | btScalar newc = -bounce * vel; |
---|
| 568 | if(newc < info->m_constraintError[srow]) |
---|
| 569 | { |
---|
| 570 | info->m_constraintError[srow] = newc; |
---|
| 571 | } |
---|
| 572 | } |
---|
| 573 | } |
---|
| 574 | } |
---|
[8393] | 575 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 576 | info->m_constraintError[srow] *= m_limit.getBiasFactor(); |
---|
| 577 | #else |
---|
[2882] | 578 | info->m_constraintError[srow] *= m_biasFactor; |
---|
[8393] | 579 | #endif |
---|
[2882] | 580 | } // if(limit) |
---|
| 581 | } // if angular limit or powered |
---|
[1963] | 582 | } |
---|
| 583 | |
---|
[2882] | 584 | |
---|
[8393] | 585 | void btHingeConstraint::setFrames(const btTransform & frameA, const btTransform & frameB) |
---|
| 586 | { |
---|
| 587 | m_rbAFrame = frameA; |
---|
| 588 | m_rbBFrame = frameB; |
---|
| 589 | buildJacobian(); |
---|
| 590 | } |
---|
[1963] | 591 | |
---|
| 592 | |
---|
| 593 | void btHingeConstraint::updateRHS(btScalar timeStep) |
---|
| 594 | { |
---|
| 595 | (void)timeStep; |
---|
| 596 | |
---|
| 597 | } |
---|
| 598 | |
---|
[2882] | 599 | |
---|
[1963] | 600 | btScalar btHingeConstraint::getHingeAngle() |
---|
| 601 | { |
---|
[8351] | 602 | return getHingeAngle(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform()); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | btScalar btHingeConstraint::getHingeAngle(const btTransform& transA,const btTransform& transB) |
---|
| 606 | { |
---|
| 607 | const btVector3 refAxis0 = transA.getBasis() * m_rbAFrame.getBasis().getColumn(0); |
---|
| 608 | const btVector3 refAxis1 = transA.getBasis() * m_rbAFrame.getBasis().getColumn(1); |
---|
| 609 | const btVector3 swingAxis = transB.getBasis() * m_rbBFrame.getBasis().getColumn(1); |
---|
| 610 | // btScalar angle = btAtan2Fast(swingAxis.dot(refAxis0), swingAxis.dot(refAxis1)); |
---|
| 611 | btScalar angle = btAtan2(swingAxis.dot(refAxis0), swingAxis.dot(refAxis1)); |
---|
[2882] | 612 | return m_referenceSign * angle; |
---|
[1963] | 613 | } |
---|
| 614 | |
---|
[2882] | 615 | |
---|
| 616 | |
---|
[8351] | 617 | void btHingeConstraint::testLimit(const btTransform& transA,const btTransform& transB) |
---|
| 618 | { |
---|
| 619 | // Compute limit information |
---|
| 620 | m_hingeAngle = getHingeAngle(transA,transB); |
---|
[8393] | 621 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 622 | m_limit.test(m_hingeAngle); |
---|
| 623 | #else |
---|
[8351] | 624 | m_correction = btScalar(0.); |
---|
| 625 | m_limitSign = btScalar(0.); |
---|
| 626 | m_solveLimit = false; |
---|
| 627 | if (m_lowerLimit <= m_upperLimit) |
---|
| 628 | { |
---|
| 629 | m_hingeAngle = btAdjustAngleToLimits(m_hingeAngle, m_lowerLimit, m_upperLimit); |
---|
| 630 | if (m_hingeAngle <= m_lowerLimit) |
---|
| 631 | { |
---|
| 632 | m_correction = (m_lowerLimit - m_hingeAngle); |
---|
| 633 | m_limitSign = 1.0f; |
---|
| 634 | m_solveLimit = true; |
---|
| 635 | } |
---|
| 636 | else if (m_hingeAngle >= m_upperLimit) |
---|
| 637 | { |
---|
| 638 | m_correction = m_upperLimit - m_hingeAngle; |
---|
| 639 | m_limitSign = -1.0f; |
---|
| 640 | m_solveLimit = true; |
---|
| 641 | } |
---|
| 642 | } |
---|
[8393] | 643 | #endif |
---|
[8351] | 644 | return; |
---|
| 645 | } |
---|
| 646 | |
---|
[8393] | 647 | |
---|
[8351] | 648 | static btVector3 vHinge(0, 0, btScalar(1)); |
---|
| 649 | |
---|
| 650 | void btHingeConstraint::setMotorTarget(const btQuaternion& qAinB, btScalar dt) |
---|
| 651 | { |
---|
| 652 | // convert target from body to constraint space |
---|
| 653 | btQuaternion qConstraint = m_rbBFrame.getRotation().inverse() * qAinB * m_rbAFrame.getRotation(); |
---|
| 654 | qConstraint.normalize(); |
---|
| 655 | |
---|
| 656 | // extract "pure" hinge component |
---|
| 657 | btVector3 vNoHinge = quatRotate(qConstraint, vHinge); vNoHinge.normalize(); |
---|
| 658 | btQuaternion qNoHinge = shortestArcQuat(vHinge, vNoHinge); |
---|
| 659 | btQuaternion qHinge = qNoHinge.inverse() * qConstraint; |
---|
| 660 | qHinge.normalize(); |
---|
| 661 | |
---|
| 662 | // compute angular target, clamped to limits |
---|
| 663 | btScalar targetAngle = qHinge.getAngle(); |
---|
| 664 | if (targetAngle > SIMD_PI) // long way around. flip quat and recalculate. |
---|
| 665 | { |
---|
| 666 | qHinge = operator-(qHinge); |
---|
| 667 | targetAngle = qHinge.getAngle(); |
---|
| 668 | } |
---|
| 669 | if (qHinge.getZ() < 0) |
---|
| 670 | targetAngle = -targetAngle; |
---|
| 671 | |
---|
| 672 | setMotorTarget(targetAngle, dt); |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | void btHingeConstraint::setMotorTarget(btScalar targetAngle, btScalar dt) |
---|
| 676 | { |
---|
[8393] | 677 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 678 | m_limit.fit(targetAngle); |
---|
| 679 | #else |
---|
[8351] | 680 | if (m_lowerLimit < m_upperLimit) |
---|
| 681 | { |
---|
| 682 | if (targetAngle < m_lowerLimit) |
---|
| 683 | targetAngle = m_lowerLimit; |
---|
| 684 | else if (targetAngle > m_upperLimit) |
---|
| 685 | targetAngle = m_upperLimit; |
---|
| 686 | } |
---|
[8393] | 687 | #endif |
---|
[8351] | 688 | // compute angular velocity |
---|
| 689 | btScalar curAngle = getHingeAngle(m_rbA.getCenterOfMassTransform(),m_rbB.getCenterOfMassTransform()); |
---|
| 690 | btScalar dAngle = targetAngle - curAngle; |
---|
| 691 | m_motorTargetVelocity = dAngle / dt; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | |
---|
| 695 | |
---|
| 696 | void btHingeConstraint::getInfo2InternalUsingFrameOffset(btConstraintInfo2* info, const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB) |
---|
| 697 | { |
---|
| 698 | btAssert(!m_useSolveConstraintObsolete); |
---|
| 699 | int i, s = info->rowskip; |
---|
| 700 | // transforms in world space |
---|
| 701 | btTransform trA = transA*m_rbAFrame; |
---|
| 702 | btTransform trB = transB*m_rbBFrame; |
---|
| 703 | // pivot point |
---|
| 704 | btVector3 pivotAInW = trA.getOrigin(); |
---|
| 705 | btVector3 pivotBInW = trB.getOrigin(); |
---|
| 706 | #if 1 |
---|
| 707 | // difference between frames in WCS |
---|
| 708 | btVector3 ofs = trB.getOrigin() - trA.getOrigin(); |
---|
| 709 | // now get weight factors depending on masses |
---|
| 710 | btScalar miA = getRigidBodyA().getInvMass(); |
---|
| 711 | btScalar miB = getRigidBodyB().getInvMass(); |
---|
| 712 | bool hasStaticBody = (miA < SIMD_EPSILON) || (miB < SIMD_EPSILON); |
---|
| 713 | btScalar miS = miA + miB; |
---|
| 714 | btScalar factA, factB; |
---|
| 715 | if(miS > btScalar(0.f)) |
---|
| 716 | { |
---|
| 717 | factA = miB / miS; |
---|
| 718 | } |
---|
| 719 | else |
---|
| 720 | { |
---|
| 721 | factA = btScalar(0.5f); |
---|
| 722 | } |
---|
| 723 | factB = btScalar(1.0f) - factA; |
---|
| 724 | // get the desired direction of hinge axis |
---|
| 725 | // as weighted sum of Z-orthos of frameA and frameB in WCS |
---|
| 726 | btVector3 ax1A = trA.getBasis().getColumn(2); |
---|
| 727 | btVector3 ax1B = trB.getBasis().getColumn(2); |
---|
| 728 | btVector3 ax1 = ax1A * factA + ax1B * factB; |
---|
| 729 | ax1.normalize(); |
---|
| 730 | // fill first 3 rows |
---|
| 731 | // we want: velA + wA x relA == velB + wB x relB |
---|
| 732 | btTransform bodyA_trans = transA; |
---|
| 733 | btTransform bodyB_trans = transB; |
---|
| 734 | int s0 = 0; |
---|
| 735 | int s1 = s; |
---|
| 736 | int s2 = s * 2; |
---|
| 737 | int nrow = 2; // last filled row |
---|
| 738 | btVector3 tmpA, tmpB, relA, relB, p, q; |
---|
| 739 | // get vector from bodyB to frameB in WCS |
---|
| 740 | relB = trB.getOrigin() - bodyB_trans.getOrigin(); |
---|
| 741 | // get its projection to hinge axis |
---|
| 742 | btVector3 projB = ax1 * relB.dot(ax1); |
---|
| 743 | // get vector directed from bodyB to hinge axis (and orthogonal to it) |
---|
| 744 | btVector3 orthoB = relB - projB; |
---|
| 745 | // same for bodyA |
---|
| 746 | relA = trA.getOrigin() - bodyA_trans.getOrigin(); |
---|
| 747 | btVector3 projA = ax1 * relA.dot(ax1); |
---|
| 748 | btVector3 orthoA = relA - projA; |
---|
| 749 | btVector3 totalDist = projA - projB; |
---|
| 750 | // get offset vectors relA and relB |
---|
| 751 | relA = orthoA + totalDist * factA; |
---|
| 752 | relB = orthoB - totalDist * factB; |
---|
| 753 | // now choose average ortho to hinge axis |
---|
| 754 | p = orthoB * factA + orthoA * factB; |
---|
| 755 | btScalar len2 = p.length2(); |
---|
| 756 | if(len2 > SIMD_EPSILON) |
---|
| 757 | { |
---|
| 758 | p /= btSqrt(len2); |
---|
| 759 | } |
---|
| 760 | else |
---|
| 761 | { |
---|
| 762 | p = trA.getBasis().getColumn(1); |
---|
| 763 | } |
---|
| 764 | // make one more ortho |
---|
| 765 | q = ax1.cross(p); |
---|
| 766 | // fill three rows |
---|
| 767 | tmpA = relA.cross(p); |
---|
| 768 | tmpB = relB.cross(p); |
---|
| 769 | for (i=0; i<3; i++) info->m_J1angularAxis[s0+i] = tmpA[i]; |
---|
| 770 | for (i=0; i<3; i++) info->m_J2angularAxis[s0+i] = -tmpB[i]; |
---|
| 771 | tmpA = relA.cross(q); |
---|
| 772 | tmpB = relB.cross(q); |
---|
| 773 | if(hasStaticBody && getSolveLimit()) |
---|
| 774 | { // to make constraint between static and dynamic objects more rigid |
---|
| 775 | // remove wA (or wB) from equation if angular limit is hit |
---|
| 776 | tmpB *= factB; |
---|
| 777 | tmpA *= factA; |
---|
| 778 | } |
---|
| 779 | for (i=0; i<3; i++) info->m_J1angularAxis[s1+i] = tmpA[i]; |
---|
| 780 | for (i=0; i<3; i++) info->m_J2angularAxis[s1+i] = -tmpB[i]; |
---|
| 781 | tmpA = relA.cross(ax1); |
---|
| 782 | tmpB = relB.cross(ax1); |
---|
| 783 | if(hasStaticBody) |
---|
| 784 | { // to make constraint between static and dynamic objects more rigid |
---|
| 785 | // remove wA (or wB) from equation |
---|
| 786 | tmpB *= factB; |
---|
| 787 | tmpA *= factA; |
---|
| 788 | } |
---|
| 789 | for (i=0; i<3; i++) info->m_J1angularAxis[s2+i] = tmpA[i]; |
---|
| 790 | for (i=0; i<3; i++) info->m_J2angularAxis[s2+i] = -tmpB[i]; |
---|
| 791 | |
---|
| 792 | btScalar k = info->fps * info->erp; |
---|
| 793 | |
---|
| 794 | if (!m_angularOnly) |
---|
| 795 | { |
---|
| 796 | for (i=0; i<3; i++) info->m_J1linearAxis[s0+i] = p[i]; |
---|
| 797 | for (i=0; i<3; i++) info->m_J1linearAxis[s1+i] = q[i]; |
---|
| 798 | for (i=0; i<3; i++) info->m_J1linearAxis[s2+i] = ax1[i]; |
---|
| 799 | |
---|
| 800 | // compute three elements of right hand side |
---|
| 801 | |
---|
| 802 | btScalar rhs = k * p.dot(ofs); |
---|
| 803 | info->m_constraintError[s0] = rhs; |
---|
| 804 | rhs = k * q.dot(ofs); |
---|
| 805 | info->m_constraintError[s1] = rhs; |
---|
| 806 | rhs = k * ax1.dot(ofs); |
---|
| 807 | info->m_constraintError[s2] = rhs; |
---|
| 808 | } |
---|
| 809 | // the hinge axis should be the only unconstrained |
---|
| 810 | // rotational axis, the angular velocity of the two bodies perpendicular to |
---|
| 811 | // the hinge axis should be equal. thus the constraint equations are |
---|
| 812 | // p*w1 - p*w2 = 0 |
---|
| 813 | // q*w1 - q*w2 = 0 |
---|
| 814 | // where p and q are unit vectors normal to the hinge axis, and w1 and w2 |
---|
| 815 | // are the angular velocity vectors of the two bodies. |
---|
| 816 | int s3 = 3 * s; |
---|
| 817 | int s4 = 4 * s; |
---|
| 818 | info->m_J1angularAxis[s3 + 0] = p[0]; |
---|
| 819 | info->m_J1angularAxis[s3 + 1] = p[1]; |
---|
| 820 | info->m_J1angularAxis[s3 + 2] = p[2]; |
---|
| 821 | info->m_J1angularAxis[s4 + 0] = q[0]; |
---|
| 822 | info->m_J1angularAxis[s4 + 1] = q[1]; |
---|
| 823 | info->m_J1angularAxis[s4 + 2] = q[2]; |
---|
| 824 | |
---|
| 825 | info->m_J2angularAxis[s3 + 0] = -p[0]; |
---|
| 826 | info->m_J2angularAxis[s3 + 1] = -p[1]; |
---|
| 827 | info->m_J2angularAxis[s3 + 2] = -p[2]; |
---|
| 828 | info->m_J2angularAxis[s4 + 0] = -q[0]; |
---|
| 829 | info->m_J2angularAxis[s4 + 1] = -q[1]; |
---|
| 830 | info->m_J2angularAxis[s4 + 2] = -q[2]; |
---|
| 831 | // compute the right hand side of the constraint equation. set relative |
---|
| 832 | // body velocities along p and q to bring the hinge back into alignment. |
---|
| 833 | // if ax1A,ax1B are the unit length hinge axes as computed from bodyA and |
---|
| 834 | // bodyB, we need to rotate both bodies along the axis u = (ax1 x ax2). |
---|
| 835 | // if "theta" is the angle between ax1 and ax2, we need an angular velocity |
---|
| 836 | // along u to cover angle erp*theta in one step : |
---|
| 837 | // |angular_velocity| = angle/time = erp*theta / stepsize |
---|
| 838 | // = (erp*fps) * theta |
---|
| 839 | // angular_velocity = |angular_velocity| * (ax1 x ax2) / |ax1 x ax2| |
---|
| 840 | // = (erp*fps) * theta * (ax1 x ax2) / sin(theta) |
---|
| 841 | // ...as ax1 and ax2 are unit length. if theta is smallish, |
---|
| 842 | // theta ~= sin(theta), so |
---|
| 843 | // angular_velocity = (erp*fps) * (ax1 x ax2) |
---|
| 844 | // ax1 x ax2 is in the plane space of ax1, so we project the angular |
---|
| 845 | // velocity to p and q to find the right hand side. |
---|
| 846 | k = info->fps * info->erp; |
---|
| 847 | btVector3 u = ax1A.cross(ax1B); |
---|
| 848 | info->m_constraintError[s3] = k * u.dot(p); |
---|
| 849 | info->m_constraintError[s4] = k * u.dot(q); |
---|
| 850 | #endif |
---|
| 851 | // check angular limits |
---|
| 852 | nrow = 4; // last filled row |
---|
| 853 | int srow; |
---|
| 854 | btScalar limit_err = btScalar(0.0); |
---|
| 855 | int limit = 0; |
---|
| 856 | if(getSolveLimit()) |
---|
| 857 | { |
---|
[8393] | 858 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 859 | limit_err = m_limit.getCorrection() * m_referenceSign; |
---|
| 860 | #else |
---|
| 861 | limit_err = m_correction * m_referenceSign; |
---|
| 862 | #endif |
---|
| 863 | limit = (limit_err > btScalar(0.0)) ? 1 : 2; |
---|
| 864 | |
---|
[8351] | 865 | } |
---|
| 866 | // if the hinge has joint limits or motor, add in the extra row |
---|
| 867 | int powered = 0; |
---|
| 868 | if(getEnableAngularMotor()) |
---|
| 869 | { |
---|
| 870 | powered = 1; |
---|
| 871 | } |
---|
| 872 | if(limit || powered) |
---|
| 873 | { |
---|
| 874 | nrow++; |
---|
| 875 | srow = nrow * info->rowskip; |
---|
| 876 | info->m_J1angularAxis[srow+0] = ax1[0]; |
---|
| 877 | info->m_J1angularAxis[srow+1] = ax1[1]; |
---|
| 878 | info->m_J1angularAxis[srow+2] = ax1[2]; |
---|
| 879 | |
---|
| 880 | info->m_J2angularAxis[srow+0] = -ax1[0]; |
---|
| 881 | info->m_J2angularAxis[srow+1] = -ax1[1]; |
---|
| 882 | info->m_J2angularAxis[srow+2] = -ax1[2]; |
---|
| 883 | |
---|
| 884 | btScalar lostop = getLowerLimit(); |
---|
| 885 | btScalar histop = getUpperLimit(); |
---|
| 886 | if(limit && (lostop == histop)) |
---|
| 887 | { // the joint motor is ineffective |
---|
| 888 | powered = 0; |
---|
| 889 | } |
---|
| 890 | info->m_constraintError[srow] = btScalar(0.0f); |
---|
| 891 | btScalar currERP = (m_flags & BT_HINGE_FLAGS_ERP_STOP) ? m_stopERP : info->erp; |
---|
| 892 | if(powered) |
---|
| 893 | { |
---|
| 894 | if(m_flags & BT_HINGE_FLAGS_CFM_NORM) |
---|
| 895 | { |
---|
| 896 | info->cfm[srow] = m_normalCFM; |
---|
| 897 | } |
---|
| 898 | btScalar mot_fact = getMotorFactor(m_hingeAngle, lostop, histop, m_motorTargetVelocity, info->fps * currERP); |
---|
| 899 | info->m_constraintError[srow] += mot_fact * m_motorTargetVelocity * m_referenceSign; |
---|
| 900 | info->m_lowerLimit[srow] = - m_maxMotorImpulse; |
---|
| 901 | info->m_upperLimit[srow] = m_maxMotorImpulse; |
---|
| 902 | } |
---|
| 903 | if(limit) |
---|
| 904 | { |
---|
| 905 | k = info->fps * currERP; |
---|
| 906 | info->m_constraintError[srow] += k * limit_err; |
---|
| 907 | if(m_flags & BT_HINGE_FLAGS_CFM_STOP) |
---|
| 908 | { |
---|
| 909 | info->cfm[srow] = m_stopCFM; |
---|
| 910 | } |
---|
| 911 | if(lostop == histop) |
---|
| 912 | { |
---|
| 913 | // limited low and high simultaneously |
---|
| 914 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 915 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 916 | } |
---|
| 917 | else if(limit == 1) |
---|
| 918 | { // low limit |
---|
| 919 | info->m_lowerLimit[srow] = 0; |
---|
| 920 | info->m_upperLimit[srow] = SIMD_INFINITY; |
---|
| 921 | } |
---|
| 922 | else |
---|
| 923 | { // high limit |
---|
| 924 | info->m_lowerLimit[srow] = -SIMD_INFINITY; |
---|
| 925 | info->m_upperLimit[srow] = 0; |
---|
| 926 | } |
---|
| 927 | // bounce (we'll use slider parameter abs(1.0 - m_dampingLimAng) for that) |
---|
[8393] | 928 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 929 | btScalar bounce = m_limit.getRelaxationFactor(); |
---|
| 930 | #else |
---|
[8351] | 931 | btScalar bounce = m_relaxationFactor; |
---|
[8393] | 932 | #endif |
---|
[8351] | 933 | if(bounce > btScalar(0.0)) |
---|
| 934 | { |
---|
| 935 | btScalar vel = angVelA.dot(ax1); |
---|
| 936 | vel -= angVelB.dot(ax1); |
---|
| 937 | // only apply bounce if the velocity is incoming, and if the |
---|
| 938 | // resulting c[] exceeds what we already have. |
---|
| 939 | if(limit == 1) |
---|
| 940 | { // low limit |
---|
| 941 | if(vel < 0) |
---|
| 942 | { |
---|
| 943 | btScalar newc = -bounce * vel; |
---|
| 944 | if(newc > info->m_constraintError[srow]) |
---|
| 945 | { |
---|
| 946 | info->m_constraintError[srow] = newc; |
---|
| 947 | } |
---|
| 948 | } |
---|
| 949 | } |
---|
| 950 | else |
---|
| 951 | { // high limit - all those computations are reversed |
---|
| 952 | if(vel > 0) |
---|
| 953 | { |
---|
| 954 | btScalar newc = -bounce * vel; |
---|
| 955 | if(newc < info->m_constraintError[srow]) |
---|
| 956 | { |
---|
| 957 | info->m_constraintError[srow] = newc; |
---|
| 958 | } |
---|
| 959 | } |
---|
| 960 | } |
---|
| 961 | } |
---|
[8393] | 962 | #ifdef _BT_USE_CENTER_LIMIT_ |
---|
| 963 | info->m_constraintError[srow] *= m_limit.getBiasFactor(); |
---|
| 964 | #else |
---|
[8351] | 965 | info->m_constraintError[srow] *= m_biasFactor; |
---|
[8393] | 966 | #endif |
---|
[8351] | 967 | } // if(limit) |
---|
| 968 | } // if angular limit or powered |
---|
| 969 | } |
---|
| 970 | |
---|
| 971 | |
---|
| 972 | ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5). |
---|
| 973 | ///If no axis is provided, it uses the default axis for this constraint. |
---|
| 974 | void btHingeConstraint::setParam(int num, btScalar value, int axis) |
---|
| 975 | { |
---|
| 976 | if((axis == -1) || (axis == 5)) |
---|
| 977 | { |
---|
| 978 | switch(num) |
---|
| 979 | { |
---|
| 980 | case BT_CONSTRAINT_STOP_ERP : |
---|
| 981 | m_stopERP = value; |
---|
| 982 | m_flags |= BT_HINGE_FLAGS_ERP_STOP; |
---|
| 983 | break; |
---|
| 984 | case BT_CONSTRAINT_STOP_CFM : |
---|
| 985 | m_stopCFM = value; |
---|
| 986 | m_flags |= BT_HINGE_FLAGS_CFM_STOP; |
---|
| 987 | break; |
---|
| 988 | case BT_CONSTRAINT_CFM : |
---|
| 989 | m_normalCFM = value; |
---|
| 990 | m_flags |= BT_HINGE_FLAGS_CFM_NORM; |
---|
| 991 | break; |
---|
| 992 | default : |
---|
| 993 | btAssertConstrParams(0); |
---|
| 994 | } |
---|
| 995 | } |
---|
| 996 | else |
---|
| 997 | { |
---|
| 998 | btAssertConstrParams(0); |
---|
| 999 | } |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | ///return the local value of parameter |
---|
| 1003 | btScalar btHingeConstraint::getParam(int num, int axis) const |
---|
| 1004 | { |
---|
| 1005 | btScalar retVal = 0; |
---|
| 1006 | if((axis == -1) || (axis == 5)) |
---|
| 1007 | { |
---|
| 1008 | switch(num) |
---|
| 1009 | { |
---|
| 1010 | case BT_CONSTRAINT_STOP_ERP : |
---|
| 1011 | btAssertConstrParams(m_flags & BT_HINGE_FLAGS_ERP_STOP); |
---|
| 1012 | retVal = m_stopERP; |
---|
| 1013 | break; |
---|
| 1014 | case BT_CONSTRAINT_STOP_CFM : |
---|
| 1015 | btAssertConstrParams(m_flags & BT_HINGE_FLAGS_CFM_STOP); |
---|
| 1016 | retVal = m_stopCFM; |
---|
| 1017 | break; |
---|
| 1018 | case BT_CONSTRAINT_CFM : |
---|
| 1019 | btAssertConstrParams(m_flags & BT_HINGE_FLAGS_CFM_NORM); |
---|
| 1020 | retVal = m_normalCFM; |
---|
| 1021 | break; |
---|
| 1022 | default : |
---|
| 1023 | btAssertConstrParams(0); |
---|
| 1024 | } |
---|
| 1025 | } |
---|
| 1026 | else |
---|
| 1027 | { |
---|
| 1028 | btAssertConstrParams(0); |
---|
| 1029 | } |
---|
| 1030 | return retVal; |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | |
---|