7 #include "BlMotorizedJoint.h"
8 #include "BlRigidBody.h"
9 #include "BlSimulator.h"
33 m_fltPrevBtJointPos = 0;
34 m_fltPrevJointPos = 0;
36 for(
int iIdx=0; iIdx<6; iIdx++)
48 m_lpVsSim =
dynamic_cast<BlSimulator *
>(m_lpThisAB->GetSimulator());
50 THROW_TEXT_ERROR(Osg_Err_lThisPointerNotDefined, Osg_Err_strThisPointerNotDefined,
"m_lpVsSim, " + m_lpThisAB->Name());
55 bool BlJoint::Physics_IsDefined()
63 void BlJoint::InitBaseJointPointers(RigidBody *lpParent, RigidBody *lpChild, ConstraintRelaxation **aryRelaxations,
int iDisallowSpringIndex)
66 THROW_ERROR(Al_Err_lParentNotDefined, Al_Err_strParentNotDefined);
69 THROW_ERROR(Al_Err_lChildNotDefined, Al_Err_strChildNotDefined);
71 m_lpBlParent =
dynamic_cast<BlRigidBody *
>(lpParent);
73 THROW_ERROR(Bl_Err_lUnableToConvertToBlRigidBody, Bl_Err_strUnableToConvertToBlRigidBody);
77 THROW_ERROR(Bl_Err_lUnableToConvertToBlRigidBody, Bl_Err_strUnableToConvertToBlRigidBody);
79 m_btParent = m_lpBlParent->Part();
80 m_btChild = m_lpBlChild->Part();
82 for(
int iIdx=0; iIdx<6; iIdx++)
84 if(aryRelaxations[iIdx])
93 if(iDisallowSpringIndex >= 0 && iDisallowSpringIndex <6 &&
m_aryBlRelaxations[iDisallowSpringIndex])
98 void BlJoint::SetLimitValues()
102 GetLimitsFromRelaxations(m_vLowerLinear, m_vUpperLinear, m_vLowerAngular, m_vUpperAngular);
104 m_bt6DofJoint->setLinearLowerLimit(m_vLowerLinear);
105 m_bt6DofJoint->setLinearUpperLimit(m_vUpperLinear);
106 m_bt6DofJoint->setAngularLowerLimit(m_vLowerAngular);
107 m_bt6DofJoint->setAngularUpperLimit(m_vUpperAngular);
111 void BlJoint::DeletePhysics(
bool bIncludeChildren)
116 if(GetBlSimulator() && GetBlSimulator()->DynamicsWorld())
118 GetBlSimulator()->DynamicsWorld()->removeConstraint(m_btJoint);
125 float BlJoint::GetCurrentBtPositionScaled()
127 float fltDistanceUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
128 float fltMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
130 float fltCurrentJointPos = GetCurrentBtPosition();
139 if(m_lpThisJoint->UsesRadians())
141 int iPrevPosSign =
Std_Sign(m_fltPrevBtJointPos);
143 if(
Std_Sign(fltCurrentJointPos) != iPrevPosSign && fabs(fltCurrentJointPos - m_fltPrevBtJointPos) > 0.1)
144 fltDelta = fltCurrentJointPos - (-m_fltPrevBtJointPos);
146 fltDelta = fltCurrentJointPos - m_fltPrevBtJointPos;
148 fltCurrentJointPos = m_lpThisJoint->JointPosition() + fltDelta;
151 if(!m_lpThisJoint->UsesRadians())
152 fltCurrentJointPos *= fltDistanceUnits;
154 return fltCurrentJointPos;
157 void BlJoint::Physics_CollectData()
159 OsgJoint::Physics_CollectData();
161 if(m_lpThisJoint && m_btJoint && m_lpThisJoint->GetSimulator())
163 float fltCurrentJointPos = GetCurrentBtPositionScaled();
164 float fltJointVel = (fltCurrentJointPos - m_fltPrevJointPos)/(m_lpThisJoint->GetSimulator()->PhysicsTimeStep());
169 m_lpThisJoint->JointPosition(fltCurrentJointPos);
170 m_lpThisJoint->JointVelocity(fltJointVel);
173 m_fltPrevBtJointPos = GetCurrentBtPosition();
174 m_fltPrevJointPos = fltCurrentJointPos;
178 void BlJoint::Physics_ResetSimulation()
182 m_lpThisJoint->WakeDynamics();
183 m_fltPrevBtJointPos = 0;
184 m_fltPrevJointPos = 0;
185 OsgJoint::Physics_ResetSimulation();
189 void BlJoint::CalculateRelativeJointMatrices(btTransform &mtJointRelToParent, btTransform &mtJointRelToChild)
191 CStdFPoint vRot(0, 0, 0);
192 CalculateRelativeJointMatrices(vRot, mtJointRelToParent,mtJointRelToChild);
195 void BlJoint::CalculateRelativeJointMatrices(CStdFPoint vAdditionalRot, btTransform &mtJointRelToParent, btTransform &mtJointRelToChild)
197 osg::Matrix mtParent = GetParentPhysicsWorldMatrix();
198 osg::Matrix mtChild = GetChildWorldMatrix();
199 CStdFPoint vPos1 = m_lpThisMI->Position();
200 CStdFPoint vRot1 = m_lpThisMI->Rotation() + vAdditionalRot;
201 osg::Matrix osgJointRelChild = SetupMatrix(vPos1, vRot1);
203 osg::Matrix mtJointMTFromChild = osgJointRelChild * mtChild;
204 osg::Matrix mtLocalRelToParent = mtJointMTFromChild * osg::Matrix::inverse(mtParent);
206 osg::Matrix mtChildCom = GetChildComMatrix(
true);
207 osg::Matrix mtJointRelChild = osgJointRelChild * mtChildCom;
209 mtJointRelToParent = osgbCollision::asBtTransform(mtLocalRelToParent);
210 mtJointRelToChild = osgbCollision::asBtTransform(mtJointRelChild);
213 void BlJoint::GetLimitsFromRelaxations(btVector3 &vLowerLinear, btVector3 &UpperLinear, btVector3 &vLowerAngular, btVector3 &vUpperAngular)
217 for(
int iIdx=0, iBlIdx=0; iIdx<3; iIdx++, iBlIdx++)
226 vLowerLinear[iIdx] = 0;
227 UpperLinear[iIdx] = 0;
231 for(
int iIdx=0, iBlIdx=3; iIdx<3; iIdx++, iBlIdx++)
240 vLowerAngular[iIdx] = 0;
241 vUpperAngular[iIdx] = 0;
A common class for all rigid body data specific to vortex.
int Std_Sign(float fltVal)
Determines the sign of a number.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual float MinLimit()
Gets the minimum value that this relaxation can move.
BlConstraintRelaxation * m_aryBlRelaxations[6]
The bullet relaxation for the primary displacement relaxation.
virtual bool DisallowSpringEnable()
Gets the disallow spring enable.
virtual float MaxLimit()
Gets the maximum value that this relaxation can move.