Bullet Collision Detection & Physics Library: btCompoundFromGimpact.h Source File

Bullet Collision Detection & Physics Library

btCompoundFromGimpact.h
Go to the documentation of this file.
1#ifndef BT_COMPOUND_FROM_GIMPACT
2#define BT_COMPOUND_FROM_GIMPACT
3
4#include "BulletCollision/CollisionShapes/btCompoundShape.h"
5#include "btGImpactShape.h"
6#include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
7
8 ATTRIBUTE_ALIGNED16(class)
9 btCompoundFromGimpactShape : public btCompoundShape
10{
11public:
12 BT_DECLARE_ALIGNED_ALLOCATOR();
13
14 virtual ~btCompoundFromGimpactShape()
15 {
16 /*delete all the btBU_Simplex1to4 ChildShapes*/
17 for (int i = 0; i < m_children.size(); i++)
18 {
19 delete m_children[i].m_childShape;
20 }
21 }
22};
23
24 struct MyCallback : public btTriangleRaycastCallback
25{
26 int m_ignorePart;
27 int m_ignoreTriangleIndex;
28
29 MyCallback(const btVector3& from, const btVector3& to, int ignorePart, int ignoreTriangleIndex)
30 : btTriangleRaycastCallback(from, to),
31 m_ignorePart(ignorePart),
32 m_ignoreTriangleIndex(ignoreTriangleIndex)
33 {
34 }
35 virtual btScalar reportHit(const btVector3& hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)
36 {
37 if (partId != m_ignorePart || triangleIndex != m_ignoreTriangleIndex)
38 {
39 if (hitFraction < m_hitFraction)
40 return hitFraction;
41 }
42
43 return m_hitFraction;
44 }
45};
46 struct MyInternalTriangleIndexCallback : public btInternalTriangleIndexCallback
47{
48 const btGImpactMeshShape* m_gimpactShape;
49 btCompoundShape* m_colShape;
50 btScalar m_depth;
51
52 MyInternalTriangleIndexCallback(btCompoundShape* colShape, const btGImpactMeshShape* meshShape, btScalar depth)
53 : m_colShape(colShape),
54 m_gimpactShape(meshShape),
55 m_depth(depth)
56 {
57 }
58
59 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
60 {
61 btVector3 scale = m_gimpactShape->getLocalScaling();
62 btVector3 v0 = triangle[0] * scale;
63 btVector3 v1 = triangle[1] * scale;
64 btVector3 v2 = triangle[2] * scale;
65
66 btVector3 centroid = (v0 + v1 + v2) / 3;
67 btVector3 normal = (v1 - v0).cross(v2 - v0);
68 normal.normalize();
69 btVector3 rayFrom = centroid;
70 btVector3 rayTo = centroid - normal * m_depth;
71
72 MyCallback cb(rayFrom, rayTo, partId, triangleIndex);
73
74 m_gimpactShape->processAllTrianglesRay(&cb, rayFrom, rayTo);
75 if (cb.m_hitFraction < 1)
76 {
77 rayTo.setInterpolate3(cb.m_from, cb.m_to, cb.m_hitFraction);
78 //rayTo = cb.m_from;
79 //rayTo = rayTo.lerp(cb.m_to,cb.m_hitFraction);
80 //gDebugDraw.drawLine(tr(centroid),tr(centroid+normal),btVector3(1,0,0));
81 }
82
83 btBU_Simplex1to4* tet = new btBU_Simplex1to4(v0, v1, v2, rayTo);
84 btTransform ident;
85 ident.setIdentity();
86 m_colShape->addChildShape(ident, tet);
87 }
88};
89
90 btCompoundShape* btCreateCompoundFromGimpactShape(const btGImpactMeshShape* gimpactMesh, btScalar depth)
91{
92 btCompoundShape* colShape = new btCompoundFromGimpactShape();
93
94 btTransform tr;
95 tr.setIdentity();
96
97 MyInternalTriangleIndexCallback cb(colShape, gimpactMesh, depth);
98 btVector3 aabbMin, aabbMax;
99 gimpactMesh->getAabb(tr, aabbMin, aabbMax);
100 gimpactMesh->getMeshInterface()->InternalProcessAllTriangles(&cb, aabbMin, aabbMax);
101
102 return colShape;
103}
104
105#endif //BT_COMPOUND_FROM_GIMPACT
btCreateCompoundFromGimpactShape
btCompoundShape * btCreateCompoundFromGimpactShape(const btGImpactMeshShape *gimpactMesh, btScalar depth)
Definition btCompoundFromGimpact.h:90
btCompoundShape.h
btGImpactShape.h
btRaycastCallback.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition btScalar.h:99
btBU_Simplex1to4
The btBU_Simplex1to4 implements tetrahedron, triangle, line, vertex collision shapes....
Definition btTetrahedronShape.h:25
btCompoundFromGimpactShape
Definition btCompoundFromGimpact.h:10
btCompoundFromGimpactShape::BT_DECLARE_ALIGNED_ALLOCATOR
BT_DECLARE_ALIGNED_ALLOCATOR()
btCompoundFromGimpactShape::~btCompoundFromGimpactShape
virtual ~btCompoundFromGimpactShape()
Definition btCompoundFromGimpact.h:14
btCompoundShape
The btCompoundShape allows to store multiple other btCollisionShapes This allows for moving concave c...
Definition btCompoundShape.h:57
btCompoundShape::btCompoundShape
btCompoundShape(bool enableDynamicAabbTree=true, const int initialChildCapacity=0)
Definition btCompoundShape.cpp:21
btCompoundShape::m_children
btAlignedObjectArray< btCompoundShapeChild > m_children
Definition btCompoundShape.h:59
btGImpactMeshShape
This class manages a mesh supplied by the btStridingMeshInterface interface.
Definition btGImpactShape.h:851
btGImpactMeshShape::getMeshInterface
btStridingMeshInterface * getMeshInterface()
Definition btGImpactShape.h:895
btGImpactShapeInterface::getAabb
void getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const
If the Bounding box is not updated, then this class attemps to calculate it.
Definition btGImpactShape.h:126
btInternalTriangleIndexCallback
Definition btTriangleCallback.h:31
btStridingMeshInterface::InternalProcessAllTriangles
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Definition btStridingMeshInterface.cpp:23
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:30
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition btTransform.h:167
btTriangleRaycastCallback::m_hitFraction
btScalar m_hitFraction
Definition btRaycastCallback.h:45
btTriangleRaycastCallback::m_to
btVector3 m_to
Definition btRaycastCallback.h:29
btTriangleRaycastCallback::m_from
btVector3 m_from
Definition btRaycastCallback.h:28
btTriangleRaycastCallback::btTriangleRaycastCallback
btTriangleRaycastCallback(const btVector3 &from, const btVector3 &to, unsigned int flags=0)
Definition btRaycastCallback.cpp:26
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btVector3::setInterpolate3
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition btVector3.h:492
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
MyCallback
Definition btCompoundFromGimpact.h:25
MyCallback::m_ignoreTriangleIndex
int m_ignoreTriangleIndex
Definition btCompoundFromGimpact.h:27
MyCallback::m_ignorePart
int m_ignorePart
Definition btCompoundFromGimpact.h:26
MyCallback::reportHit
virtual btScalar reportHit(const btVector3 &hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)
Definition btCompoundFromGimpact.h:35
MyCallback::MyCallback
MyCallback(const btVector3 &from, const btVector3 &to, int ignorePart, int ignoreTriangleIndex)
Definition btCompoundFromGimpact.h:29
MyInternalTriangleIndexCallback
Definition btCompoundFromGimpact.h:47
MyInternalTriangleIndexCallback::m_gimpactShape
const btGImpactMeshShape * m_gimpactShape
Definition btCompoundFromGimpact.h:48
MyInternalTriangleIndexCallback::m_depth
btScalar m_depth
Definition btCompoundFromGimpact.h:50
MyInternalTriangleIndexCallback::MyInternalTriangleIndexCallback
MyInternalTriangleIndexCallback(btCompoundShape *colShape, const btGImpactMeshShape *meshShape, btScalar depth)
Definition btCompoundFromGimpact.h:52
MyInternalTriangleIndexCallback::internalProcessTriangleIndex
virtual void internalProcessTriangleIndex(btVector3 *triangle, int partId, int triangleIndex)
Definition btCompoundFromGimpact.h:59
MyInternalTriangleIndexCallback::m_colShape
btCompoundShape * m_colShape
Definition btCompoundFromGimpact.h:49