Bullet Collision Detection & Physics Library
btGImpactShape.cpp
Go to the documentation of this file.
1 /*
2 This source file is part of GIMPACT Library.
3 
4 For the latest info, see http://gimpact.sourceforge.net/
5 
6 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
7 email: projectileman@yahoo.com
8 
9 
10 This software is provided 'as-is', without any express or implied warranty.
11 In no event will the authors be held liable for any damages arising from the use of this software.
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it freely,
14 subject to the following restrictions:
15 
16 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19 */
20 
21 
22 #include "btGImpactShape.h"
23 #include "btGImpactMassUtil.h"
24 
25 
26 #define CALC_EXACT_INERTIA 1
27 
28 
30 {
32 #ifdef CALC_EXACT_INERTIA
33  inertia.setValue(0.f,0.f,0.f);
34 
35  int i = this->getNumChildShapes();
36  btScalar shapemass = mass/btScalar(i);
37 
38  while(i--)
39  {
40  btVector3 temp_inertia;
41  m_childShapes[i]->calculateLocalInertia(shapemass,temp_inertia);
43  {
44  inertia = gim_inertia_add_transformed( inertia,temp_inertia,m_childTransforms[i]);
45  }
46  else
47  {
48  inertia = gim_inertia_add_transformed( inertia,temp_inertia,btTransform::getIdentity());
49  }
50 
51  }
52 
53 #else
54 
55  // Calc box inertia
56 
60  const btScalar x2 = lx*lx;
61  const btScalar y2 = ly*ly;
62  const btScalar z2 = lz*lz;
63  const btScalar scaledmass = mass * btScalar(0.08333333);
64 
65  inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
66 
67 #endif
69 }
70 
71 
72 
74 {
76 
77 
78 #ifdef CALC_EXACT_INERTIA
79  inertia.setValue(0.f,0.f,0.f);
80 
81  int i = this->getVertexCount();
82  btScalar pointmass = mass/btScalar(i);
83 
84  while(i--)
85  {
86  btVector3 pointintertia;
87  this->getVertex(i,pointintertia);
88  pointintertia = gim_get_point_inertia(pointintertia,pointmass);
89  inertia+=pointintertia;
90  }
91 
92 #else
93 
94  // Calc box inertia
95 
99  const btScalar x2 = lx*lx;
100  const btScalar y2 = ly*ly;
101  const btScalar z2 = lz*lz;
102  const btScalar scaledmass = mass * btScalar(0.08333333);
103 
104  inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
105 
106 #endif
107 
109 }
110 
112 {
113 
114 #ifdef CALC_EXACT_INERTIA
115  inertia.setValue(0.f,0.f,0.f);
116 
117  int i = this->getMeshPartCount();
118  btScalar partmass = mass/btScalar(i);
119 
120  while(i--)
121  {
122  btVector3 partinertia;
123  getMeshPart(i)->calculateLocalInertia(partmass,partinertia);
124  inertia+=partinertia;
125  }
126 
127 #else
128 
129  // Calc box inertia
130 
134  const btScalar x2 = lx*lx;
135  const btScalar y2 = ly*ly;
136  const btScalar z2 = lz*lz;
137  const btScalar scaledmass = mass * btScalar(0.08333333);
138 
139  inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
140 
141 #endif
142 }
143 
144 void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
145 {
146 }
147 
149 {
150  lockChildShapes();
151 
152  btAlignedObjectArray<int> collided;
153  btVector3 rayDir(rayTo - rayFrom);
154  rayDir.normalize();
155  m_box_set.rayQuery(rayDir, rayFrom, collided);
156 
157  if(collided.size()==0)
158  {
160  return;
161  }
162 
163  int part = (int)getPart();
164  btPrimitiveTriangle triangle;
165  int i = collided.size();
166  while(i--)
167  {
168  getPrimitiveTriangle(collided[i],triangle);
169  callback->processTriangle(triangle.m_vertices,part,collided[i]);
170  }
172 }
173 
174 void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
175 {
176  lockChildShapes();
177  btAABB box;
178  box.m_min = aabbMin;
179  box.m_max = aabbMax;
180 
181  btAlignedObjectArray<int> collided;
182  m_box_set.boxQuery(box,collided);
183 
184  if(collided.size()==0)
185  {
187  return;
188  }
189 
190  int part = (int)getPart();
191  btPrimitiveTriangle triangle;
192  int i = collided.size();
193  while(i--)
194  {
195  this->getPrimitiveTriangle(collided[i],triangle);
196  callback->processTriangle(triangle.m_vertices,part,collided[i]);
197  }
199 
200 }
201 
202 void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
203 {
204  int i = m_mesh_parts.size();
205  while(i--)
206  {
207  m_mesh_parts[i]->processAllTriangles(callback,aabbMin,aabbMax);
208  }
209 }
210 
211 void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
212 {
213  int i = m_mesh_parts.size();
214  while(i--)
215  {
216  m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
217  }
218 }
219 
220 
222 const char* btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
223 {
224  btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*) dataBuffer;
225 
226  btCollisionShape::serialize(&trimeshData->m_collisionShapeData,serializer);
227 
228  m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
229 
230  trimeshData->m_collisionMargin = float(m_collisionMargin);
231 
233 
234  trimeshData->m_gimpactSubType = int(getGImpactShapeType());
235 
236  return "btGImpactMeshShapeData";
237 }
238 
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
virtual void rayTest(const btVector3 &rayFrom, const btVector3 &rayTo, btCollisionWorld::RayResultCallback &resultCallback) const
virtual method for ray collision
btVector3 m_max
void getVertex(int vertex_index, btVector3 &vertex) const
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)=0
btScalar m_collisionMargin
RayResultCallback is used to report new raycast results.
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
bool rayQuery(const btVector3 &ray_dir, const btVector3 &ray_origin, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
btVector3 m_min
btGImpactBoxSet m_box_set
virtual bool childrenHasTransform() const
if true, then its children must get transforms.
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:297
virtual eGIMPACT_SHAPE_TYPE getGImpactShapeType() const
Subshape member functions.
btGImpactMeshShapePart * getMeshPart(int index)
btAlignedObjectArray< btTransform > m_childTransforms
int size() const
return the number of elements in the array
The btTriangleCallback provides a callback for each overlapping triangle when calling processAllTrian...
virtual void processAllTrianglesRay(btTriangleCallback *callback, const btVector3 &rayFrom, const btVector3 &rayTo) const
Function for retrieve triangles.
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Function for retrieve triangles.
btVector3 gim_inertia_add_transformed(const btVector3 &source_inertia, const btVector3 &added_inertia, const btTransform &transform)
virtual void lockChildShapes() const
call when reading child shapes
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Function for retrieve triangles.
btStridingMeshInterfaceData m_meshInterface
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Axis aligned box.
virtual void unlockChildShapes() const
void serializeFloat(struct btVector3FloatData &dataOut) const
Definition: btVector3.h:1311
btCollisionShapeData m_collisionShapeData
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
virtual void lockChildShapes() const
call when reading child shapes
virtual void unlockChildShapes() const
static const btTransform & getIdentity()
Return an identity transform.
Definition: btTransform.h:203
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btStridingMeshInterface * m_meshInterface
btVector3 gim_get_point_inertia(const btVector3 &point, btScalar mass)
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Calculates the exact inertia tensor for this shape.
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
void getPrimitiveTriangle(int index, btPrimitiveTriangle &triangle) const
if this trimesh
btAlignedObjectArray< btGImpactMeshShapePart * > m_mesh_parts
btVector3FloatData m_localScaling
int getMeshPartCount() const
btAlignedObjectArray< btCollisionShape * > m_childShapes
virtual int getNumChildShapes() const
Gets the number of children.
virtual void processAllTrianglesRay(btTriangleCallback *callback, const btVector3 &rayFrom, const btVector3 &rayTo) const
Function for retrieve triangles.
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
int getVertexCount() const