btGeneric6DofSpringConstraint.cpp

Go to the documentation of this file.
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         for(int i = 0; i < 6; i++)
00025         {
00026                 m_springEnabled[i] = false;
00027                 m_equilibriumPoint[i] = btScalar(0.f);
00028                 m_springStiffness[i] = btScalar(0.f);
00029                 m_springDamping[i] = btScalar(1.f);
00030         }
00031 }
00032 
00033 
00034 void btGeneric6DofSpringConstraint::enableSpring(int index, bool onOff)
00035 {
00036         btAssert((index >= 0) && (index < 6));
00037         m_springEnabled[index] = onOff;
00038         if(index < 3)
00039         {
00040                 m_linearLimits.m_enableMotor[index] = onOff;
00041         }
00042         else
00043         {
00044                 m_angularLimits[index - 3].m_enableMotor = onOff;
00045         }
00046 }
00047 
00048 
00049 
00050 void btGeneric6DofSpringConstraint::setStiffness(int index, btScalar stiffness)
00051 {
00052         btAssert((index >= 0) && (index < 6));
00053         m_springStiffness[index] = stiffness;
00054 }
00055 
00056 
00057 void btGeneric6DofSpringConstraint::setDamping(int index, btScalar damping)
00058 {
00059         btAssert((index >= 0) && (index < 6));
00060         m_springDamping[index] = damping;
00061 }
00062 
00063 
00064 void btGeneric6DofSpringConstraint::setEquilibriumPoint()
00065 {
00066         calculateTransforms();
00067         int i;
00068 
00069         for( i = 0; i < 3; i++)
00070         {
00071                 m_equilibriumPoint[i] = m_calculatedLinearDiff[i];
00072         }
00073         for(i = 0; i < 3; i++)
00074         {
00075                 m_equilibriumPoint[i + 3] = m_calculatedAxisAngleDiff[i];
00076         }
00077 }
00078 
00079 
00080 
00081 void btGeneric6DofSpringConstraint::setEquilibriumPoint(int index)
00082 {
00083         btAssert((index >= 0) && (index < 6));
00084         calculateTransforms();
00085         if(index < 3)
00086         {
00087                 m_equilibriumPoint[index] = m_calculatedLinearDiff[index];
00088         }
00089         else
00090         {
00091                 m_equilibriumPoint[index] = m_calculatedAxisAngleDiff[index - 3];
00092         }
00093 }
00094 
00095 
00096 
00097 void btGeneric6DofSpringConstraint::internalUpdateSprings(btConstraintInfo2* info)
00098 {
00099         // it is assumed that calculateTransforms() have been called before this call
00100         int i;
00101         btVector3 relVel = m_rbB.getLinearVelocity() - m_rbA.getLinearVelocity();
00102         for(i = 0; i < 3; i++)
00103         {
00104                 if(m_springEnabled[i])
00105                 {
00106                         // get current position of constraint
00107                         btScalar currPos = m_calculatedLinearDiff[i];
00108                         // calculate difference
00109                         btScalar delta = currPos - m_equilibriumPoint[i];
00110                         // spring force is (delta * m_stiffness) according to Hooke's Law
00111                         btScalar force = delta * m_springStiffness[i];
00112                         btScalar velFactor = info->fps * m_springDamping[i] / btScalar(info->m_numIterations);
00113                         m_linearLimits.m_targetVelocity[i] =  velFactor * force;
00114                         m_linearLimits.m_maxMotorForce[i] =  btFabs(force) / info->fps;
00115                 }
00116         }
00117         for(i = 0; i < 3; i++)
00118         {
00119                 if(m_springEnabled[i + 3])
00120                 {
00121                         // get current position of constraint
00122                         btScalar currPos = m_calculatedAxisAngleDiff[i];
00123                         // calculate difference
00124                         btScalar delta = currPos - m_equilibriumPoint[i+3];
00125                         // spring force is (-delta * m_stiffness) according to Hooke's Law
00126                         btScalar force = -delta * m_springStiffness[i+3];
00127                         btScalar velFactor = info->fps * m_springDamping[i+3] / btScalar(info->m_numIterations);
00128                         m_angularLimits[i].m_targetVelocity = velFactor * force;
00129                         m_angularLimits[i].m_maxMotorForce = btFabs(force) / info->fps;
00130                 }
00131         }
00132 }
00133 
00134 
00135 void btGeneric6DofSpringConstraint::getInfo2(btConstraintInfo2* info)
00136 {
00137         // this will be called by constraint solver at the constraint setup stage
00138         // set current motor parameters
00139         internalUpdateSprings(info);
00140         // do the rest of job for constraint setup
00141         btGeneric6DofConstraint::getInfo2(info);
00142 }
00143 
00144 
00145 
00146 

Generated on Mon Feb 15 22:17:04 2010 for Bullet Collision Detection & Physics Library by  doxygen 1.6.1