Back to index

supertuxkart  0.5+dfsg1
btGeneric6DofConstraint.h
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
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 2007-09-09
00017 btGeneric6DofConstraint Refactored by Francisco León
00018 email: projectileman@yahoo.com
00019 http://gimpact.sf.net
00020 */
00021 
00022 
00023 #ifndef GENERIC_6DOF_CONSTRAINT_H
00024 #define GENERIC_6DOF_CONSTRAINT_H
00025 
00026 #include "LinearMath/btVector3.h"
00027 #include "btJacobianEntry.h"
00028 #include "btTypedConstraint.h"
00029 
00030 class btRigidBody;
00031 
00032 
00034 class btRotationalLimitMotor
00035 {
00036 public:
00039     btScalar m_loLimit;
00040     btScalar m_hiLimit;
00041     btScalar m_targetVelocity;
00042     btScalar m_maxMotorForce;
00043     btScalar m_maxLimitForce;
00044     btScalar m_damping;
00045     btScalar m_limitSoftness;
00046     btScalar m_ERP;
00047     btScalar m_bounce;
00048     bool m_enableMotor;
00049 
00051 
00054     btScalar m_currentLimitError;
00055     int m_currentLimit;
00056     btScalar m_accumulatedImpulse;
00058 
00059     btRotationalLimitMotor()
00060     {
00061        m_accumulatedImpulse = 0.f;
00062         m_targetVelocity = 0;
00063         m_maxMotorForce = 0.1f;
00064         m_maxLimitForce = 300.0f;
00065         m_loLimit = -SIMD_INFINITY;
00066         m_hiLimit = SIMD_INFINITY;
00067         m_ERP = 0.5f;
00068         m_bounce = 0.0f;
00069         m_damping = 1.0f;
00070         m_limitSoftness = 0.5f;
00071         m_currentLimit = 0;
00072         m_currentLimitError = 0;
00073         m_enableMotor = false;
00074     }
00075 
00076     btRotationalLimitMotor(const btRotationalLimitMotor & limot)
00077     {
00078         m_targetVelocity = limot.m_targetVelocity;
00079         m_maxMotorForce = limot.m_maxMotorForce;
00080         m_limitSoftness = limot.m_limitSoftness;
00081         m_loLimit = limot.m_loLimit;
00082         m_hiLimit = limot.m_hiLimit;
00083         m_ERP = limot.m_ERP;
00084         m_bounce = limot.m_bounce;
00085         m_currentLimit = limot.m_currentLimit;
00086         m_currentLimitError = limot.m_currentLimitError;
00087         m_enableMotor = limot.m_enableMotor;
00088     }
00089 
00090 
00091 
00093     bool isLimited()
00094     {
00095        if(m_loLimit>=m_hiLimit) return false;
00096        return true;
00097     }
00098 
00100     bool needApplyTorques()
00101     {
00102        if(m_currentLimit == 0 && m_enableMotor == false) return false;
00103        return true;
00104     }
00105 
00107 
00110        int testLimitValue(btScalar test_value);
00111 
00113     btScalar solveAngularLimits(btScalar timeStep,btVector3& axis, btScalar jacDiagABInv,btRigidBody * body0, btRigidBody * body1);
00114 
00115 
00116 };
00117 
00118 
00119 
00120 class btTranslationalLimitMotor
00121 {
00122 public:
00123        btVector3 m_lowerLimit;
00124     btVector3 m_upperLimit;
00125     btVector3 m_accumulatedImpulse;
00128     btScalar  m_limitSoftness;
00129     btScalar  m_damping;
00130     btScalar  m_restitution;
00131 
00132 
00133     btTranslationalLimitMotor()
00134     {
00135        m_lowerLimit.setValue(0.f,0.f,0.f);
00136        m_upperLimit.setValue(0.f,0.f,0.f);
00137        m_accumulatedImpulse.setValue(0.f,0.f,0.f);
00138 
00139        m_limitSoftness = 0.7f;
00140        m_damping = btScalar(1.0f);
00141        m_restitution = btScalar(0.5f);
00142     }
00143 
00144     btTranslationalLimitMotor(const btTranslationalLimitMotor & other )
00145     {
00146        m_lowerLimit = other.m_lowerLimit;
00147        m_upperLimit = other.m_upperLimit;
00148        m_accumulatedImpulse = other.m_accumulatedImpulse;
00149 
00150        m_limitSoftness = other.m_limitSoftness ;
00151        m_damping = other.m_damping;
00152        m_restitution = other.m_restitution;
00153     }
00154 
00156 
00162     inline bool      isLimited(int limitIndex)
00163     {
00164        return (m_upperLimit[limitIndex] >= m_lowerLimit[limitIndex]);
00165     }
00166 
00167 
00168     btScalar solveLinearAxis(
00169        btScalar timeStep,
00170         btScalar jacDiagABInv,
00171         btRigidBody& body1,const btVector3 &pointInA,
00172         btRigidBody& body2,const btVector3 &pointInB,
00173         int limit_index,
00174         const btVector3 & axis_normal_on_a);
00175 
00176 
00177 };
00178 
00180 
00213 class btGeneric6DofConstraint : public btTypedConstraint
00214 {
00215 protected:
00216 
00219        btTransform   m_frameInA;
00220     btTransform      m_frameInB;
00221 
00222 
00225     btJacobianEntry  m_jacLinear[3];
00226     btJacobianEntry  m_jacAng[3];
00227 
00228 
00231     btTranslationalLimitMotor m_linearLimits;
00233 
00234 
00237     btRotationalLimitMotor m_angularLimits[3];
00239 
00240 
00241 protected:
00244     btScalar m_timeStep;
00245     btTransform m_calculatedTransformA;
00246     btTransform m_calculatedTransformB;
00247     btVector3 m_calculatedAxisAngleDiff;
00248     btVector3 m_calculatedAxis[3];
00249     
00250     bool      m_useLinearReferenceFrameA;
00251     
00253 
00254     btGeneric6DofConstraint&       operator=(btGeneric6DofConstraint& other)
00255     {
00256         btAssert(0);
00257         (void) other;
00258         return *this;
00259     }
00260 
00261 
00262 
00263     void buildLinearJacobian(
00264         btJacobianEntry & jacLinear,const btVector3 & normalWorld,
00265         const btVector3 & pivotAInW,const btVector3 & pivotBInW);
00266 
00267     void buildAngularJacobian(btJacobianEntry & jacAngular,const btVector3 & jointAxisW);
00268 
00269 
00271     void calculateAngleInfo();
00272 
00273 
00274 
00275 public:
00276     btGeneric6DofConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA);
00277 
00278     btGeneric6DofConstraint();
00279 
00281 
00285     void calculateTransforms();
00286 
00288 
00291     const btTransform & getCalculatedTransformA() const
00292     {
00293        return m_calculatedTransformA;
00294     }
00295 
00297 
00300     const btTransform & getCalculatedTransformB() const
00301     {
00302        return m_calculatedTransformB;
00303     }
00304 
00305     const btTransform & getFrameOffsetA() const
00306     {
00307        return m_frameInA;
00308     }
00309 
00310     const btTransform & getFrameOffsetB() const
00311     {
00312        return m_frameInB;
00313     }
00314 
00315 
00316     btTransform & getFrameOffsetA()
00317     {
00318        return m_frameInA;
00319     }
00320 
00321     btTransform & getFrameOffsetB()
00322     {
00323        return m_frameInB;
00324     }
00325 
00326 
00328     virtual void     buildJacobian();
00329 
00330     virtual   void   solveConstraint(btScalar    timeStep);
00331 
00332     void      updateRHS(btScalar   timeStep);
00333 
00335 
00338     btVector3 getAxis(int axis_index) const;
00339 
00341 
00344     btScalar getAngle(int axis_index) const;
00345 
00347 
00351     bool testAngularLimitMotor(int axis_index);
00352 
00353     void      setLinearLowerLimit(const btVector3& linearLower)
00354     {
00355        m_linearLimits.m_lowerLimit = linearLower;
00356     }
00357 
00358     void      setLinearUpperLimit(const btVector3& linearUpper)
00359     {
00360        m_linearLimits.m_upperLimit = linearUpper;
00361     }
00362 
00363     void      setAngularLowerLimit(const btVector3& angularLower)
00364     {
00365         m_angularLimits[0].m_loLimit = angularLower.getX();
00366         m_angularLimits[1].m_loLimit = angularLower.getY();
00367         m_angularLimits[2].m_loLimit = angularLower.getZ();
00368     }
00369 
00370     void      setAngularUpperLimit(const btVector3& angularUpper)
00371     {
00372         m_angularLimits[0].m_hiLimit = angularUpper.getX();
00373         m_angularLimits[1].m_hiLimit = angularUpper.getY();
00374         m_angularLimits[2].m_hiLimit = angularUpper.getZ();
00375     }
00376 
00378     btRotationalLimitMotor * getRotationalLimitMotor(int index)
00379     {
00380        return &m_angularLimits[index];
00381     }
00382 
00384     btTranslationalLimitMotor * getTranslationalLimitMotor()
00385     {
00386        return &m_linearLimits;
00387     }
00388 
00389     //first 3 are linear, next 3 are angular
00390     void setLimit(int axis, btScalar lo, btScalar hi)
00391     {
00392        if(axis<3)
00393        {
00394               m_linearLimits.m_lowerLimit[axis] = lo;
00395               m_linearLimits.m_upperLimit[axis] = hi;
00396        }
00397        else
00398        {
00399               m_angularLimits[axis-3].m_loLimit = lo;
00400               m_angularLimits[axis-3].m_hiLimit = hi;
00401        }
00402     }
00403 
00405 
00411     bool      isLimited(int limitIndex)
00412     {
00413        if(limitIndex<3)
00414        {
00415                      return m_linearLimits.isLimited(limitIndex);
00416 
00417        }
00418         return m_angularLimits[limitIndex-3].isLimited();
00419     }
00420 
00421     const btRigidBody& getRigidBodyA() const
00422     {
00423         return m_rbA;
00424     }
00425     const btRigidBody& getRigidBodyB() const
00426     {
00427         return m_rbB;
00428     }
00429 
00430 
00431 };
00432 
00433 #endif //GENERIC_6DOF_CONSTRAINT_H