Bullet Collision Detection & Physics Library
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"
19 #include "LinearMath/btAabbUtil2.h"
20 
22 {
24 }
25 
27 {
30 }
31 
33 {
34  btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
35  btAssert(otherObject);
37  int index = m_overlappingObjects.findLinearSearch(otherObject);
38  if (index == m_overlappingObjects.size())
39  {
40  //not found
41  m_overlappingObjects.push_back(otherObject);
42  }
43 }
44 
46 {
47  btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
48  btAssert(otherObject);
49  int index = m_overlappingObjects.findLinearSearch(otherObject);
50  if (index < m_overlappingObjects.size())
51  {
54  }
55 }
56 
58 {
60 }
61 
63 {
66 }
67 
69 {
70  btBroadphaseProxy* actualThisProxy = thisProxy ? thisProxy : getBroadphaseHandle();
71  btAssert(actualThisProxy);
72 
73  btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
74  btAssert(otherObject);
75  int index = m_overlappingObjects.findLinearSearch(otherObject);
76  if (index == m_overlappingObjects.size())
77  {
78  m_overlappingObjects.push_back(otherObject);
79  m_hashPairCache->addOverlappingPair(actualThisProxy, otherProxy);
80  }
81 }
82 
84 {
85  btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
86  btBroadphaseProxy* actualThisProxy = thisProxy1 ? thisProxy1 : getBroadphaseHandle();
87  btAssert(actualThisProxy);
88 
89  btAssert(otherObject);
90  int index = m_overlappingObjects.findLinearSearch(otherObject);
91  if (index < m_overlappingObjects.size())
92  {
95  m_hashPairCache->removeOverlappingPair(actualThisProxy, otherProxy, dispatcher);
96  }
97 }
98 
99 void btGhostObject::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration) const
100 {
101  btTransform convexFromTrans, convexToTrans;
102  convexFromTrans = convexFromWorld;
103  convexToTrans = convexToWorld;
104  btVector3 castShapeAabbMin, castShapeAabbMax;
105  /* Compute AABB that encompasses angular movement */
106  {
107  btVector3 linVel, angVel;
108  btTransformUtil::calculateVelocity(convexFromTrans, convexToTrans, 1.0, linVel, angVel);
109  btTransform R;
110  R.setIdentity();
111  R.setRotation(convexFromTrans.getRotation());
112  castShape->calculateTemporalAabb(R, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax);
113  }
114 
116  // do a ray-shape query using convexCaster (CCD)
117  int i;
118  for (i = 0; i < m_overlappingObjects.size(); i++)
119  {
120  btCollisionObject* collisionObject = m_overlappingObjects[i];
121  //only perform raycast if filterMask matches
122  if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
123  {
124  //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
125  btVector3 collisionObjectAabbMin, collisionObjectAabbMax;
126  collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(), collisionObjectAabbMin, collisionObjectAabbMax);
127  AabbExpand(collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
128  btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
129  btVector3 hitNormal;
130  if (btRayAabb(convexFromWorld.getOrigin(), convexToWorld.getOrigin(), collisionObjectAabbMin, collisionObjectAabbMax, hitLambda, hitNormal))
131  {
132  btCollisionWorld::objectQuerySingle(castShape, convexFromTrans, convexToTrans,
133  collisionObject,
134  collisionObject->getCollisionShape(),
135  collisionObject->getWorldTransform(),
136  resultCallback,
137  allowedCcdPenetration);
138  }
139  }
140  }
141 }
142 
143 void btGhostObject::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const
144 {
145  btTransform rayFromTrans;
146  rayFromTrans.setIdentity();
147  rayFromTrans.setOrigin(rayFromWorld);
148  btTransform rayToTrans;
149  rayToTrans.setIdentity();
150  rayToTrans.setOrigin(rayToWorld);
151 
152  int i;
153  for (i = 0; i < m_overlappingObjects.size(); i++)
154  {
155  btCollisionObject* collisionObject = m_overlappingObjects[i];
156  //only perform raycast if filterMask matches
157  if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
158  {
159  btCollisionWorld::rayTestSingle(rayFromTrans, rayToTrans,
160  collisionObject,
161  collisionObject->getCollisionShape(),
162  collisionObject->getWorldTransform(),
163  resultCallback);
164  }
165  }
166 }
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:147
btGhostObject::removeOverlappingObjectInternal
virtual void removeOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btDispatcher *dispatcher, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:45
btGhostObject::~btGhostObject
virtual ~btGhostObject()
Definition: btGhostObject.cpp:26
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:48
btBroadphaseProxy
The btBroadphaseProxy is the main class that can be used with the Bullet broadphases.
Definition: btBroadphaseProxy.h:84
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:47
btGhostObject::addOverlappingObjectInternal
virtual void addOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:32
btTransform::getRotation
btQuaternion getRotation() const
Return a quaternion representing the rotation.
Definition: btTransform.h:118
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:276
btHashedOverlappingPairCache
Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman,...
Definition: btOverlappingPairCache.h:86
btPairCachingGhostObject::~btPairCachingGhostObject
virtual ~btPairCachingGhostObject()
Definition: btGhostObject.cpp:62
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:294
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:76
btAlignedObjectArray::findLinearSearch
int findLinearSearch(const T &key) const
Definition: btAlignedObjectArray.h:445
btConvexShape.h
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:46
btCollisionWorld.h
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:166
btCollisionWorld::ConvexResultCallback::needsCollision
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
Definition: btCollisionWorld.h:352
btCollisionObject::m_internalType
int m_internalType
m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody,...
Definition: btCollisionObject.h:94
btCollisionObject::getWorldTransform
btTransform & getWorldTransform()
Definition: btCollisionObject.h:365
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....
Definition: btCollisionWorld.cpp:587
btAssert
#define btAssert(x)
Definition: btScalar.h:133
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:57
btTransform::setRotation
void setRotation(const btQuaternion &q)
Set the rotational element by btQuaternion.
Definition: btTransform.h:160
AabbExpand
void AabbExpand(btVector3 &aabbMin, btVector3 &aabbMax, const btVector3 &expansionMin, const btVector3 &expansionMax)
Definition: btAabbUtil2.h:22
btBroadphaseProxy::m_clientObject
void * m_clientObject
Definition: btBroadphaseProxy.h:102
btGhostObject::m_overlappingObjects
btAlignedObjectArray< btCollisionObject * > m_overlappingObjects
Definition: btGhostObject.h:37
btAlignedObjectArray::pop_back
void pop_back()
Definition: btAlignedObjectArray.h:192
btCollisionObject::getBroadphaseHandle
btBroadphaseProxy * getBroadphaseHandle()
Definition: btCollisionObject.h:381
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.
btPairCachingGhostObject::addOverlappingObjectInternal
virtual void addOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:68
btPairCachingGhostObject::removeOverlappingObjectInternal
virtual void removeOverlappingObjectInternal(btBroadphaseProxy *otherProxy, btDispatcher *dispatcher, btBroadphaseProxy *thisProxy=0)
this method is mainly for expert/internal use only.
Definition: btGhostObject.cpp:83
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:28
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:80
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:99
btTransform::getOrigin
btVector3 & getOrigin()
Return the origin vector translation.
Definition: btTransform.h:113
btAabbUtil2.h
btGhostObject::btGhostObject
btGhostObject()
Definition: btGhostObject.cpp:21
btHashedOverlappingPairCache::removeOverlappingPair
virtual void * removeOverlappingPair(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1, btDispatcher *dispatcher)
Definition: btOverlappingPairCache.cpp:229
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
btCollisionWorld::RayResultCallback::needsCollision
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
Definition: btCollisionWorld.h:222
btGhostObject::rayTest
void rayTest(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, btCollisionWorld::RayResultCallback &resultCallback) const
Definition: btGhostObject.cpp:143
btPairCachingGhostObject::m_hashPairCache
btHashedOverlappingPairCache * m_hashPairCache
Definition: btGhostObject.h:98
btHashedOverlappingPairCache::addOverlappingPair
virtual btBroadphasePair * addOverlappingPair(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1)
Definition: btOverlappingPairCache.h:120
btCollisionWorld::RayResultCallback
RayResultCallback is used to report new raycast results.
Definition: btCollisionWorld.h:195
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:264
btGhostObject.h
btCollisionWorld::ConvexResultCallback
RayResultCallback is used to report new raycast results.
Definition: btCollisionWorld.h:330
btTransform::setOrigin
void setOrigin(const btVector3 &origin)
Set the translational element.
Definition: btTransform.h:146
btTransformUtil::calculateVelocity
static void calculateVelocity(const btTransform &transform0, const btTransform &transform1, btScalar timeStep, btVector3 &linVel, btVector3 &angVel)
Definition: btTransformUtil.h:115
btPairCachingGhostObject::btPairCachingGhostObject
btPairCachingGhostObject()
Definition: btGhostObject.cpp:57
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
btRayAabb
bool btRayAabb(const btVector3 &rayFrom, const btVector3 &rayTo, const btVector3 &aabbMin, const btVector3 &aabbMax, btScalar &param, btVector3 &normal)
Definition: btAabbUtil2.h:117
btHashedOverlappingPairCache::~btHashedOverlappingPairCache
virtual ~btHashedOverlappingPairCache()
Definition: btOverlappingPairCache.cpp:32
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:224