2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
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:
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.
16 #ifndef BT_GHOST_OBJECT_H
17 #define BT_GHOST_OBJECT_H
19 #include "btCollisionObject.h"
20 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCallback.h"
21 #include "LinearMath/btAlignedAllocator.h"
22 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
23 #include "btCollisionWorld.h"
29 ///The btGhostObject can keep track of all objects that are overlapping
30 ///By default, this overlap is based on the AABB
31 ///This is useful for creating a character controller, collision sensors/triggers, explosions etc.
32 ///We plan on adding rayTest and other queries for the btGhostObject
33 ATTRIBUTE_ALIGNED16(class)
34 btGhostObject : public btCollisionObject
37 btAlignedObjectArray<btCollisionObject*> m_overlappingObjects;
42 virtual ~btGhostObject();
44 void convexSweepTest(const class btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = 0.f) const;
46 void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const;
48 ///this method is mainly for expert/internal use only.
49 virtual void addOverlappingObjectInternal(btBroadphaseProxy * otherProxy, btBroadphaseProxy* thisProxy = 0);
50 ///this method is mainly for expert/internal use only.
51 virtual void removeOverlappingObjectInternal(btBroadphaseProxy * otherProxy, btDispatcher * dispatcher, btBroadphaseProxy* thisProxy = 0);
53 int getNumOverlappingObjects() const
55 return m_overlappingObjects.size();
58 btCollisionObject* getOverlappingObject(int index)
60 return m_overlappingObjects[index];
63 const btCollisionObject* getOverlappingObject(int index) const
65 return m_overlappingObjects[index];
68 btAlignedObjectArray<btCollisionObject*>& getOverlappingPairs()
70 return m_overlappingObjects;
73 const btAlignedObjectArray<btCollisionObject*> getOverlappingPairs() const
75 return m_overlappingObjects;
82 static const btGhostObject* upcast(const btCollisionObject* colObj)
84 if (colObj->getInternalType() == CO_GHOST_OBJECT)
85 return (const btGhostObject*)colObj;
88 static btGhostObject* upcast(btCollisionObject * colObj)
90 if (colObj->getInternalType() == CO_GHOST_OBJECT)
91 return (btGhostObject*)colObj;
96 class btPairCachingGhostObject : public btGhostObject
98 btHashedOverlappingPairCache* m_hashPairCache;
101 btPairCachingGhostObject();
103 virtual ~btPairCachingGhostObject();
105 ///this method is mainly for expert/internal use only.
106 virtual void addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy = 0);
108 virtual void removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btDispatcher* dispatcher, btBroadphaseProxy* thisProxy = 0);
110 btHashedOverlappingPairCache* getOverlappingPairCache()
112 return m_hashPairCache;
116 ///The btGhostPairCallback interfaces and forwards adding and removal of overlapping pairs from the btBroadphaseInterface to btGhostObject.
117 class btGhostPairCallback : public btOverlappingPairCallback
120 btGhostPairCallback()
124 virtual ~btGhostPairCallback()
128 virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1)
130 btCollisionObject* colObj0 = (btCollisionObject*)proxy0->m_clientObject;
131 btCollisionObject* colObj1 = (btCollisionObject*)proxy1->m_clientObject;
132 btGhostObject* ghost0 = btGhostObject::upcast(colObj0);
133 btGhostObject* ghost1 = btGhostObject::upcast(colObj1);
135 ghost0->addOverlappingObjectInternal(proxy1, proxy0);
137 ghost1->addOverlappingObjectInternal(proxy0, proxy1);
141 virtual void* removeOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1, btDispatcher* dispatcher)
143 btCollisionObject* colObj0 = (btCollisionObject*)proxy0->m_clientObject;
144 btCollisionObject* colObj1 = (btCollisionObject*)proxy1->m_clientObject;
145 btGhostObject* ghost0 = btGhostObject::upcast(colObj0);
146 btGhostObject* ghost1 = btGhostObject::upcast(colObj1);
148 ghost0->removeOverlappingObjectInternal(proxy1, dispatcher, proxy0);
150 ghost1->removeOverlappingObjectInternal(proxy0, dispatcher, proxy1);
154 virtual void removeOverlappingPairsContainingProxy(btBroadphaseProxy* /*proxy0*/, btDispatcher* /*dispatcher*/)
157 //need to keep track of all ghost objects and call them here
158 //m_hashPairCache->removeOverlappingPairsContainingProxy(proxy0,dispatcher);