Blender V2.61 - r43446
|
00001 /* 00002 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org 00003 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc. 00004 00005 This software is provided 'as-is', without any express or implied warranty. 00006 In no event will the authors be held liable for any damages arising from the use of this software. 00007 Permission is granted to anyone to use this software for any purpose, 00008 including commercial applications, and to alter it and redistribute it freely, 00009 subject to the following restrictions: 00010 00011 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. 00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 00013 3. This notice may not be removed or altered from any source distribution. 00014 */ 00015 00016 #include "btGeneric6DofSpringConstraint.h" 00017 #include "BulletDynamics/Dynamics/btRigidBody.h" 00018 #include "LinearMath/btTransformUtil.h" 00019 00020 00021 btGeneric6DofSpringConstraint::btGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA) 00022 : btGeneric6DofConstraint(rbA, rbB, frameInA, frameInB, useLinearReferenceFrameA) 00023 { 00024 m_objectType = D6_SPRING_CONSTRAINT_TYPE; 00025 00026 for(int i = 0; i < 6; i++) 00027 { 00028 m_springEnabled[i] = false; 00029 m_equilibriumPoint[i] = btScalar(0.f); 00030 m_springStiffness[i] = btScalar(0.f); 00031 m_springDamping[i] = btScalar(1.f); 00032 } 00033 } 00034 00035 00036 void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff) 00037 { 00038 btAssert((index >= 0) && (index < 6)); 00039 m_springEnabled[index] = onOff; 00040 if(index < 3) 00041 { 00042 m_linearLimits.m_enableMotor[index] = onOff; 00043 } 00044 else 00045 { 00046 m_angularLimits[index - 3].m_enableMotor = onOff; 00047 } 00048 } 00049 00050 00051 00052 void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness) 00053 { 00054 btAssert((index >= 0) && (index < 6)); 00055 m_springStiffness[index] = stiffness; 00056 } 00057 00058 00059 void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping) 00060 { 00061 btAssert((index >= 0) && (index < 6)); 00062 m_springDamping[index] = damping; 00063 } 00064 00065 00066 void btGeneric6DofSpringConstraint::setEquilibriumPoint() 00067 { 00068 calculateTransforms(); 00069 int i; 00070 00071 for( i = 0; i < 3; i++) 00072 { 00073 m_equilibriumPoint[i] = m_calculatedLinearDiff[i]; 00074 } 00075 for(i = 0; i < 3; i++) 00076 { 00077 m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i]; 00078 } 00079 } 00080 00081 00082 00083 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index) 00084 { 00085 btAssert((index >= 0) && (index < 6)); 00086 calculateTransforms(); 00087 if(index < 3) 00088 { 00089 m_equilibriumPoint[index] = m_calculatedLinearDiff[index]; 00090 } 00091 else 00092 { 00093 m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3]; 00094 } 00095 } 00096 00097 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index, btScalar val) 00098 { 00099 btAssert((index >= 0) && (index < 6)); 00100 m_equilibriumPoint[index] = val; 00101 } 00102 00103 00104 void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info) 00105 { 00106 // it is assumed that calculateTransforms() have been called before this call 00107 int i; 00108 btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity(); 00109 for(i = 0; i < 3; i++) 00110 { 00111 if(m_springEnabled[i]) 00112 { 00113 // get current position of constraint 00114 btScalar currPos = m_calculatedLinearDiff[i]; 00115 // calculate difference 00116 btScalar delta = currPos - m_equilibriumPoint[i]; 00117 // spring force is (delta * m_stiffness) according to Hooke's Law 00118 btScalar force = delta * m_springStiffness[i]; 00119 btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations); 00120 m_linearLimits.m_targetVelocity[i] = velFactor * force; 00121 m_linearLimits.m_maxMotorForce[i] = btFabs(force) / info->fps; 00122 } 00123 } 00124 for(i = 0; i < 3; i++) 00125 { 00126 if(m_springEnabled[i + 3]) 00127 { 00128 // get current position of constraint 00129 btScalar currPos = m_calculatedAxisAngleDiff[i]; 00130 // calculate difference 00131 btScalar delta = currPos - m_equilibriumPoint[i+3]; 00132 // spring force is (-delta * m_stiffness) according to Hooke's Law 00133 btScalar force = -delta * m_springStiffness[i+3]; 00134 btScalar velFactor = info->fps * m_springDamping[i+3] / btScalar(info->m_numIterations); 00135 m_angularLimits[i].m_targetVelocity = velFactor * force; 00136 m_angularLimits[i].m_maxMotorForce = btFabs(force) / info->fps; 00137 } 00138 } 00139 } 00140 00141 00142 void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info) 00143 { 00144 // this will be called by constraint solver at the constraint setup stage 00145 // set current motor parameters 00146 internalUpdateSprings(info); 00147 // do the rest of job for constraint setup 00148 btGeneric6DofConstraint::getInfo2(info); 00149 } 00150 00151 00152 void btGeneric6DofSpringConstraint::setAxis(const btVector3& axis1,const btVector3& axis2) 00153 { 00154 btVector3 zAxis = axis1.normalized(); 00155 btVector3 yAxis = axis2.normalized(); 00156 btVector3 xAxis = yAxis.cross(zAxis); // we want right coordinate system 00157 00158 btTransform frameInW; 00159 frameInW.setIdentity(); 00160 frameInW.getBasis().setValue( xAxis[0], yAxis[0], zAxis[0], 00161 xAxis[1], yAxis[1], zAxis[1], 00162 xAxis[2], yAxis[2], zAxis[2]); 00163 00164 // now get constraint frame in local coordinate systems 00165 m_frameInA = m_rbA.getCenterOfMassTransform().inverse() * frameInW; 00166 m_frameInB = m_rbB.getCenterOfMassTransform().inverse() * frameInW; 00167 00168 calculateTransforms(); 00169 } 00170 00171 00172