6 #include "OsgMovableItem.h"
8 #include "OsgRigidBody.h"
11 #include "OsgSimulator.h"
12 #include "OsgUserData.h"
13 #include "OsgUserDataVisitor.h"
14 #include "OsgDragger.h"
25 OsgRigidBody::OsgRigidBody()
27 for(
int i=0; i<3; i++)
31 m_vLinearVelocity[i] = 0;
32 m_vAngularVelocity[i] = 0;
33 m_vLinearAcceleration[i] = 0;
34 m_vAngularAcceleration[i] = 0;
38 OsgRigidBody::~OsgRigidBody()
46 {
Std_TraceMsg(0,
"Caught Error in desctructor of OsgRigidBody\r\n",
"", -1,
false,
true);}
49 void OsgRigidBody::SetThisPointers()
51 OsgBody::SetThisPointers();
52 m_lpThisRB =
dynamic_cast<RigidBody *
>(
this);
54 THROW_TEXT_ERROR(Osg_Err_lThisPointerNotDefined, Osg_Err_strThisPointerNotDefined,
"m_lpThisRB, " + m_lpThisAB->Name());
56 m_lpThisRB->PhysicsBody(
this);
59 void OsgRigidBody::Physics_UpdateMatrix()
61 OsgMovableItem::Physics_UpdateMatrix();
66 void OsgRigidBody::UpdatePositionAndRotationFromMatrix()
69 m_lpThisRB->UpdatePhysicsPosFromGraphics();
71 OsgBody::UpdatePositionAndRotationFromMatrix();
74 void OsgRigidBody::Physics_SetColor()
77 SetColor(*m_lpThisRB->Ambient(), *m_lpThisRB->Diffuse(), *m_lpThisRB->Specular(), m_lpThisRB->Shininess());
80 void OsgRigidBody::Physics_TextureChanged()
83 SetTexture(m_lpThisRB->Texture());
99 void OsgRigidBody::StartGripDrag()
103 DeleteChildGraphics(
true);
108 SetupChildGraphics(
true);
112 void OsgRigidBody::EndGripDrag()
116 if(m_lpThisRB && m_lpThisRB->HasStaticJoint())
118 OsgBody::UpdatePositionAndRotationFromMatrix();
126 lpOsgParent->DeletePhysics(
true);
127 lpOsgParent->DeleteChildGraphics(
true);
129 lpOsgParent->EndGripDrag();
136 CreatePhysicsGeometry();
138 OsgBody::UpdatePositionAndRotationFromMatrix();
140 DeleteChildGraphics(
true);
148 m_lpThisRB->CreateChildParts();
151 if(m_lpThisRB->JointToParent())
152 m_lpThisRB->JointToParent()->CreateJoint();
155 m_lpThisRB->CreateChildJoints();
159 if(m_lpThisRB && m_lpThisRB->IsRoot())
163 lpOsgStruct->FinalMatrix(m_osgFinalMatrix);
164 Physics_UpdateAbsolutePosition();
169 void OsgRigidBody::SetupChildGraphics(
bool bRoot)
173 InitializeGraphicsGeometry();
177 OsgJoint *lpVsJoint =
dynamic_cast<OsgJoint *
>(m_lpThisRB->JointToParent());
182 int iCount = m_lpThisRB->ChildParts()->GetSize();
183 for(
int iIdx=0; iIdx<iCount; iIdx++)
185 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIdx);
190 lpVsChild->SetupChildGraphics(
false);
195 void OsgRigidBody::DeleteChildGraphics(
bool bRoot)
200 OsgJoint *lpVsJoint =
dynamic_cast<OsgJoint *
>(m_lpThisRB->JointToParent());
203 lpVsJoint->DeleteGraphics();
205 int iCount = m_lpThisRB->ChildParts()->GetSize();
206 for(
int iIdx=0; iIdx<iCount; iIdx++)
208 RigidBody *lpChild = m_lpThisRB->ChildParts()->at(iIdx);
213 lpVsChild->DeleteChildGraphics(
false);
218 void OsgRigidBody::Physics_Resize()
221 if(m_osgNode.valid())
223 osg::Geode *osgGroup = NULL;
227 osgGroup =
dynamic_cast<osg::Geode *
>(m_osgNode.get());
230 THROW_TEXT_ERROR(Osg_Err_lNodeNotGeode, Osg_Err_strNodeNotGeode, m_lpThisAB->Name());
232 if(osgGroup && osgGroup->containsDrawable(m_osgGeometry.get()))
233 osgGroup->removeDrawable(m_osgGeometry.get());
235 m_osgGeometry.release();
238 CreateGraphicsGeometry();
240 m_osgGeometry->setName(m_lpThisAB->Name() +
"_Geometry");
243 osgGroup->addDrawable(m_osgGeometry.get());
246 if(m_osgDragger.valid())
247 m_osgDragger->SetupMatrix();
250 if(m_osgNodeGroup.valid())
253 osgVisitor->traverse(*m_osgNodeGroup);
257 if(Physics_IsDefined())
259 ResizePhysicsGeometry();
263 Physics_SetDensity(m_lpThisRB->Density());
270 void OsgRigidBody::Physics_SelectedVertex(
float fltXPos,
float fltYPos,
float fltZPos)
272 if(m_lpThisRB->IsCollisionObject() && m_osgSelVertexMT.valid())
275 osgMT.makeIdentity();
276 osgMT = osgMT.translate(fltXPos, fltYPos, fltZPos);
278 m_osgSelVertexMT->setMatrix(osgMT);
283 void OsgRigidBody::ShowSelectedVertex()
285 if(m_lpThisRB && m_lpThisAB && m_lpThisRB->IsCollisionObject() && m_lpThisAB->Selected() && m_osgMT.valid() && m_osgSelVertexMT.valid())
287 if(!m_osgMT->containsNode(m_osgSelVertexMT.get()))
288 m_osgMT->addChild(m_osgSelVertexMT.get());
292 void OsgRigidBody::HideSelectedVertex()
294 if(m_lpThisRB->IsCollisionObject() && m_osgMT.valid() && m_osgSelVertexMT.valid())
296 if(m_osgMT->containsNode(m_osgSelVertexMT.get()))
297 m_osgMT->removeChild(m_osgSelVertexMT.get());
301 void OsgRigidBody::Physics_ResizeSelectedReceptiveFieldVertex()
303 DeleteSelectedVertex();
304 CreateSelectedVertex(m_lpThisAB->Name());
307 void OsgRigidBody::Initialize()
324 if(m_lpThisRB && m_lpThisMI && m_lpThisAB)
326 if(m_lpThisRB->IsRoot())
327 OsgBody::BuildLocalMatrix(m_lpThisAB->GetStructure()->AbsolutePosition(), CStdFPoint(0, 0, 0), m_lpThisMI->Rotation(), m_lpThisAB->Name());
329 OsgBody::BuildLocalMatrix(m_lpThisMI->Position(), CStdFPoint(0, 0, 0), m_lpThisMI->Rotation(), m_lpThisAB->Name());
349 if(!m_lpThisRB->IsRoot() && m_lpParentVsMI)
350 return m_lpParentVsMI->GetMatrixTransform();
352 return GetOsgSimulator()->OSGRoot();
355 osg::Matrix OsgRigidBody::GetComMatrix(
bool bInvert)
359 CStdFPoint vCom = m_lpThisRB->CenterOfMassWithStaticChildren();
364 osg::Matrixd mat = osg::Matrixd::identity();
365 return mat.translate(vCom.x, vCom.y, vCom.z);
368 return OsgBody::GetComMatrix();
371 void OsgRigidBody::CreateGeometry(
bool bOverrideStatic)
375 if(m_lpThisRB && (!m_lpThisRB->HasStaticJoint() || bOverrideStatic))
377 InitializeGraphicsGeometry();
378 CreatePhysicsGeometry();
382 void OsgRigidBody::SetupPhysics()
386 if(m_lpThisRB && m_lpThisRB->IsCollisionObject())
390 if(m_lpThisRB->IsContactSensor())
392 else if(m_lpThisRB->HasStaticJoint())
403 bool OsgRigidBody::AddOsgNodeToParent()
405 if(!m_lpThisRB || !m_lpThisRB->IsCollisionObject() || m_lpThisRB->IsContactSensor() || GetOsgSimulator()->InDrag() || m_lpThisRB->HasStaticJoint())
411 float *OsgRigidBody::Physics_GetDataPointer(
const std::string &strDataType)
414 RigidBody *lpBody =
dynamic_cast<RigidBody *
>(
this);
416 if(strType ==
"BODYTORQUEX")
418 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
419 return (&m_vTorque[0]);
422 if(strType ==
"BODYTORQUEY")
424 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
425 return (&m_vTorque[1]);
428 if(strType ==
"BODYTORQUEZ")
430 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
431 return (&m_vTorque[2]);
434 if(strType ==
"BODYFORCEX")
436 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
437 return (&m_vForce[0]);
440 if(strType ==
"BODYFORCEY")
442 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
443 return (&m_vForce[1]);
446 if(strType ==
"BODYFORCEZ")
448 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
449 return (&m_vForce[2]);
452 if(strType ==
"BODYLINEARVELOCITYX")
454 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
455 return (&m_vLinearVelocity[0]);
458 if(strType ==
"BODYLINEARVELOCITYY")
460 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
461 return (&m_vLinearVelocity[1]);
464 if(strType ==
"BODYLINEARVELOCITYZ")
466 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
467 return (&m_vLinearVelocity[2]);
470 if(strType ==
"BODYANGULARVELOCITYX")
472 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
473 return (&m_vAngularVelocity[0]);
476 if(strType ==
"BODYANGULARVELOCITYY")
478 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
479 return (&m_vAngularVelocity[1]);
482 if(strType ==
"BODYANGULARVELOCITYZ")
484 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
485 return (&m_vAngularVelocity[2]);
491 if(strType ==
"BODYLINEARACCELERATIONX")
493 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
494 return (&m_vLinearAcceleration[0]);
497 if(strType ==
"BODYLINEARACCELERATIONY")
499 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
500 return (&m_vLinearAcceleration[1]);
503 if(strType ==
"BODYLINEARACCELERATIONZ")
505 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
506 return (&m_vLinearAcceleration[2]);
509 if(strType ==
"BODYANGULARACCELERATIONX")
511 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
512 return (&m_vAngularAcceleration[0]);
515 if(strType ==
"BODYANGULARACCELERATIONY")
517 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
518 return (&m_vAngularAcceleration[1]);
521 if(strType ==
"BODYANGULARACCELERATIONZ")
523 GetOsgSimulator()->AddToExtractExtraData(m_lpThisRB);
524 return (&m_vAngularAcceleration[2]);
osg::ref_ptr< osg::MatrixTransform > m_osgGeometryRotationMT
A common class for all rigid body data specific to vortex.
virtual void SetupGraphics()
Sets up the graphics for the joint.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual osg::MatrixTransform * ParentOSG()
Gets the parent osg node.
virtual void BuildLocalMatrix()
Builds the local matrix.
A common class for all joint data specific to vortex.
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.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the vortex structure class.