[7983] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org |
---|
| 3 | Copyright (C) 2006, 2007 Sony Computer Entertainment Inc. |
---|
| 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 | #include "btGeneric6DofSpringConstraint.h" |
---|
| 17 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 18 | #include "LinearMath/btTransformUtil.h" |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA) |
---|
| 22 | : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA) |
---|
| 23 | { |
---|
| 24 | for(int i = 0; i < 6; i++) |
---|
| 25 | { |
---|
| 26 | m_springEnabled[i] = false; |
---|
| 27 | m_equilibriumPoint[i] = btScalar(0.f); |
---|
| 28 | m_springStiffness[i] = btScalar(0.f); |
---|
| 29 | m_springDamping[i] = btScalar(1.f); |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff) |
---|
| 35 | { |
---|
| 36 | btAssert((index >= 0) && (index < 6)); |
---|
| 37 | m_springEnabled[index] = onOff; |
---|
| 38 | if(index < 3) |
---|
| 39 | { |
---|
| 40 | m_linearLimits.m_enableMotor[index] = onOff; |
---|
| 41 | } |
---|
| 42 | else |
---|
| 43 | { |
---|
| 44 | m_angularLimits[index - 3].m_enableMotor = onOff; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness) |
---|
| 51 | { |
---|
| 52 | btAssert((index >= 0) && (index < 6)); |
---|
| 53 | m_springStiffness[index] = stiffness; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping) |
---|
| 58 | { |
---|
| 59 | btAssert((index >= 0) && (index < 6)); |
---|
| 60 | m_springDamping[index] = damping; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | void btGeneric6DofSpringConstraint::setEquilibriumPoint() |
---|
| 65 | { |
---|
| 66 | calculateTransforms(); |
---|
| 67 | int i; |
---|
| 68 | |
---|
| 69 | for( i = 0; i < 3; i++) |
---|
| 70 | { |
---|
| 71 | m_equilibriumPoint[i] = m_calculatedLinearDiff[i]; |
---|
| 72 | } |
---|
| 73 | for(i = 0; i < 3; i++) |
---|
| 74 | { |
---|
| 75 | m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i]; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index) |
---|
| 82 | { |
---|
| 83 | btAssert((index >= 0) && (index < 6)); |
---|
| 84 | calculateTransforms(); |
---|
| 85 | if(index < 3) |
---|
| 86 | { |
---|
| 87 | m_equilibriumPoint[index] = m_calculatedLinearDiff[index]; |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
| 91 | m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3]; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index, btScalar val) |
---|
| 96 | { |
---|
| 97 | btAssert((index >= 0) && (index < 6)); |
---|
| 98 | m_equilibriumPoint[index] = val; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info) |
---|
| 103 | { |
---|
| 104 | // it is assumed that calculateTransforms() have been called before this call |
---|
| 105 | int i; |
---|
| 106 | btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity(); |
---|
| 107 | for(i = 0; i < 3; i++) |
---|
| 108 | { |
---|
| 109 | if(m_springEnabled[i]) |
---|
| 110 | { |
---|
| 111 | // get current position of constraint |
---|
| 112 | btScalar currPos = m_calculatedLinearDiff[i]; |
---|
| 113 | // calculate difference |
---|
| 114 | btScalar delta = currPos - m_equilibriumPoint[i]; |
---|
| 115 | // spring force is (delta * m_stiffness) according to Hooke's Law |
---|
| 116 | btScalar force = delta * m_springStiffness[i]; |
---|
| 117 | btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations); |
---|
| 118 | m_linearLimits.m_targetVelocity[i] = velFactor * force; |
---|
| 119 | m_linearLimits.m_maxMotorForce[i] = btFabs(force) / info->fps; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | for(i = 0; i < 3; i++) |
---|
| 123 | { |
---|
| 124 | if(m_springEnabled[i + 3]) |
---|
| 125 | { |
---|
| 126 | // get current position of constraint |
---|
| 127 | btScalar currPos = m_calculatedAxisAngleDiff[i]; |
---|
| 128 | // calculate difference |
---|
| 129 | btScalar delta = currPos - m_equilibriumPoint[i+3]; |
---|
| 130 | // spring force is (-delta * m_stiffness) according to Hooke's Law |
---|
| 131 | btScalar force = -delta * m_springStiffness[i+3]; |
---|
| 132 | btScalar velFactor = info->fps * m_springDamping[i+3] / btScalar(info->m_numIterations); |
---|
| 133 | m_angularLimits[i].m_targetVelocity = velFactor * force; |
---|
| 134 | m_angularLimits[i].m_maxMotorForce = btFabs(force) / info->fps; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info) |
---|
| 141 | { |
---|
| 142 | // this will be called by constraint solver at the constraint setup stage |
---|
| 143 | // set current motor parameters |
---|
| 144 | internalUpdateSprings(info); |
---|
| 145 | // do the rest of job for constraint setup |
---|
| 146 | btGeneric6DofConstraint::getInfo2(info); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|