Bullet Collision Detection & Physics Library: btGImpactShape.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btGImpactShape.cpp
Go to the documentation of this file.
1/*
2This source file is part of GIMPACT Library.
3
4For the latest info, see http://gimpact.sourceforge.net/
5
6Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
7email: projectileman@yahoo.com
8
9
10This software is provided 'as-is', without any express or implied warranty.
11In no event will the authors be held liable for any damages arising from the use of this software.
12Permission is granted to anyone to use this software for any purpose,
13including commercial applications, and to alter it and redistribute it freely,
14subject to the following restrictions:
15
161. 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.
172. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20
21#include "btGImpactShape.h"
22#include "btGImpactMassUtil.h"
23
24 btGImpactMeshShapePart::btGImpactMeshShapePart(btStridingMeshInterface* meshInterface, int part)
25{
26 // moved from .h to .cpp because of conditional compilation
27 // (The setting of BT_THREADSAFE may differ between various cpp files, so it is best to
28 // avoid using it in h files)
29 m_primitive_manager.m_meshInterface = meshInterface;
30 m_primitive_manager.m_part = part;
31 m_box_set.setPrimitiveManager(&m_primitive_manager);
32#if BT_THREADSAFE
33 // If threadsafe is requested, this object uses a different lock/unlock
34 // model with the btStridingMeshInterface -- lock once when the object is constructed
35 // and unlock once in the destructor.
36 // The other way of locking and unlocking for each collision check in the narrowphase
37 // is not threadsafe. Note these are not thread-locks, they are calls to the meshInterface's
38 // getLockedReadOnlyVertexIndexBase virtual function, which by default just returns a couple of
39 // pointers. In theory a client could override the lock function to do all sorts of
40 // things like reading data from GPU memory, or decompressing data on the fly, but such things
41 // do not seem all that likely or useful, given the performance cost.
42 m_primitive_manager.lock();
43#endif
44}
45
46btGImpactMeshShapePart::~btGImpactMeshShapePart()
47{
48 // moved from .h to .cpp because of conditional compilation
49#if BT_THREADSAFE
50 m_primitive_manager.unlock();
51#endif
52}
53
54 void btGImpactMeshShapePart::lockChildShapes() const
55{
56 // moved from .h to .cpp because of conditional compilation
57#if !BT_THREADSAFE
58 // called in the narrowphase -- not threadsafe!
59 void* dummy = (void*)(m_box_set.getPrimitiveManager());
60 TrimeshPrimitiveManager* dummymanager = static_cast<TrimeshPrimitiveManager*>(dummy);
61 dummymanager->lock();
62#endif
63}
64
65 void btGImpactMeshShapePart::unlockChildShapes() const
66{
67 // moved from .h to .cpp because of conditional compilation
68#if !BT_THREADSAFE
69 // called in the narrowphase -- not threadsafe!
70 void* dummy = (void*)(m_box_set.getPrimitiveManager());
71 TrimeshPrimitiveManager* dummymanager = static_cast<TrimeshPrimitiveManager*>(dummy);
72 dummymanager->unlock();
73#endif
74}
75
76#define CALC_EXACT_INERTIA 1
77
78 void btGImpactCompoundShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
79{
80 lockChildShapes();
81#ifdef CALC_EXACT_INERTIA
82 inertia.setValue(0.f, 0.f, 0.f);
83
84 int i = this->getNumChildShapes();
85 btScalar shapemass = mass / btScalar(i);
86
87 while (i--)
88 {
89 btVector3 temp_inertia;
90 m_childShapes[i]->calculateLocalInertia(shapemass, temp_inertia);
91 if (childrenHasTransform())
92 {
93 inertia = gim_inertia_add_transformed(inertia, temp_inertia, m_childTransforms[i]);
94 }
95 else
96 {
97 inertia = gim_inertia_add_transformed(inertia, temp_inertia, btTransform::getIdentity());
98 }
99 }
100
101#else
102
103 // Calc box inertia
104
105 btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
106 btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
107 btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
108 const btScalar x2 = lx * lx;
109 const btScalar y2 = ly * ly;
110 const btScalar z2 = lz * lz;
111 const btScalar scaledmass = mass * btScalar(0.08333333);
112
113 inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
114
115#endif
116 unlockChildShapes();
117}
118
119 void btGImpactMeshShapePart::calculateLocalInertia(btScalar mass, btVector3& inertia) const
120{
121 lockChildShapes();
122
123#ifdef CALC_EXACT_INERTIA
124 inertia.setValue(0.f, 0.f, 0.f);
125
126 int i = this->getVertexCount();
127 btScalar pointmass = mass / btScalar(i);
128
129 while (i--)
130 {
131 btVector3 pointintertia;
132 this->getVertex(i, pointintertia);
133 pointintertia = gim_get_point_inertia(pointintertia, pointmass);
134 inertia += pointintertia;
135 }
136
137#else
138
139 // Calc box inertia
140
141 btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
142 btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
143 btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
144 const btScalar x2 = lx * lx;
145 const btScalar y2 = ly * ly;
146 const btScalar z2 = lz * lz;
147 const btScalar scaledmass = mass * btScalar(0.08333333);
148
149 inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
150
151#endif
152
153 unlockChildShapes();
154}
155
156 void btGImpactMeshShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
157{
158#ifdef CALC_EXACT_INERTIA
159 inertia.setValue(0.f, 0.f, 0.f);
160
161 int i = this->getMeshPartCount();
162 btScalar partmass = mass / btScalar(i);
163
164 while (i--)
165 {
166 btVector3 partinertia;
167 getMeshPart(i)->calculateLocalInertia(partmass, partinertia);
168 inertia += partinertia;
169 }
170
171#else
172
173 // Calc box inertia
174
175 btScalar lx = m_localAABB.m_max[0] - m_localAABB.m_min[0];
176 btScalar ly = m_localAABB.m_max[1] - m_localAABB.m_min[1];
177 btScalar lz = m_localAABB.m_max[2] - m_localAABB.m_min[2];
178 const btScalar x2 = lx * lx;
179 const btScalar y2 = ly * ly;
180 const btScalar z2 = lz * lz;
181 const btScalar scaledmass = mass * btScalar(0.08333333);
182
183 inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
184
185#endif
186}
187
188 void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
189{
190}
191
192 void btGImpactMeshShapePart::processAllTrianglesRay(btTriangleCallback* callback, const btVector3& rayFrom, const btVector3& rayTo) const
193{
194 lockChildShapes();
195
196 btAlignedObjectArray<int> collided;
197 btVector3 rayDir(rayTo - rayFrom);
198 rayDir.normalize();
199 m_box_set.rayQuery(rayDir, rayFrom, collided);
200
201 if (collided.size() == 0)
202 {
203 unlockChildShapes();
204 return;
205 }
206
207 int part = (int)getPart();
208 btPrimitiveTriangle triangle;
209 int i = collided.size();
210 while (i--)
211 {
212 getPrimitiveTriangle(collided[i], triangle);
213 callback->processTriangle(triangle.m_vertices, part, collided[i]);
214 }
215 unlockChildShapes();
216}
217
218 void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
219{
220 lockChildShapes();
221 btAABB box;
222 box.m_min = aabbMin;
223 box.m_max = aabbMax;
224
225 btAlignedObjectArray<int> collided;
226 m_box_set.boxQuery(box, collided);
227
228 if (collided.size() == 0)
229 {
230 unlockChildShapes();
231 return;
232 }
233
234 int part = (int)getPart();
235 btPrimitiveTriangle triangle;
236 int i = collided.size();
237 while (i--)
238 {
239 this->getPrimitiveTriangle(collided[i], triangle);
240 callback->processTriangle(triangle.m_vertices, part, collided[i]);
241 }
242 unlockChildShapes();
243}
244
245 void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
246{
247 int i = m_mesh_parts.size();
248 while (i--)
249 {
250 m_mesh_parts[i]->processAllTriangles(callback, aabbMin, aabbMax);
251 }
252}
253
254 void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback, const btVector3& rayFrom, const btVector3& rayTo) const
255{
256 int i = m_mesh_parts.size();
257 while (i--)
258 {
259 m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
260 }
261}
262
264 const char* btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
265{
266 btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*)dataBuffer;
267
268 btCollisionShape::serialize(&trimeshData->m_collisionShapeData, serializer);
269
270 m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
271
272 trimeshData->m_collisionMargin = float(m_collisionMargin);
273
274 localScaling.serializeFloat(trimeshData->m_localScaling);
275
276 trimeshData->m_gimpactSubType = int(getGImpactShapeType());
277
278 return "btGImpactMeshShapeData";
279}
btGImpactMassUtil.h
gim_get_point_inertia
btVector3 gim_get_point_inertia(const btVector3 &point, btScalar mass)
Definition btGImpactMassUtil.h:48
gim_inertia_add_transformed
btVector3 gim_inertia_add_transformed(const btVector3 &source_inertia, const btVector3 &added_inertia, const btTransform &transform)
Definition btGImpactMassUtil.h:29
btGImpactShape.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
btAABB
Axis aligned box.
Definition btBoxCollision.h:216
btAABB::m_min
btVector3 m_min
Definition btBoxCollision.h:218
btAABB::m_max
btVector3 m_max
Definition btBoxCollision.h:219
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition btAlignedObjectArray.h:46
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition btAlignedObjectArray.h:142
btCollisionShape::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition btCollisionShape.cpp:96
btConcaveShape::m_collisionMargin
btScalar m_collisionMargin
Definition btConcaveShape.h:41
btGImpactCompoundShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Calculates the exact inertia tensor for this shape.
Definition btGImpactShape.cpp:78
btGImpactCompoundShape::m_childTransforms
btAlignedObjectArray< btTransform > m_childTransforms
Definition btGImpactShape.h:355
btGImpactCompoundShape::m_childShapes
btAlignedObjectArray< btCollisionShape * > m_childShapes
Definition btGImpactShape.h:356
btGImpactCompoundShape::childrenHasTransform
virtual bool childrenHasTransform() const
if true, then its children must get transforms.
Definition btGImpactShape.h:371
btGImpactCompoundShape::getNumChildShapes
virtual int getNumChildShapes() const
Gets the number of children.
Definition btGImpactShape.h:390
btGImpactMeshShapePart::TrimeshPrimitiveManager
Trimesh primitive manager.
Definition btGImpactShape.h:510
btGImpactMeshShapePart::TrimeshPrimitiveManager::m_meshInterface
btStridingMeshInterface * m_meshInterface
Definition btGImpactShape.h:513
btGImpactMeshShapePart::TrimeshPrimitiveManager::lock
void lock()
Definition btGImpactShape.h:575
btGImpactMeshShapePart::TrimeshPrimitiveManager::unlock
void unlock()
Definition btGImpactShape.h:589
btGImpactMeshShapePart::TrimeshPrimitiveManager::m_part
int m_part
Definition btGImpactShape.h:515
btGImpactMeshShapePart::processAllTriangles
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Function for retrieve triangles.
Definition btGImpactShape.cpp:218
btGImpactMeshShapePart::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Definition btGImpactShape.cpp:119
btGImpactMeshShapePart::btGImpactMeshShapePart
btGImpactMeshShapePart()
Definition btGImpactShape.h:695
btGImpactMeshShapePart::processAllTrianglesRay
virtual void processAllTrianglesRay(btTriangleCallback *callback, const btVector3 &rayFrom, const btVector3 &rayTo) const
Function for retrieve triangles.
Definition btGImpactShape.cpp:192
btGImpactMeshShapePart::getPart
int getPart() const
Definition btGImpactShape.h:833
btGImpactMeshShapePart::getVertex
void getVertex(int vertex_index, btVector3 &vertex) const
Definition btGImpactShape.h:806
btGImpactMeshShapePart::m_primitive_manager
TrimeshPrimitiveManager m_primitive_manager
Definition btGImpactShape.h:692
btGImpactMeshShapePart::getVertexCount
int getVertexCount() const
Definition btGImpactShape.h:801
btGImpactMeshShapePart::lockChildShapes
virtual void lockChildShapes() const
call when reading child shapes
Definition btGImpactShape.cpp:54
btGImpactMeshShapePart::~btGImpactMeshShapePart
virtual ~btGImpactMeshShapePart()
Definition btGImpactShape.cpp:46
btGImpactMeshShapePart::unlockChildShapes
virtual void unlockChildShapes() const
Definition btGImpactShape.cpp:65
btGImpactMeshShape::getMeshPartCount
int getMeshPartCount() const
Definition btGImpactShape.h:905
btGImpactMeshShape::rayTest
virtual void rayTest(const btVector3 &rayFrom, const btVector3 &rayTo, btCollisionWorld::RayResultCallback &resultCallback) const
virtual method for ray collision
Definition btGImpactShape.cpp:188
btGImpactMeshShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Definition btGImpactShape.cpp:156
btGImpactMeshShape::getMeshPart
btGImpactMeshShapePart * getMeshPart(int index)
Definition btGImpactShape.h:910
btGImpactMeshShape::m_meshInterface
btStridingMeshInterface * m_meshInterface
Definition btGImpactShape.h:852
btGImpactMeshShape::processAllTriangles
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Function for retrieve triangles.
Definition btGImpactShape.cpp:245
btGImpactMeshShape::processAllTrianglesRay
virtual void processAllTrianglesRay(btTriangleCallback *callback, const btVector3 &rayFrom, const btVector3 &rayTo) const
Function for retrieve triangles.
Definition btGImpactShape.cpp:254
btGImpactMeshShape::getGImpactShapeType
virtual eGIMPACT_SHAPE_TYPE getGImpactShapeType() const
Subshape member functions.
Definition btGImpactShape.h:1070
btGImpactMeshShape::m_mesh_parts
btAlignedObjectArray< btGImpactMeshShapePart * > m_mesh_parts
Definition btGImpactShape.h:855
btGImpactMeshShape::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition btGImpactShape.cpp:264
btGImpactQuantizedBvh::rayQuery
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
Definition btGImpactQuantizedBvh.cpp:327
btGImpactQuantizedBvh::getPrimitiveManager
btPrimitiveManagerBase * getPrimitiveManager() const
Definition btGImpactQuantizedBvh.h:191
btGImpactQuantizedBvh::boxQuery
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
Definition btGImpactQuantizedBvh.cpp:286
btGImpactQuantizedBvh::setPrimitiveManager
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition btGImpactQuantizedBvh.h:186
btGImpactShapeInterface::localScaling
btVector3 localScaling
Definition btGImpactShape.h:78
btGImpactShapeInterface::getPrimitiveTriangle
void getPrimitiveTriangle(int index, btPrimitiveTriangle &triangle) const
if this trimesh
Definition btGImpactShape.h:226
btGImpactShapeInterface::lockChildShapes
virtual void lockChildShapes() const
call when reading child shapes
Definition btGImpactShape.h:217
btGImpactShapeInterface::m_box_set
btGImpactBoxSet m_box_set
Definition btGImpactShape.h:79
btGImpactShapeInterface::m_localAABB
btAABB m_localAABB
Definition btGImpactShape.h:76
btGImpactShapeInterface::unlockChildShapes
virtual void unlockChildShapes() const
Definition btGImpactShape.h:221
btPrimitiveTriangle
Definition btTriangleShapeEx.h:70
btPrimitiveTriangle::m_vertices
btVector3 m_vertices[3]
Definition btTriangleShapeEx.h:72
btSerializer
Definition btSerializer.h:66
btStridingMeshInterface
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
Definition btStridingMeshInterface.h:28
btStridingMeshInterface::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition btStridingMeshInterface.cpp:211
btTransform::getIdentity
static const btTransform & getIdentity()
Return an identity transform.
Definition btTransform.h:198
btTriangleCallback
The btTriangleCallback provides a callback for each overlapping triangle when calling processAllTrian...
Definition btTriangleCallback.h:24
btTriangleCallback::processTriangle
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)=0
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btVector3::serializeFloat
void serializeFloat(struct btVector3FloatData &dataOut) const
Definition btVector3.h:1291
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition btVector3.h:640
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
btCollisionWorld::RayResultCallback
RayResultCallback is used to report new raycast results.
Definition btCollisionWorld.h:196
btGImpactMeshShapeData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
Definition btGImpactShape.h:1098
btGImpactMeshShapeData::m_collisionShapeData
btCollisionShapeData m_collisionShapeData
Definition btGImpactShape.h:1099
btGImpactMeshShapeData::m_gimpactSubType
int m_gimpactSubType
Definition btGImpactShape.h:1107
btGImpactMeshShapeData::m_meshInterface
btStridingMeshInterfaceData m_meshInterface
Definition btGImpactShape.h:1101
btGImpactMeshShapeData::m_collisionMargin
float m_collisionMargin
Definition btGImpactShape.h:1105
btGImpactMeshShapeData::m_localScaling
btVector3FloatData m_localScaling
Definition btGImpactShape.h:1103