btTypedConstraint.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "btTypedConstraint.h"
00018 #include "BulletDynamics/Dynamics/btRigidBody.h"
00019 #include "LinearMath/btSerializer.h"
00020
00021
00022 #define DEFAULT_DEBUGDRAW_SIZE btScalar(0.3f)
00023
00024 btTypedConstraint::btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA)
00025 :btTypedObject(type),
00026 m_userConstraintType(-1),
00027 m_userConstraintId(-1),
00028 m_needsFeedback(false),
00029 m_rbA(rbA),
00030 m_rbB(getFixedBody()),
00031 m_appliedImpulse(btScalar(0.)),
00032 m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE)
00033 {
00034 }
00035
00036
00037 btTypedConstraint::btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB)
00038 :btTypedObject(type),
00039 m_userConstraintType(-1),
00040 m_userConstraintId(-1),
00041 m_needsFeedback(false),
00042 m_rbA(rbA),
00043 m_rbB(rbB),
00044 m_appliedImpulse(btScalar(0.)),
00045 m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE)
00046 {
00047 }
00048
00049
00050
00051
00052 btScalar btTypedConstraint::getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact)
00053 {
00054 if(lowLim > uppLim)
00055 {
00056 return btScalar(1.0f);
00057 }
00058 else if(lowLim == uppLim)
00059 {
00060 return btScalar(0.0f);
00061 }
00062 btScalar lim_fact = btScalar(1.0f);
00063 btScalar delta_max = vel / timeFact;
00064 if(delta_max < btScalar(0.0f))
00065 {
00066 if((pos >= lowLim) && (pos < (lowLim - delta_max)))
00067 {
00068 lim_fact = (lowLim - pos) / delta_max;
00069 }
00070 else if(pos < lowLim)
00071 {
00072 lim_fact = btScalar(0.0f);
00073 }
00074 else
00075 {
00076 lim_fact = btScalar(1.0f);
00077 }
00078 }
00079 else if(delta_max > btScalar(0.0f))
00080 {
00081 if((pos <= uppLim) && (pos > (uppLim - delta_max)))
00082 {
00083 lim_fact = (uppLim - pos) / delta_max;
00084 }
00085 else if(pos > uppLim)
00086 {
00087 lim_fact = btScalar(0.0f);
00088 }
00089 else
00090 {
00091 lim_fact = btScalar(1.0f);
00092 }
00093 }
00094 else
00095 {
00096 lim_fact = btScalar(0.0f);
00097 }
00098 return lim_fact;
00099 }
00100
00102 const char* btTypedConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
00103 {
00104 btTypedConstraintData* tcd = (btTypedConstraintData*) dataBuffer;
00105
00106 tcd->m_rbA = (btRigidBodyData*)&m_rbA;
00107 tcd->m_rbB = (btRigidBodyData*)&m_rbB;
00108 tcd->m_name = (char*) serializer->findNameForPointer(this);
00109 if (tcd->m_name)
00110 {
00111 serializer->serializeName(tcd->m_name);
00112 }
00113
00114 tcd->m_objectType = m_objectType;
00115 tcd->m_needsFeedback = m_needsFeedback;
00116 tcd->m_userConstraintId =m_userConstraintId;
00117 tcd->m_userConstraintType =m_userConstraintType;
00118
00119 tcd->m_appliedImpulse = float(m_appliedImpulse);
00120 tcd->m_dbgDrawSize = float(m_dbgDrawSize );
00121
00122 tcd->m_disableCollisionsBetweenLinkedBodies = false;
00123
00124 int i;
00125 for (i=0;i<m_rbA.getNumConstraintRefs();i++)
00126 if (m_rbA.getConstraintRef(i) == this)
00127 tcd->m_disableCollisionsBetweenLinkedBodies = true;
00128 for (i=0;i<m_rbB.getNumConstraintRefs();i++)
00129 if (m_rbB.getConstraintRef(i) == this)
00130 tcd->m_disableCollisionsBetweenLinkedBodies = true;
00131
00132 return "btTypedConstraintData";
00133 }
00134
00135