2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
2022-01-06 22:37:49 +00:00
|
|
|
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "btSimpleBroadphase.h"
|
|
|
|
#include "BulletCollision/BroadphaseCollision/btDispatcher.h"
|
|
|
|
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
|
|
|
|
|
|
|
|
#include "LinearMath/btVector3.h"
|
|
|
|
#include "LinearMath/btTransform.h"
|
|
|
|
#include "LinearMath/btMatrix3x3.h"
|
|
|
|
#include "LinearMath/btAabbUtil2.h"
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::validate()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_numHandles; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = i + 1; j < m_numHandles; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btAssert(&m_pHandles[i] != &m_pHandles[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btSimpleBroadphase::btSimpleBroadphase(int maxProxies, btOverlappingPairCache* overlappingPairCache)
|
2019-01-03 13:26:51 +00:00
|
|
|
: m_pairCache(overlappingPairCache),
|
|
|
|
m_ownsPairCache(false),
|
|
|
|
m_invalidPair(0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!overlappingPairCache)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
void* mem = btAlignedAlloc(sizeof(btHashedOverlappingPairCache), 16);
|
|
|
|
m_pairCache = new (mem) btHashedOverlappingPairCache();
|
2017-08-01 12:30:58 +00:00
|
|
|
m_ownsPairCache = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate handles buffer and put all handles on free list
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pHandlesRawPtr = btAlignedAlloc(sizeof(btSimpleBroadphaseProxy) * maxProxies, 16);
|
|
|
|
m_pHandles = new (m_pHandlesRawPtr) btSimpleBroadphaseProxy[maxProxies];
|
2017-08-01 12:30:58 +00:00
|
|
|
m_maxHandles = maxProxies;
|
|
|
|
m_numHandles = 0;
|
|
|
|
m_firstFreeHandle = 0;
|
|
|
|
m_LastHandleIndex = -1;
|
|
|
|
|
|
|
|
{
|
|
|
|
for (int i = m_firstFreeHandle; i < maxProxies; i++)
|
|
|
|
{
|
|
|
|
m_pHandles[i].SetNextFree(i + 1);
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pHandles[i].m_uniqueId = i + 2; //any UID will do, we just avoid too trivial values (0,1) for debugging purposes
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
m_pHandles[maxProxies - 1].SetNextFree(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btSimpleBroadphase::~btSimpleBroadphase()
|
|
|
|
{
|
|
|
|
btAlignedFree(m_pHandlesRawPtr);
|
|
|
|
|
|
|
|
if (m_ownsPairCache)
|
|
|
|
{
|
|
|
|
m_pairCache->~btOverlappingPairCache();
|
|
|
|
btAlignedFree(m_pairCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btBroadphaseProxy* btSimpleBroadphase::createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* /*dispatcher*/)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (m_numHandles >= m_maxHandles)
|
|
|
|
{
|
|
|
|
btAssert(0);
|
2019-01-03 13:26:51 +00:00
|
|
|
return 0; //should never happen, but don't let the game crash ;-)
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(aabbMin[0] <= aabbMax[0] && aabbMin[1] <= aabbMax[1] && aabbMin[2] <= aabbMax[2]);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
int newHandleIndex = allocHandle();
|
2019-01-03 13:26:51 +00:00
|
|
|
btSimpleBroadphaseProxy* proxy = new (&m_pHandles[newHandleIndex]) btSimpleBroadphaseProxy(aabbMin, aabbMax, shapeType, userPtr, collisionFilterGroup, collisionFilterMask);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
class RemovingOverlapCallback : public btOverlapCallback
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
protected:
|
2019-01-03 13:26:51 +00:00
|
|
|
virtual bool processOverlap(btBroadphasePair& pair)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
(void)pair;
|
|
|
|
btAssert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class RemovePairContainingProxy
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btBroadphaseProxy* m_targetProxy;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
public:
|
2017-08-01 12:30:58 +00:00
|
|
|
virtual ~RemovePairContainingProxy()
|
|
|
|
{
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
protected:
|
|
|
|
virtual bool processOverlap(btBroadphasePair& pair)
|
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0);
|
|
|
|
btSimpleBroadphaseProxy* proxy1 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1);
|
|
|
|
|
|
|
|
return ((m_targetProxy == proxy0 || m_targetProxy == proxy1));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::destroyProxy(btBroadphaseProxy* proxyOrg, btDispatcher* dispatcher)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-06-11 11:18:05 +00:00
|
|
|
m_pairCache->removeOverlappingPairsContainingProxy(proxyOrg, dispatcher);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(proxyOrg);
|
|
|
|
freeHandle(proxy0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
//validate();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
|
|
|
|
aabbMin = sbp->m_aabbMin;
|
|
|
|
aabbMax = sbp->m_aabbMax;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* /*dispatcher*/)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
|
|
|
|
sbp->m_aabbMin = aabbMin;
|
|
|
|
sbp->m_aabbMax = aabbMax;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin, const btVector3& aabbMax)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i <= m_LastHandleIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!proxy->m_clientObject)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rayCallback.process(proxy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i <= m_LastHandleIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!proxy->m_clientObject)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (TestAabbAgainstAabb2(aabbMin, aabbMax, proxy->m_aabbMin, proxy->m_aabbMax))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
callback.process(proxy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btSimpleBroadphase::aabbOverlap(btSimpleBroadphaseProxy* proxy0, btSimpleBroadphaseProxy* proxy1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return proxy0->m_aabbMin[0] <= proxy1->m_aabbMax[0] && proxy1->m_aabbMin[0] <= proxy0->m_aabbMax[0] &&
|
2017-08-01 12:30:58 +00:00
|
|
|
proxy0->m_aabbMin[1] <= proxy1->m_aabbMax[1] && proxy1->m_aabbMin[1] <= proxy0->m_aabbMax[1] &&
|
|
|
|
proxy0->m_aabbMin[2] <= proxy1->m_aabbMax[2] && proxy1->m_aabbMin[2] <= proxy0->m_aabbMax[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
//then remove non-overlapping ones
|
|
|
|
class CheckOverlapCallback : public btOverlapCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool processOverlap(btBroadphasePair& pair)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return (!btSimpleBroadphase::aabbOverlap(static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0), static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1)));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//first check for new overlapping pairs
|
2019-01-03 13:26:51 +00:00
|
|
|
int i, j;
|
2017-08-01 12:30:58 +00:00
|
|
|
if (m_numHandles >= 0)
|
|
|
|
{
|
|
|
|
int new_largest_index = -1;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i <= m_LastHandleIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* proxy0 = &m_pHandles[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!proxy0->m_clientObject)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
new_largest_index = i;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (j = i + 1; j <= m_LastHandleIndex; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* proxy1 = &m_pHandles[j];
|
|
|
|
btAssert(proxy0 != proxy1);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!proxy1->m_clientObject)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
|
|
|
|
btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (aabbOverlap(p0, p1))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!m_pairCache->findPair(proxy0, proxy1))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pairCache->addOverlappingPair(proxy0, proxy1);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (!m_pairCache->hasDeferredRemoval())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_pairCache->findPair(proxy0, proxy1))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pairCache->removeOverlappingPair(proxy0, proxy1, dispatcher);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_LastHandleIndex = new_largest_index;
|
|
|
|
|
|
|
|
if (m_ownsPairCache && m_pairCache->hasDeferredRemoval())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btBroadphasePairArray& overlappingPairArray = m_pairCache->getOverlappingPairArray();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//perform a sort, to find duplicates and to sort 'invalid' pairs to the end
|
|
|
|
overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
|
|
|
|
|
|
|
|
overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
|
|
|
|
m_invalidPair = 0;
|
|
|
|
|
|
|
|
btBroadphasePair previousPair;
|
|
|
|
previousPair.m_pProxy0 = 0;
|
|
|
|
previousPair.m_pProxy1 = 0;
|
|
|
|
previousPair.m_algorithm = 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < overlappingPairArray.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btBroadphasePair& pair = overlappingPairArray[i];
|
|
|
|
|
|
|
|
bool isDuplicate = (pair == previousPair);
|
|
|
|
|
|
|
|
previousPair = pair;
|
|
|
|
|
|
|
|
bool needsRemoval = false;
|
|
|
|
|
|
|
|
if (!isDuplicate)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
bool hasOverlap = testAabbOverlap(pair.m_pProxy0, pair.m_pProxy1);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (hasOverlap)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
needsRemoval = false; //callback->processOverlap(pair);
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
needsRemoval = true;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//remove duplicate
|
|
|
|
needsRemoval = true;
|
|
|
|
//should have no algorithm
|
|
|
|
btAssert(!pair.m_algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsRemoval)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_pairCache->cleanOverlappingPair(pair, dispatcher);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
|
|
|
|
// m_overlappingPairArray.pop_back();
|
|
|
|
pair.m_pProxy0 = 0;
|
|
|
|
pair.m_pProxy1 = 0;
|
|
|
|
m_invalidPair++;
|
2018-09-07 14:11:04 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///if you don't like to skip the invalid pairs in the array, execute following code:
|
|
|
|
#define CLEAN_INVALID_PAIRS 1
|
|
|
|
#ifdef CLEAN_INVALID_PAIRS
|
|
|
|
|
|
|
|
//perform a sort, to sort 'invalid' pairs to the end
|
|
|
|
overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
|
|
|
|
|
|
|
|
overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
|
|
|
|
m_invalidPair = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //CLEAN_INVALID_PAIRS
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btSimpleBroadphase::testAabbOverlap(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
|
|
|
|
btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
|
2019-01-03 13:26:51 +00:00
|
|
|
return aabbOverlap(p0, p1);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btSimpleBroadphase::resetPool(btDispatcher* dispatcher)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//not yet
|
|
|
|
}
|