| 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 | { | 
|---|
| 28 | ///btGhostObject should have been removed from the world, so no overlapping objects | 
|---|
| 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); | 
|---|
| 37 | ///if this linearSearch becomes too slow (too many overlapping objects) we should add a more appropriate data structure | 
|---|
| 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 | btAlignedFree( m_hashPairCache ); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void btPairCachingGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btBroadphaseProxy* thisProxy) | 
|---|
| 70 | { | 
|---|
| 71 | btBroadphaseProxy*actualThisProxy = thisProxy ? thisProxy : getBroadphaseHandle(); | 
|---|
| 72 | btAssert(actualThisProxy); | 
|---|
| 73 |  | 
|---|
| 74 | btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject; | 
|---|
| 75 | btAssert(otherObject); | 
|---|
| 76 | int index = m_overlappingObjects.findLinearSearch(otherObject); | 
|---|
| 77 | if (index==m_overlappingObjects.size()) | 
|---|
| 78 | { | 
|---|
| 79 | m_overlappingObjects.push_back(otherObject); | 
|---|
| 80 | m_hashPairCache->addOverlappingPair(actualThisProxy,otherProxy); | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void btPairCachingGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btDispatcher* dispatcher,btBroadphaseProxy* thisProxy1) | 
|---|
| 85 | { | 
|---|
| 86 | btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject; | 
|---|
| 87 | btBroadphaseProxy* actualThisProxy = thisProxy1 ? thisProxy1 : getBroadphaseHandle(); | 
|---|
| 88 | btAssert(actualThisProxy); | 
|---|
| 89 |  | 
|---|
| 90 | btAssert(otherObject); | 
|---|
| 91 | int index = m_overlappingObjects.findLinearSearch(otherObject); | 
|---|
| 92 | if (index<m_overlappingObjects.size()) | 
|---|
| 93 | { | 
|---|
| 94 | m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size()-1]; | 
|---|
| 95 | m_overlappingObjects.pop_back(); | 
|---|
| 96 | m_hashPairCache->removeOverlappingPair(actualThisProxy,otherProxy,dispatcher); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | void    btGhostObject::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration) const | 
|---|
| 102 | { | 
|---|
| 103 | btTransform     convexFromTrans,convexToTrans; | 
|---|
| 104 | convexFromTrans = convexFromWorld; | 
|---|
| 105 | convexToTrans = convexToWorld; | 
|---|
| 106 | btVector3 castShapeAabbMin, castShapeAabbMax; | 
|---|
| 107 | /* Compute AABB that encompasses angular movement */ | 
|---|
| 108 | { | 
|---|
| 109 | btVector3 linVel, angVel; | 
|---|
| 110 | btTransformUtil::calculateVelocity (convexFromTrans, convexToTrans, 1.0, linVel, angVel); | 
|---|
| 111 | btTransform R; | 
|---|
| 112 | R.setIdentity (); | 
|---|
| 113 | R.setRotation (convexFromTrans.getRotation()); | 
|---|
| 114 | castShape->calculateTemporalAabb (R, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /// go over all objects, and if the ray intersects their aabb + cast shape aabb, | 
|---|
| 118 | // do a ray-shape query using convexCaster (CCD) | 
|---|
| 119 | int i; | 
|---|
| 120 | for (i=0;i<m_overlappingObjects.size();i++) | 
|---|
| 121 | { | 
|---|
| 122 | btCollisionObject*      collisionObject= m_overlappingObjects[i]; | 
|---|
| 123 | //only perform raycast if filterMask matches | 
|---|
| 124 | if(resultCallback.needsCollision(collisionObject->getBroadphaseHandle())) { | 
|---|
| 125 | //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject(); | 
|---|
| 126 | btVector3 collisionObjectAabbMin,collisionObjectAabbMax; | 
|---|
| 127 | collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(),collisionObjectAabbMin,collisionObjectAabbMax); | 
|---|
| 128 | AabbExpand (collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax); | 
|---|
| 129 | btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing | 
|---|
| 130 | btVector3 hitNormal; | 
|---|
| 131 | if (btRayAabb(convexFromWorld.getOrigin(),convexToWorld.getOrigin(),collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,hitNormal)) | 
|---|
| 132 | { | 
|---|
| 133 | btCollisionWorld::objectQuerySingle(castShape, convexFromTrans,convexToTrans, | 
|---|
| 134 | collisionObject, | 
|---|
| 135 | collisionObject->getCollisionShape(), | 
|---|
| 136 | collisionObject->getWorldTransform(), | 
|---|
| 137 | resultCallback, | 
|---|
| 138 | allowedCcdPenetration); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | void    btGhostObject::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const | 
|---|
| 146 | { | 
|---|
| 147 | btTransform rayFromTrans; | 
|---|
| 148 | rayFromTrans.setIdentity(); | 
|---|
| 149 | rayFromTrans.setOrigin(rayFromWorld); | 
|---|
| 150 | btTransform  rayToTrans; | 
|---|
| 151 | rayToTrans.setIdentity(); | 
|---|
| 152 | rayToTrans.setOrigin(rayToWorld); | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | int i; | 
|---|
| 156 | for (i=0;i<m_overlappingObjects.size();i++) | 
|---|
| 157 | { | 
|---|
| 158 | btCollisionObject*      collisionObject= m_overlappingObjects[i]; | 
|---|
| 159 | //only perform raycast if filterMask matches | 
|---|
| 160 | if(resultCallback.needsCollision(collisionObject->getBroadphaseHandle())) | 
|---|
| 161 | { | 
|---|
| 162 | btCollisionWorld::rayTestSingle(rayFromTrans,rayToTrans, | 
|---|
| 163 | collisionObject, | 
|---|
| 164 | collisionObject->getCollisionShape(), | 
|---|
| 165 | collisionObject->getWorldTransform(), | 
|---|
| 166 | resultCallback); | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|