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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btGhostObject.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "btGhostObject.h"
17 #include "btCollisionWorld.h"
18 #include "BulletCollision/CollisionShapes/btConvexShape.h"
19 #include "LinearMath/btAabbUtil2.h"
20
21 btGhostObject::btGhostObject()
22 {
23 m_internalType = CO_GHOST_OBJECT;
24 }
25
26 btGhostObject::~btGhostObject()
27 {
29 btAssert(!m_overlappingObjects.size());
30 }
31
32
33 void btGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btBroadphaseProxy* thisProxy)
34 {
35 btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
36 btAssert(otherObject);
38 int index = m_overlappingObjects.findLinearSearch(otherObject);
39 if (index==m_overlappingObjects.size())
40 {
41 //not found
42 m_overlappingObjects.push_back(otherObject);
43 }
44 }
45
46 void btGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btDispatcher* dispatcher,btBroadphaseProxy* thisProxy)
47 {
48 btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
49 btAssert(otherObject);
50 int index = m_overlappingObjects.findLinearSearch(otherObject);
51 if (index<m_overlappingObjects.size())
52 {
53 m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size()-1];
54 m_overlappingObjects.pop_back();
55 }
56 }
57
58
59 btPairCachingGhostObject::btPairCachingGhostObject()
60 {
61 m_hashPairCache = new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16)) btHashedOverlappingPairCache();
62 }
63
64 btPairCachingGhostObject::~btPairCachingGhostObject()
65 {
66 m_hashPairCache->~btHashedOverlappingPairCache();
67 btAlignedFree( m_hashPairCache );
68 }
69
70 void btPairCachingGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btBroadphaseProxy* thisProxy)
71 {
72 btBroadphaseProxy*actualThisProxy = thisProxy ? thisProxy : getBroadphaseHandle();
73 btAssert(actualThisProxy);
74
75 btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
76 btAssert(otherObject);
77 int index = m_overlappingObjects.findLinearSearch(otherObject);
78 if (index==m_overlappingObjects.size())
79 {
80 m_overlappingObjects.push_back(otherObject);
81 m_hashPairCache->addOverlappingPair(actualThisProxy,otherProxy);
82 }
83 }
84
85 void btPairCachingGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btDispatcher* dispatcher,btBroadphaseProxy* thisProxy1)
86 {
87 btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
88 btBroadphaseProxy* actualThisProxy = thisProxy1 ? thisProxy1 : getBroadphaseHandle();
89 btAssert(actualThisProxy);
90
91 btAssert(otherObject);
92 int index = m_overlappingObjects.findLinearSearch(otherObject);
93 if (index<m_overlappingObjects.size())
94 {
95 m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size()-1];
96 m_overlappingObjects.pop_back();
97 m_hashPairCache->removeOverlappingPair(actualThisProxy,otherProxy,dispatcher);
98 }
99 }
100
101
102 void btGhostObject::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration) const
103 {
104 btTransform convexFromTrans,convexToTrans;
105 convexFromTrans = convexFromWorld;
106 convexToTrans = convexToWorld;
107 btVector3 castShapeAabbMin, castShapeAabbMax;
108 /* Compute AABB that encompasses angular movement */
109 {
110 btVector3 linVel, angVel;
111 btTransformUtil::calculateVelocity (convexFromTrans, convexToTrans, 1.0, linVel, angVel);
112 btTransform R;
113 R.setIdentity ();
114 R.setRotation (convexFromTrans.getRotation());
115 castShape->calculateTemporalAabb (R, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax);
116 }
117
119 // do a ray-shape query using convexCaster (CCD)
120 int i;
121 for (i=0;i<m_overlappingObjects.size();i++)
122 {
123 btCollisionObject* collisionObject= m_overlappingObjects[i];
124 //only perform raycast if filterMask matches
125 if(resultCallback.needsCollision(collisionObject->getBroadphaseHandle())) {
126 //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
127 btVector3 collisionObjectAabbMin,collisionObjectAabbMax;
128 collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(),collisionObjectAabbMin,collisionObjectAabbMax);
129 AabbExpand (collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
130 btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
131 btVector3 hitNormal;
132 if (btRayAabb(convexFromWorld.getOrigin(),convexToWorld.getOrigin(),collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,hitNormal))
133 {
134 btCollisionWorld::objectQuerySingle(castShape, convexFromTrans,convexToTrans,
135 collisionObject,
136 collisionObject->getCollisionShape(),
137 collisionObject->getWorldTransform(),
138 resultCallback,
139 allowedCcdPenetration);
140 }
141 }
142 }
143
144 }
145
146 void btGhostObject::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const
147 {
148 btTransform rayFromTrans;
149 rayFromTrans.setIdentity();
150 rayFromTrans.setOrigin(rayFromWorld);
151 btTransform rayToTrans;
152 rayToTrans.setIdentity();
153 rayToTrans.setOrigin(rayToWorld);
154
155
156 int i;
157 for (i=0;i<m_overlappingObjects.size();i++)
158 {
159 btCollisionObject* collisionObject= m_overlappingObjects[i];
160 //only perform raycast if filterMask matches
161 if(resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
162 {
163 btCollisionWorld::rayTestSingle(rayFromTrans,rayToTrans,
164 collisionObject,
165 collisionObject->getCollisionShape(),
166 collisionObject->getWorldTransform(),
167 resultCallback);
168 }
169 }
170 }
171
btTransform::setOrigin
void setOrigin(const btVector3 &origin)
Set the translational element.
Definition: btTransform.h:150
btGhostObject.h
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:276
btPairCachingGhostObject::~btPairCachingGhostObject
virtual ~btPairCachingGhostObject()
Definition: btGhostObject.cpp:64
btCollisionWorld.h
btTransform::getRotation
btQuaternion getRotation() const
Return a quaternion representing the rotation.
Definition: btTransform.h:122
btPairCachingGhostObject::removeOverlappingObjectInternal
virtual void removeOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btDispatcher *dispatcher, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:85
btGhostObject::rayTest
void rayTest(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, btCollisionWorld::RayResultCallback &resultCallback) const
Definition: btGhostObject.cpp:146
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
btAssert
#define btAssert(x)
Definition: btScalar.h:113
btCollisionWorld::RayResultCallback
RayResultCallback is used to report new raycast results.
Definition: btCollisionWorld.h:204
btPairCachingGhostObject::btPairCachingGhostObject
btPairCachingGhostObject()
Definition: btGhostObject.cpp:59
btCollisionShape::getAabb
virtual void getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const =0
getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t...
btConvexShape.h
btGhostObject::~btGhostObject
virtual ~btGhostObject()
Definition: btGhostObject.cpp:26
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
AabbExpand
void AabbExpand(btVector3 &aabbMin, btVector3 &aabbMax, const btVector3 &expansionMin, const btVector3 &expansionMax)
Definition: btAabbUtil2.h:26
btCollisionObject::getWorldTransform
btTransform & getWorldTransform()
Definition: btCollisionObject.h:327
btCollisionObject::m_internalType
int m_internalType
m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody, btSoftBody, btGhostObject etc.
Definition: btCollisionObject.h:92
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
btTransform::getOrigin
btVector3 & getOrigin()
Return the origin vector translation.
Definition: btTransform.h:117
btCollisionObject::getBroadphaseHandle
btBroadphaseProxy * getBroadphaseHandle()
Definition: btCollisionObject.h:344
btCollisionShape::calculateTemporalAabb
void calculateTemporalAabb(const btTransform &curTrans, const btVector3 &linvel, const btVector3 &angvel, btScalar timeStep, btVector3 &temporalAabbMin, btVector3 &temporalAabbMax) const
calculateTemporalAabb calculates the enclosing aabb for the moving object over interval [0...
Definition: btCollisionShape.cpp:60
btAabbUtil2.h
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:42
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:49
btTransform::setRotation
void setRotation(const btQuaternion &q)
Set the rotational element by btQuaternion.
Definition: btTransform.h:165
btBroadphaseProxy::m_clientObject
void * m_clientObject
Definition: btBroadphaseProxy.h:103
btCollisionWorld::rayTestSingle
static void rayTestSingle(const btTransform &rayFromTrans, const btTransform &rayToTrans, btCollisionObject *collisionObject, const btCollisionShape *collisionShape, const btTransform &colObjWorldTransform, RayResultCallback &resultCallback)
rayTestSingle performs a raycast call and calls the resultCallback.
Definition: btCollisionWorld.cpp:262
btPairCachingGhostObject::addOverlappingObjectInternal
virtual void addOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:70
btAlignedObjectArray::pop_back
void pop_back()
Definition: btAlignedObjectArray.h:193
btCollisionWorld::ConvexResultCallback::needsCollision
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
Definition: btCollisionWorld.h:364
btBroadphaseProxy
The btBroadphaseProxy is the main class that can be used with the Bullet broadphases.
Definition: btBroadphaseProxy.h:85
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btHashedOverlappingPairCache::removeOverlappingPair
virtual void * removeOverlappingPair(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1, btDispatcher *dispatcher)
Definition: btOverlappingPairCache.cpp:273
btCollisionWorld::RayResultCallback::needsCollision
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
Definition: btCollisionWorld.h:231
btCollisionWorld::objectQuerySingle
static void objectQuerySingle(const btConvexShape *castShape, const btTransform &rayFromTrans, const btTransform &rayToTrans, btCollisionObject *collisionObject, const btCollisionShape *collisionShape, const btTransform &colObjWorldTransform, ConvexResultCallback &resultCallback, btScalar allowedPenetration)
objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
Definition: btCollisionWorld.cpp:572
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btGhostObject::removeOverlappingObjectInternal
virtual void removeOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btDispatcher *dispatcher, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:46
btPairCachingGhostObject::m_hashPairCache
btHashedOverlappingPairCache * m_hashPairCache
Definition: btGhostObject.h:101
btCollisionObject::CO_GHOST_OBJECT
CO_GHOST_OBJECT keeps track of all objects overlapping its AABB and that pass its collision filter It...
Definition: btCollisionObject.h:139
btAlignedObjectArray::findLinearSearch
int findLinearSearch(const T &key) const
Definition: btAlignedObjectArray.h:465
btGhostObject::btGhostObject
btGhostObject()
Definition: btGhostObject.cpp:21
btGhostObject::convexSweepTest
void convexSweepTest(const class btConvexShape *castShape, const btTransform &convexFromWorld, const btTransform &convexToWorld, btCollisionWorld::ConvexResultCallback &resultCallback, btScalar allowedCcdPenetration=0.f) const
Definition: btGhostObject.cpp:102
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:41
btHashedOverlappingPairCache::addOverlappingPair
virtual btBroadphasePair * addOverlappingPair(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1)
Definition: btOverlappingPairCache.h:128
btCollisionWorld::ConvexResultCallback
RayResultCallback is used to report new raycast results.
Definition: btCollisionWorld.h:340
btGhostObject::addOverlappingObjectInternal
virtual void addOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:33
btGhostObject::m_overlappingObjects
btAlignedObjectArray< btCollisionObject * > m_overlappingObjects
Definition: btGhostObject.h:38
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:69
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:215
btTransformUtil::calculateVelocity
static void calculateVelocity(const btTransform &transform0, const btTransform &transform1, btScalar timeStep, btVector3 &linVel, btVector3 &angVel)
Definition: btTransformUtil.h:112
btRayAabb
bool btRayAabb(const btVector3 &rayFrom, const btVector3 &rayTo, const btVector3 &aabbMin, const btVector3 &aabbMax, btScalar &param, btVector3 &normal)
Definition: btAabbUtil2.h:125
btHashedOverlappingPairCache
Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman...
Definition: btOverlappingPairCache.h:93
btHashedOverlappingPairCache::~btHashedOverlappingPairCache
virtual ~btHashedOverlappingPairCache()
Definition: btOverlappingPairCache.cpp:48
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278