6 #include "VsMovableItem.h"
9 #include "VsMotorizedJoint.h"
10 #include "VsRigidBody.h"
11 #include "VsMeshBase.h"
12 #include "VsSimulator.h"
13 #include "VsDragger.h"
14 #include "VsOsgUserDataVisitor.h"
27 VsMeshBase::VsMeshBase()
31 VsMeshBase::~VsMeshBase()
38 {
Std_TraceMsg(0,
"Caught Error in desctructor of VsMeshBase\r\n",
"", -1,
false,
true);}
41 void VsMeshBase::SetThisPointers()
43 VsRigidBody::SetThisPointers();
44 m_lpThisMesh =
dynamic_cast<Mesh *
>(
this);
46 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisMesh, " + m_lpThisAB->Name());
49 void VsMeshBase::CreateGraphicsGeometry()
51 m_osgGeometry = CreateBoxGeometry(1, 1, 1, 1, 1, 1);
54 void VsMeshBase::LoadMeshNode()
56 std::string strPath = m_lpThisAB->GetSimulator()->ProjectPath();
57 std::string strMeshFile;
59 if(m_lpThisRB->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() ==
"CONVEX")
60 strMeshFile = m_lpThisMesh->ConvexMeshFile();
62 strMeshFile = m_lpThisMesh->MeshFile();
64 std::string strFile = AnimatSim::GetFilePath(strPath, strMeshFile);
65 m_osgBaseMeshNode = GetVsSimulator()->MeshMgr()->LoadMesh(strFile);
67 if(!m_osgBaseMeshNode.valid())
70 m_osgBaseMeshNode->setName(m_lpThisAB->Name() +
"_MeshNodeBase");
76 CStdFPoint vScale(1, 1, 1);
80 if( !(m_lpThisMesh->IsCollisionObject() && m_lpThisMesh->CollisionMeshType() ==
"CONVEX") )
81 vScale= m_lpThisMesh->Scale();
83 osg::Matrix osgScaleMatrix = osg::Matrix::identity();
84 osgScaleMatrix.makeScale(vScale.x, vScale.y, vScale.z);
86 m_osgMeshNode =
new osg::MatrixTransform(osgScaleMatrix);
88 m_osgMeshNode->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
90 m_osgMeshNode->addChild(m_osgBaseMeshNode.get());
91 m_osgMeshNode->setName(m_lpThisAB->Name() +
"_MeshNode");
94 void VsMeshBase::CreateDefaultMesh()
96 CreateGraphicsGeometry();
97 m_osgGeometry->setName(m_lpThisAB->Name() +
"_Geometry");
99 osg::Geode *osgGroup =
new osg::Geode;
100 osgGroup->addDrawable(m_osgGeometry.get());
102 m_osgBaseMeshNode = osgGroup;
105 void VsMeshBase::CreateGeometry()
113 CreatePhysicsGeometry();
116 osg::Group *osgNodeGroup = NULL;
117 if(m_osgNode.valid())
118 osgNodeGroup = dynamic_cast<osg::Group *>(m_osgNode.get());
121 osgNodeGroup =
new osg::Group;
122 m_osgNode = osgNodeGroup;
126 THROW_TEXT_ERROR(Vs_Err_lMeshOsgNodeGroupNotDefined, Vs_Err_strMeshOsgNodeGroupNotDefined,
"Body: " + m_lpThisAB->Name() +
" Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
128 osgNodeGroup->addChild(m_osgMeshNode.get());
131 void VsMeshBase::CalculateEstimatedMassAndVolume()
139 m_fltEstimatedMass = (m_lpThisRB->Density() * fltVolume) * m_lpThisRB->GetSimulator()->DisplayMassUnits();
143 void VsMeshBase::CreatePhysicsGeometry()
145 if(m_lpThisRB->IsCollisionObject())
147 if(m_lpThisMesh->CollisionMeshType() ==
"CONVEX")
148 m_vxGeometry = GetVsSimulator()->CreateConvexMeshFromOsg(m_osgMeshNode.get());
150 m_vxGeometry = GetVsSimulator()->CreatTriangleMeshFromOsg(m_osgMeshNode.get());
153 THROW_TEXT_ERROR(Vs_Err_lCreatingGeometry, Vs_Err_strCreatingGeometry,
"Body: " + m_lpThisAB->Name() +
" Mesh: " + AnimatSim::GetFilePath(m_lpThisAB->GetSimulator()->ProjectPath(), m_lpThisMesh->MeshFile()));
156 CalculateEstimatedMassAndVolume();
160 void VsMeshBase::ResizePhysicsGeometry()
162 if(m_vxGeometry && m_vxCollisionGeometry && m_vxSensor)
164 if(!m_vxSensor->removeCollisionGeometry(m_vxCollisionGeometry))
165 THROW_PARAM_ERROR(Vs_Err_lRemovingCollisionGeometry, Vs_Err_strRemovingCollisionGeometry,
"ID: ", m_lpThisAB->ID());
167 delete m_vxCollisionGeometry;
168 m_vxCollisionGeometry = NULL;
170 int iMaterialID = m_lpThisAB->GetSimulator()->GetMaterialID(m_lpThisRB->MaterialID());
171 CollisionGeometry(m_vxSensor->addGeometry(m_vxGeometry, iMaterialID, 0, m_lpThisRB->Density()));
173 CalculateEstimatedMassAndVolume();
177 void VsMeshBase::Physics_Resize()
180 if(m_osgNode.valid() && m_osgMeshNode.valid())
182 osg::Group *osgGroup =
dynamic_cast<osg::Group *
>(m_osgNode.get());
184 if(osgGroup && osgGroup->containsNode(m_osgMeshNode.get()))
185 osgGroup->removeChild(m_osgMeshNode.get());
187 m_osgGeometry.release();
188 m_osgMeshNode.release();
189 m_osgBaseMeshNode.release();
195 if(m_osgDragger.valid())
196 m_osgDragger->SetupMatrix();
199 if(m_osgNodeGroup.valid())
202 osgVisitor->traverse(*m_osgNodeGroup);
208 ResizePhysicsGeometry();
211 Physics_SetDensity(m_lpThisRB->Density());
float m_fltEstimatedVolume
The estimated volume. See m_fltEstimatedMass desciption.
float Width()
Gets the width = (Max.z - Min.z).
float Length()
Gets the length = (Max.x - Min.x).
Bounding box class for geometric objects.
float Height()
Gets the height = (Max.y - Min.y).
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
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.