7 #include "BlRigidBody.h"
8 #include "BlSimulator.h"
19 BlRigidBody::BlRigidBody()
21 m_btCompoundShape = NULL;
22 m_btCollisionShape = NULL;
23 m_btCollisionObject = NULL;
26 m_lpBulletData = NULL;
27 m_fltStaticMasses = 0;
28 m_eBodyType = BOX_SHAPE_PROXYTYPE;
33 for(
int iIdx=0; iIdx<3; iIdx++)
42 m_btStickyLock = NULL;
43 m_btStickyLock2 = NULL;
46 BlRigidBody::~BlRigidBody()
51 m_aryContactPoints.Clear();
55 {
Std_TraceMsg(0,
"Caught Error in desctructor of BlRigidBody\r\n",
"", -1,
false,
true);}
58 bool BlRigidBody::Physics_IsDefined()
60 if(m_btPart && m_btCollisionShape)
66 bool BlRigidBody::Physics_IsGeometryDefined()
68 if(m_btCollisionShape)
74 CStdFPoint BlRigidBody::Physics_GetCurrentPosition()
76 if(m_osgbMotion && m_lpThisMI)
78 m_osgWorldMatrix = m_osgMT->getMatrix();
81 osg::Vec3d vPos = m_osgWorldMatrix.getTrans();
82 m_lpThisMI->AbsolutePosition(vPos[0], vPos[1], vPos[2]);
83 return m_lpThisMI->AbsolutePosition();
92 osg::Matrix BlRigidBody::GetPhysicsWorldMatrix()
97 m_osgbMotion->getWorldTransform(trans);
98 osg::Matrix osgMT = osgbCollision::asOsgMatrix(trans);
103 osg::Matrix osgMatrix;
104 osgMatrix.makeIdentity();
113 m_lpVsSim =
dynamic_cast<BlSimulator *
>(m_lpThisAB->GetSimulator());
115 THROW_TEXT_ERROR(Osg_Err_lThisPointerNotDefined, Osg_Err_strThisPointerNotDefined,
"m_lpVsSim, " + m_lpThisAB->Name());
120 void BlRigidBody::Physics_UpdateNode()
127 OsgBody::UpdatePositionAndRotationFromMatrix();
131 if(m_lpThisRB->IsContactSensor())
132 ResetSensorCollisionGeom();
133 else if(m_lpThisRB->HasStaticJoint())
134 ResetStaticCollisionGeom();
136 ResetDynamicCollisionGeom();
140 void BlRigidBody::Physics_SetFreeze(
bool bVal)
142 ResizePhysicsGeometry();
145 void BlRigidBody::Physics_SetMass(
float fltVal)
147 ResizePhysicsGeometry();
150 float BlRigidBody::Physics_GetMass()
153 return m_lpThisRB->Mass();
158 float BlRigidBody::Physics_GetDensity()
162 float fltVolume = m_lpThisRB->Volume();
164 return m_lpThisRB->Mass()/fltVolume;
170 void BlRigidBody::Physics_SetMaterialID(std::string strID)
182 THROW_PARAM_ERROR(Bl_Err_lConvertingMaterialType, Bl_Err_strConvertingMaterialType,
"ID", strID);
186 MaterialTypeModified();
189 void BlRigidBody::MaterialTypeModified()
199 void BlRigidBody::Physics_SetVelocityDamping(
float fltLinear,
float fltAngular)
201 if(m_btPart && fltLinear > 0 && fltAngular > 0)
202 m_btPart->setDamping(fltLinear, fltAngular);
205 void BlRigidBody::Physics_SetCenterOfMass(
float fltTx,
float fltTy,
float fltTz)
207 ResetPhyiscsAndChildJoints();
210 void BlRigidBody::Physics_FluidDataChanged()
214 void BlRigidBody::Physics_WakeDynamics()
218 int istate = m_btPart->getActivationState();
220 if(m_lpThisRB->Freeze())
221 m_btPart->setActivationState(0);
222 else if(!m_btPart->isActive())
223 m_btPart->activate(
true);
227 void BlRigidBody::GetBaseValues()
232 m_lpThisRB->GetDensity();
233 m_lpThisRB->GetVolume();
237 void BlRigidBody::CreateSensorPart()
241 if(m_lpThisRB && m_lpThisAB && m_btCollisionShape)
245 m_btCollisionObject =
new btCollisionObject;
246 m_btCollisionObject->setCollisionShape( m_btCollisionShape );
247 m_btCollisionObject->setCollisionFlags( btCollisionObject::CF_KINEMATIC_OBJECT );
248 m_btCollisionObject->setWorldTransform( osgbCollision::asBtTransform( GetOSGWorldMatrix() ) );
253 m_btCollisionObject->setUserPointer((
void *) m_lpBulletData);
255 lpSim->DynamicsWorld()->addCollisionObject( m_btCollisionObject, AnimatCollisionTypes::CONTACT_SENSOR, ALL_COLLISIONS );
257 int iFlags = m_btCollisionObject->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK | btCollisionObject::CF_NO_CONTACT_RESPONSE;
258 m_btCollisionObject->setCollisionFlags(iFlags);
262 m_lpThisRB->DisableCollision(m_lpThisRB->Parent());
266 void BlRigidBody::CreateDynamicPart()
268 if(m_lpThisRB && m_lpThisAB && m_btCollisionShape &&
m_lpMaterial)
273 CStdFPoint vCom = m_lpThisRB->CenterOfMassWithStaticChildren();
279 if(m_lpThisRB->HasStaticChildren())
280 CreateStaticChildren(vCom);
281 else if(vCom != CStdFPoint(0, 0, 0))
284 osg::ref_ptr< osgbDynamics::CreationRecord > cr =
new osgbDynamics::CreationRecord;
288 cr->setCenterOfMass(osg::Vec3(vCom.x, vCom.y, vCom.z) );
289 cr->_sceneGraph = m_osgMT.get();
290 cr->_shapeType = m_eBodyType;
291 cr->_parentTransform = m_osgMT->getMatrix();
293 if(!m_lpThisRB->Freeze())
294 cr->_mass = m_lpThisRB->GetMassValueWithStaticChildren();
301 cr->_linearDamping = m_lpThisRB->LinearVelocityDamping();
302 cr->_angularDamping = m_lpThisRB->AngularVelocityDamping();
304 m_osgMT->setMatrix(osg::Matrix::identity());
306 m_btPart = osgbDynamics::createRigidBody( cr.get(), m_btCollisionShape );
309 m_btPart->setAnisotropicFriction(m_btCollisionShape->getAnisotropicRollingFrictionDirection(),btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
311 m_btPart->setLinearVelocity( btVector3( 0., 0, 0. ) );
312 m_btPart->setAngularVelocity( btVector3( 0., 0, 0. ) );
314 m_osgbMotion =
dynamic_cast<osgbDynamics::MotionState *
>(m_btPart->getMotionState());
315 m_btCollisionShape = m_btPart->getCollisionShape();
320 m_btPart->setUserPointer((
void *) m_lpBulletData);
323 Physics_WakeDynamics();
326 if(m_lpThisRB->GetContactSensor())
328 int iFlags = m_btPart->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK;
329 m_btPart->setCollisionFlags(iFlags);
332 lpSim->DynamicsWorld()->addRigidBody( m_btPart, AnimatCollisionTypes::RIGID_BODY, ALL_COLLISIONS );
334 if(m_lpThisRB->DisplayDebugCollisionGraphic())
336 m_osgDebugNode = osgbCollision::osgNodeFromBtCollisionShape( m_btCollisionShape );
337 m_osgDebugNode->setName(m_lpThisRB->Name() +
"_Debug");
338 m_osgNodeGroup->addChild(m_osgDebugNode.get());
363 btCollisionShape *btChildShape = m_btCollisionShape;
364 m_aryCompoundChildShapes.Add(m_btCollisionShape);
367 m_btCompoundShape =
new btCompoundShape();
370 m_btCollisionShape = m_btCompoundShape;
373 btTransform localTransform;
374 localTransform.setIdentity();
375 localTransform.setOrigin(btVector3(-vCom.x, -vCom.y, -vCom.z));
376 m_btCompoundShape->addChildShape(localTransform, btChildShape);
379 void BlRigidBody::CreateStaticChildren(
const CStdFPoint &vCom)
383 btCollisionShape *btChildShape = m_btCollisionShape;
384 m_aryCompoundChildShapes.Add(m_btCollisionShape);
387 m_btCompoundShape =
new btCompoundShape();
390 m_btCollisionShape = m_btCompoundShape;
393 btTransform localTransform;
394 localTransform.setIdentity();
395 localTransform.setOrigin(btVector3(-vCom.x, -vCom.y, -vCom.z));
396 m_btCompoundShape->addChildShape(localTransform, btChildShape);
399 int iCount = m_lpThisRB->ChildParts()->GetSize();
400 for(
int iIndex=0; iIndex<iCount; iIndex++)
402 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIndex);
404 if(lpChild->HasStaticJoint())
405 lpBlChild->AddStaticGeometry(
this, m_btCompoundShape, vCom);
409 void BlRigidBody::AddStaticGeometry(
BlRigidBody *lpChild, btCompoundShape *btCompound,
const CStdFPoint &vCom)
411 if(m_lpThisMI && lpChild && btCompound)
414 CreateGeometry(
true);
417 CStdFPoint vPos = m_lpThisMI->Position() - vCom;
418 CStdFPoint vRot = m_lpThisMI->Rotation();
419 osg::Matrix mt = SetupMatrix(vPos, vRot);
421 btTransform localTransform = osgbCollision::asBtTransform(mt);
422 btCompound->addChildShape(localTransform, m_btCollisionShape);
428 void BlRigidBody::RemoveStaticGeometry(
BlRigidBody *lpChild, btCompoundShape *btCompound)
430 if(m_lpThisMI && lpChild && btCompound)
433 btCompound->removeChildShape(m_btCollisionShape);
434 DeleteCollisionGeometry();
438 void BlRigidBody::ResetStaticCollisionGeom()
442 void BlRigidBody::ResetSensorCollisionGeom()
444 if(m_osgMT.valid() && m_osgbMotion && m_btCollisionObject)
446 osg::Matrix osgMTmat = m_osgMT->getMatrix();
447 btTransform wt = osgbCollision::asBtTransform(osgMTmat);
448 m_osgbMotion->setWorldTransform( wt );
449 m_btCollisionObject->setWorldTransform( wt );
453 void BlRigidBody::ResetDynamicCollisionGeom()
455 if(m_osgMT.valid() && m_osgbMotion && m_btPart)
458 osg::Vec3d vCom = m_osgbMotion->getCenterOfMass();
459 osg::Matrixd osgMtCom = osg::Matrixd::translate(vCom);
460 osg::Matrixd osgMTBase = m_osgMT->getMatrix();
461 osg::Matrixd osgMTmat = osgMTBase * osgMtCom;
463 btTransform wt = osgbCollision::asBtTransform(osgMTmat);
464 m_osgbMotion->setWorldTransform( wt );
465 m_btPart->setWorldTransform( wt );
469 void BlRigidBody::DeleteDynamicPart()
473 GetBlSimulator()->DynamicsWorld()->removeRigidBody(m_btPart);
478 {
delete m_osgbMotion; m_osgbMotion = NULL;}
481 {
delete m_lpBulletData; m_lpBulletData = NULL;}
485 void BlRigidBody::DeleteSensorPart()
487 if(m_btCollisionObject)
489 GetBlSimulator()->DynamicsWorld()->removeCollisionObject(m_btCollisionObject);
490 delete m_btCollisionObject;
491 m_btCollisionObject = NULL;
494 {
delete m_lpBulletData; m_lpBulletData = NULL;}
498 void BlRigidBody::DeleteCollisionGeometry()
501 if(m_btCompoundShape)
503 int iCount = m_btCompoundShape->getNumChildShapes();
504 for(
int iIdx=0; iIdx<iCount; iIdx++)
505 m_btCompoundShape->removeChildShapeByIndex(0);
507 m_aryCompoundChildShapes.RemoveAll();
509 delete m_btCompoundShape;
510 m_btCompoundShape = NULL;
511 m_btCollisionShape = NULL;
513 else if(m_btCollisionShape)
514 {
delete m_btCollisionShape; m_btCollisionShape = NULL;}
517 void BlRigidBody::DeletePhysics(
bool bIncludeChildren)
519 if(m_btPart || m_btCollisionObject)
523 DeleteAttachedJointPhysics();
530 DeleteCollisionGeometry();
533 DeleteChildPhysics();
535 Physics_DeleteStickyLock();
539 void BlRigidBody::DeleteChildPhysics()
541 int iCount = m_lpThisRB->ChildParts()->GetSize();
542 for(
int iIdx=0; iIdx<iCount; iIdx++)
544 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIdx);
549 lpVsChild->DeletePhysics(
true);
554 void BlRigidBody::DeleteAttachedJointPhysics()
556 BlJoint *lpVsJoint =
dynamic_cast<BlJoint *
>(m_lpThisRB->JointToParent());
559 lpVsJoint->DeletePhysics(
false);
561 int iCount = m_lpThisRB->ChildParts()->GetSize();
562 for(
int iIdx=0; iIdx<iCount; iIdx++)
564 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIdx);
567 BlJoint *lpVsChildJoint =
dynamic_cast<BlJoint *
>(lpChild->JointToParent());
569 lpVsChildJoint->DeletePhysics(
false);
574 void BlRigidBody::RecreateAttachedJointPhysics()
576 BlJoint *lpVsJoint =
dynamic_cast<BlJoint *
>(m_lpThisRB->JointToParent());
579 lpVsJoint->SetupPhysics();
581 int iCount = m_lpThisRB->ChildParts()->GetSize();
582 for(
int iIdx=0; iIdx<iCount; iIdx++)
584 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIdx);
587 BlJoint *lpVsChildJoint =
dynamic_cast<BlJoint *
>(lpChild->JointToParent());
589 lpVsChildJoint->SetupPhysics();
594 void BlRigidBody::ResizePhysicsGeometry()
596 if(m_lpThisRB && m_lpThisRB->HasStaticJoint() && m_btCollisionShape)
603 lpOsgParent->ResizePhysicsGeometry();
608 DeletePhysics(
false);
611 CreatePhysicsGeometry();
617 RecreateAttachedJointPhysics();
633 osg::Matrix rotMT = GetWorldMatrix();
634 rotMT.setTrans(osg::Vec3d(0, 0, 0));
637 osg::Vec4d vRotatedArea = rotMT * vArea;
639 m_vRotatedArea.Set(fabs(vRotatedArea[0]), fabs(vRotatedArea[1]), fabs(vRotatedArea[2]));
642 bool BlRigidBody::NeedCollision(
BlRigidBody *lpTest)
645 if(m_lpThisRB->FindCollisionExclusionBody(lpTest->m_lpThisRB,
false))
651 void BlRigidBody::Physics_ContactSensorAdded(ContactSensor *lpSensor)
655 int iFlags = m_btPart->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK;
656 m_btPart->setCollisionFlags(iFlags);
660 void BlRigidBody::Physics_ContactSensorRemoved()
664 int iFlags = m_btPart->getCollisionFlags() & ~btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK;
665 m_btPart->setCollisionFlags(iFlags);
669 void BlRigidBody::Physics_ChildBodyAdded(RigidBody *lpChild)
671 if(lpChild && lpChild->HasStaticJoint())
672 ResizePhysicsGeometry();
675 void BlRigidBody::Physics_ChildBodyRemoved(
bool bHasStaticJoint)
678 ResizePhysicsGeometry();
683 osg::Matrix mtParent = lpParent->GetWorldMatrix();
684 osg::Matrix mtChild = lpChild->GetWorldMatrix();
686 osg::Matrix mtChildRelToParent = mtChild * osg::Matrix::inverse(mtParent);
688 btTransform mtJointRelToParent = osgbCollision::asBtTransform(mtChildRelToParent);
689 btTransform mtJointRelToChild = osgbCollision::asBtTransform(osg::Matrixd::identity());
694 btVector3 vLowerLimit, vUpperLimit;
697 vLowerLimit[0] = vLowerLimit[1] = vLowerLimit[2] = 0;
698 vUpperLimit[0] = vUpperLimit[1] = vUpperLimit[2] = 0;
700 btStickyLock->setLinearLowerLimit(vLowerLimit);
701 btStickyLock->setLinearUpperLimit(vUpperLimit);
702 btStickyLock->setAngularLowerLimit(vLowerLimit);
703 btStickyLock->setAngularUpperLimit(vUpperLimit);
705 GetBlSimulator()->DynamicsWorld()->addConstraint(btStickyLock,
true);
710 void BlRigidBody::CreateStickyLock()
712 static bool bDisabledColls =
false;
714 BlRigidBody *lpChild = m_aryContactPoints[0]->m_lpContacted;
716 if(lpChild && lpParent)
718 BlRigidBody *lpRightGrip =
dynamic_cast<BlRigidBody *
>(GetSimulator()->FindByID(
"ac233f7b-8f8f-4d03-9d4f-21e363a19bc2"));
719 BlRigidBody *lpLeftGrip =
dynamic_cast<BlRigidBody *
>(GetSimulator()->FindByID(
"35b1ffff-ebbc-4bdb-8108-5d44aea2a1c5"));
721 m_btStickyLock = AddDynamicJoint(lpLeftGrip, lpChild);
728 m_lpThisRB->StickyChild(lpChild->m_lpThisRB);
745 void BlRigidBody::Physics_DeleteStickyLock()
749 if(GetBlSimulator() && GetBlSimulator()->DynamicsWorld())
751 if(m_lpThisRB->StickyChild())
753 BlRigidBody *lpRightGrip =
dynamic_cast<BlRigidBody *
>(GetSimulator()->FindByID(
"ac233f7b-8f8f-4d03-9d4f-21e363a19bc2"));
757 m_lpThisRB->StickyChild(NULL);
768 GetBlSimulator()->DynamicsWorld()->removeConstraint(m_btStickyLock);
770 delete m_btStickyLock;
771 m_btStickyLock = NULL;
777 if(GetBlSimulator() && GetBlSimulator()->DynamicsWorld())
779 GetBlSimulator()->DynamicsWorld()->removeConstraint(m_btStickyLock2);
781 delete m_btStickyLock2;
782 m_btStickyLock2 = NULL;
787 void BlRigidBody::SetSurfaceContactCount()
791 m_lpThisRB->SetSurfaceContactCount(m_aryContactPoints.size());
793 if(m_lpThisRB->IsStickyPart() && m_aryContactPoints.size() > 0 && m_aryContactPoints[0]->m_lpContacted && m_lpThisRB->Parent())
796 if(m_btStickyLock && m_lpThisRB->StickyOn() < 1)
797 Physics_DeleteStickyLock();
799 else if(!m_btStickyLock && m_lpThisRB->StickyOn() >= 1)
805 void BlRigidBody::ProcessContacts()
807 ContactSensor *lpSensor = m_lpThisRB->GetContactSensor();
810 lpSensor->ClearCurrents();
814 if(m_lpThisRB->IsContactSensor())
815 SetSurfaceContactCount();
816 else if(m_aryContactPoints.size() > 0 && m_btPart && lpSensor)
820 float fltForceMag = 0;
821 float fDisUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
822 float fMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
823 float fltPhysicsDt = m_lpThisAB->GetSimulator()->PhysicsTimeStep();
824 float fltRatio = (fMassUnits * fDisUnits) / fltPhysicsDt;
830 int iCount=m_aryContactPoints.size();
831 for(
int iIdx=0; iIdx<iCount; iIdx++)
835 if(lpContactPoint && lpContactPoint->m_lpCP)
837 lpContactPoint->m_lpCP->m_localPointB;
839 if(lpContactPoint->m_bIsBodyA)
841 vBodyPos[0] = lpContactPoint->m_lpCP->m_localPointA[0];
842 vBodyPos[1] = lpContactPoint->m_lpCP->m_localPointA[1];
843 vBodyPos[2] = lpContactPoint->m_lpCP->m_localPointA[2];
847 vBodyPos[0] = lpContactPoint->m_lpCP->m_localPointB[0];
848 vBodyPos[1] = lpContactPoint->m_lpCP->m_localPointB[1];
849 vBodyPos[2] = lpContactPoint->m_lpCP->m_localPointB[2];
852 fltForceMag = lpContactPoint->m_lpCP->m_appliedImpulse * fltRatio;
855 lpSensor->ProcessContact(vBodyPos, fltForceMag);
862 m_aryContactPoints.Clear();
865 void BlRigidBody::Physics_CollectData()
874 m_osgbMotion->getWorldTransform(trans);
875 m_osgWorldMatrix = osgbCollision::asOsgMatrix(trans);
878 btVector3 vPos = trans.getOrigin();
879 osg::Vec3 vCom = m_osgbMotion->getCenterOfMass();
881 float fltX = (vPos.x()-vCom.x());
882 float fltY = (vPos.y()-vCom.y());
883 float fltZ = (vPos.z()-vCom.z());
885 m_lpThisMI->AbsolutePosition(fltX, fltY, fltZ);
887 m_osgWorldMatrix(3,0) = fltX;
888 m_osgWorldMatrix(3,1) = fltY;
889 m_osgWorldMatrix(3,2) = fltZ;
891 CStdFPoint vRot = OsgMatrixUtil::EulerRotationFromMatrix_Static(m_osgWorldMatrix);
892 m_lpThisMI->ReportRotation(vRot[0], vRot[1], vRot[2]);
896 if(m_btCollisionObject)
897 m_btCollisionObject->setWorldTransform( osgbCollision::asBtTransform( GetOSGWorldMatrix(
true) ) );
900 Physics_UpdateAbsolutePosition();
902 CStdFPoint vRot = OsgMatrixUtil::EulerRotationFromMatrix_Static(m_osgWorldMatrix);
903 m_lpThisMI->ReportRotation(vRot[0], vRot[1], vRot[2]);
907 if(m_lpThisRB->GetContactSensor() || m_lpThisRB->IsContactSensor())
911 void BlRigidBody::Physics_CollectExtraData()
913 float fDisUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
914 float fMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
918 if(m_btPart && lpSim)
920 float m_vPrevLinearVelocity[3];
921 float m_vPrevAngularVelocity[3];
924 m_vPrevLinearVelocity[0] = m_vLinearVelocity[0];
925 m_vPrevLinearVelocity[1] = m_vLinearVelocity[1];
926 m_vPrevLinearVelocity[2] = m_vLinearVelocity[2];
928 m_vPrevAngularVelocity[0] = m_vAngularVelocity[0];
929 m_vPrevAngularVelocity[1] = m_vAngularVelocity[1];
930 m_vPrevAngularVelocity[2] = m_vAngularVelocity[2];
932 vData = m_btPart->getLinearVelocity();
933 m_vLinearVelocity[0] = vData[0] * fDisUnits;
934 m_vLinearVelocity[1] = vData[1] * fDisUnits;
935 m_vLinearVelocity[2] = vData[2] * fDisUnits;
937 vData = m_btPart->getAngularVelocity();
938 m_vAngularVelocity[0] = vData[0];
939 m_vAngularVelocity[1] = vData[1];
940 m_vAngularVelocity[2] = vData[2];
942 vData = m_btPart->getTotalForce();
943 float fltRatio = fMassUnits * fDisUnits;
944 m_vForce[0] = vData[0] * fltRatio;
945 m_vForce[1] = vData[1] * fltRatio;
946 m_vForce[2] = vData[2] * fltRatio;
948 vData = m_btPart->getTotalTorque();
949 fltRatio = fMassUnits * fDisUnits * fDisUnits;
950 m_vTorque[0] = vData[0] * fltRatio;
951 m_vTorque[1] = vData[1] * fltRatio;
952 m_vTorque[2] = vData[2] * fltRatio;
954 float fltDt = lpSim->PhysicsTimeStep();
959 m_vLinearAcceleration[0] = (m_vLinearVelocity[0] - m_vPrevLinearVelocity[0])/fltDt;
960 m_vLinearAcceleration[1] = (m_vLinearVelocity[1] - m_vPrevLinearVelocity[1])/fltDt;
961 m_vLinearAcceleration[2] = (m_vLinearVelocity[2] - m_vPrevLinearVelocity[2])/fltDt;
963 m_vAngularAcceleration[0] = (m_vAngularVelocity[0] - m_vPrevAngularVelocity[0])/fltDt;
964 m_vAngularAcceleration[1] = (m_vAngularVelocity[1] - m_vPrevAngularVelocity[1])/fltDt;
965 m_vAngularAcceleration[2] = (m_vAngularVelocity[2] - m_vPrevAngularVelocity[2])/fltDt;
970 void BlRigidBody::Physics_ResetSimulation()
972 OsgRigidBody::Physics_ResetSimulation();
976 m_btPart->clearForces();
977 btVector3 zeroVector(0,0,0);
978 m_btPart->setLinearVelocity(zeroVector);
979 m_btPart->setAngularVelocity(zeroVector);
982 Physics_WakeDynamics();
984 for(
int i=0; i<3; i++)
988 m_vLinearVelocity[i] = 0;
989 m_vAngularVelocity[i] = 0;
990 m_vLinearAcceleration[i] = 0;
991 m_vAngularAcceleration[i] = 0;
994 Physics_DeleteStickyLock();
997 void BlRigidBody::Physics_EnableCollision(RigidBody *lpBody)
999 if(m_lpBulletData && m_lpThisRB->GetExclusionCollisionSet()->size() == 0)
1000 m_lpBulletData->m_bExclusionProcessing =
false;
1003 void BlRigidBody::Physics_DisableCollision(RigidBody *lpBody)
1005 if(m_lpBulletData && m_lpThisRB->GetExclusionCollisionSet()->size() > 0)
1006 m_lpBulletData->m_bExclusionProcessing =
true;
1009 void BlRigidBody::Physics_AddBodyForceAtLocalPos(
float fltPx,
float fltPy,
float fltPz,
float fltFx,
float fltFy,
float fltFz,
bool bScaleUnits)
1011 if(m_btPart && (fltFx || fltFy || fltFz) && !m_lpThisRB->Freeze())
1013 btVector3 fltF, fltP;
1016 fltF[0] = fltFx * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1017 fltF[1] = fltFy * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1018 fltF[2] = fltFz * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1031 Physics_WakeDynamics();
1032 m_btPart->applyForce(fltF, fltP);
1036 void BlRigidBody::Physics_AddBodyForceAtWorldPos(
float fltPx,
float fltPy,
float fltPz,
float fltFx,
float fltFy,
float fltFz,
bool bScaleUnits)
1038 if(m_btPart && (fltFx || fltFy || fltFz) && !m_lpThisRB->Freeze())
1040 btVector3 fltF, fltP;
1043 fltF[0] = fltFx * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1044 fltF[1] = fltFy * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1045 fltF[2] = fltFz * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1055 btVector3 vCOM = m_btPart->getCenterOfMassPosition();
1057 fltP[0] = fltPx - vCOM[0];
1058 fltP[1] = fltPy - vCOM[1];
1059 fltP[2] = fltPz - vCOM[2];
1061 Physics_WakeDynamics();
1062 m_btPart->applyForce(fltF, fltP);
1066 void BlRigidBody::Physics_AddBodyTorque(
float fltTx,
float fltTy,
float fltTz,
bool bScaleUnits)
1068 if(m_btPart && (fltTx || fltTy || fltTz))
1073 fltT[0] = fltTx * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1074 fltT[1] = fltTy * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1075 fltT[2] = fltTz * (m_lpThisAB->GetSimulator()->InverseMassUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits() * m_lpThisAB->GetSimulator()->InverseDistanceUnits());
1084 m_btPart->applyTorque(fltT);
1088 CStdFPoint BlRigidBody::Physics_GetVelocityAtPoint(
float x,
float y,
float z)
1090 CStdFPoint linVel(0,0,0);
1091 btVector3 vLinVel(0,0,0);
1092 btVector3 vPoint(x,y,z);
1097 vLinVel = m_btPart->getVelocityInLocalPoint(vPoint);
1098 linVel.Set(vLinVel[0], vLinVel[1], vLinVel[2]);
1104 bool BlRigidBody::Physics_HasCollisionGeometry()
1106 if(m_btCollisionShape)
1112 void BlRigidBody::Physics_StepHydrodynamicSimulation()
1114 Simulator *lpSim = GetSimulator();
1116 if(m_btPart && lpSim && lpBlSim && m_lpThisRB && !m_lpThisRB->Freeze())
1118 float fltDepth = m_lpThisRB->AbsolutePosition().y;
1123 m_fltBuoyancy = -(lpPlane->Density() * m_lpThisRB->Volume() * lpSim->Gravity() * m_lpThisRB->BuoyancyScale());
1126 btVector3 vbtLinVel = m_btPart->getLinearVelocity();
1127 btVector3 vbtAngVel = m_btPart->getAngularVelocity();
1129 CStdFPoint vLinearVel(vbtLinVel[0], vbtLinVel[1], vbtLinVel[2]);
1130 CStdFPoint vAngularVel(vbtAngVel[0], vbtAngVel[1], vbtAngVel[2]);
1136 CStdFPoint vLinearDragForce = (m_lpThisRB->LinearDrag() *
m_vRotatedArea * vLinearVel * vLinearVel * vSignLinear) * (lpPlane->Density() * -0.5);
1137 CStdFPoint vAngularDragTorque = (m_lpThisRB->AngularDrag() *
m_vRotatedArea * vAngularVel * vAngularVel * vSignAngular) * (lpPlane->Density() * -0.5);
1138 float fltMaxForce = m_lpThisRB->MaxHydroForce();
1139 float fltMaxTorque = m_lpThisRB->MaxHydroTorque();
1140 for(
int i=0; i<3; i++)
1142 m_vLinearDragForce[i] = vLinearDragForce[i] * lpSim->MassUnits() * lpSim->DistanceUnits();
1143 m_vAngularDragTorque[i] = vAngularDragTorque[i] * lpSim->MassUnits() * lpSim->DistanceUnits() * lpSim->DistanceUnits();
1145 if(vLinearDragForce[i] > fltMaxForce)
1146 vLinearDragForce[i] = fltMaxForce;
1148 if(vLinearDragForce[i] < -fltMaxForce)
1149 vLinearDragForce[i] = -fltMaxForce;
1152 vAngularDragTorque[i] = fltMaxTorque;
1155 vAngularDragTorque[i] = -fltMaxTorque;
1158 Physics_WakeDynamics();
1159 m_btPart->applyCentralForce(btVector3(vLinearDragForce.x, (vLinearDragForce.y+
m_fltBuoyancy), vLinearDragForce.z));
1160 m_btPart->applyTorque(btVector3(vAngularDragTorque.x, vAngularDragTorque.y, vAngularDragTorque.z));
1166 for(
int iIdx=0; iIdx<3; iIdx++)
1176 float *BlRigidBody::Physics_GetDataPointer(
const std::string &strDataType)
1179 RigidBody *lpBody =
dynamic_cast<RigidBody *
>(
this);
1181 if(strType ==
"BODYBUOYANCY")
1184 if(strType ==
"BODYDRAGFORCEX")
1187 if(strType ==
"BODYDRAGFORCEY")
1190 if(strType ==
"BODYDRAGFORCEZ")
1193 if(strType ==
"BODYDRAGTORQUEX")
1196 if(strType ==
"BODYDRAGTORQUEY")
1199 if(strType ==
"BODYDRAGTORQUEZ")
1202 return OsgRigidBody::Physics_GetDataPointer(strDataType);
virtual void AddRigidBodyAssociation(RigidBody *lpBody)
Associates a rigid body with this material type. This is used when you change something about this ma...
virtual float FrictionAngularPrimaryConverted()
Gets the angular primary friction coefficient converted to match vortex values.
virtual void SetupOffsetCOM(const CStdFPoint &vCom)
Changes this body to use a btCompoundShape and adds the btCollision shape offset from -COM...
float m_vAngularDragTorque[3]
This is the drag forces applied to this body.
A common class for all joint data specific to vortex.
A common class for all rigid body data specific to vortex.
float m_vLinearDragForce[3]
This is the drag forces applied to this body.
virtual void CalculateRotatedAreas()
Rotates the axis area values by the amount that this part is rotated by. This is used by the hydrodyn...
int Std_Sign(float fltVal)
Determines the sign of a number.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual FluidPlane * FindFluidPlaneForDepth(float fltDepth)
Searches the fluid planes starting at the lowest height and working its way up to find the first one ...
CStdFPoint m_vRotatedArea
virtual float Restitution()
Gets the restitution for collisions between RigidBodies with these two materials. ...
virtual float FrictionLinearPrimary()
Gets the primary friction coefficient.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
BlMaterialType * m_lpMaterial
The pointer to the material for this body.
float m_fltReportBuoyancy
The buoyancy force reported to the GUI.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
float m_fltBuoyancy
The buoyancy force applied to this part.
virtual void RemoveRigidBodyAssociation(RigidBody *lpBody)
Removes the rigid body association described by lpBody from this material.
virtual float FrictionAngularPrimary()
Gets the angular primary friction coefficient.