AnimatLab  2
Test
BlMesh.cpp
1 // BlMesh.cpp: implementation of the BlMesh class.
2 //
4 
5 #include "StdAfx.h"
6 #include "BlOsgGeometry.h"
7 #include "BlJoint.h"
8 #include "BlMotorizedJoint.h"
9 #include "BlRigidBody.h"
10 #include "BlMeshBase.h"
11 #include "BlMesh.h"
12 #include "BlSimulator.h"
13 
14 namespace BulletAnimatSim
15 {
16  namespace Environment
17  {
18  namespace Bodies
19  {
20 
22 // Construction/Destruction
24 
25 BlMesh::BlMesh()
26 {
27  SetThisPointers();
28 }
29 
30 BlMesh::~BlMesh()
31 {
32  try
33  {
34  DeleteGraphics();
35  DeletePhysics(false);
36  }
37  catch(...)
38  {Std_TraceMsg(0, "Caught Error in desctructor of BlMesh/\r\n", "", -1, false, true);}
39 }
40 
53 {
54  if(m_strCollisionMeshType == "CONVEX")
55  return Mesh::Freeze();
56  else
57  return true;
58 }
59 
60 BoundingBox BlMesh::Physics_GetBoundingBox()
61 {
62  BoundingBox abb;
63 
64  if(m_osgNode.valid())
65  {
66  OsgCalculateBoundingBox bbox ;
67  m_osgNode->accept( bbox );
68  osg::BoundingBox bb = bbox.getBoundBox();
69  abb.Set(bb.xMin(), bb.yMin(), bb.zMin(), bb.xMax(), bb.yMax(), bb.zMax());
70  }
71  else
72  {
73  abb.Set(-0.5, -0.5, -0.5, 0.5, 0.5, 0.5);
74  }
75 
76  return abb;
77 }
78 
80 {
81  CreateGeometry();
82 
83  CalculateVolumeAndAreas();
84 
85  BlMeshBase::CreateItem();
86  Mesh::CreateParts();
87 }
88 
89 
90 void BlMesh::CalculateVolumeAndAreas()
91 {
92  //If this is a collision object then calculate the convex hull volume.
93  if(m_lpThisRB->IsCollisionObject())
94  {
95  m_fltVolume = OsgConvexHullVolume(m_osgMeshNode.get());
96 
97  //We are going to approximate the areas for each axis using the bounding box. I realize this is not
98  //horribly accurate, but it will do for now.
99  BoundingBox box = Physics_GetBoundingBox();
100  m_vArea.x = box.Height() * box.Width();
101  m_vArea.y = box.Length() * box.Width();
102  m_vArea.z = box.Length() * box.Height();
103  }
104  else
105  {
106  m_fltVolume = 0;
107  }
108 
109 
110  if(m_fltMass < 0)
111  {
112  float fltMass = m_fltVolume * m_fltDensity;
113  Mass(fltMass, false, false);
114  }
115 }
116 
118 {
121 
122  Mesh::CreateJoints();
123  BlMeshBase::Initialize();
124 }
125 
126  } //Bodies
127  } // Environment
128 } //BulletAnimatSim
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
Definition: BlMesh.cpp:79
std::string m_strCollisionMeshType
Type of the collision mesh. Can be Convex or Triangular.
Definition: Mesh.h:18
float m_fltMass
The mass of the object.
Definition: RigidBody.h:85
float m_fltVolume
The volume for the rigid body.
Definition: RigidBody.h:91
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void CreateJoint()
Creates the joint.
Definition: Joint.cpp:602
float m_fltDensity
Uniform density for the rigid body.
Definition: RigidBody.h:79
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.
virtual void CreateJoints()
Allows the rigid body to create its joints using the chosen physics engine.
Definition: BlMesh.cpp:117
virtual bool Freeze()
Freezes this object. In Bullet triangular meshes are not allowed to be dynamic, so I am overriding th...
Definition: BlMesh.cpp:52