2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
|
|
|
|
|
|
|
|
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 "btGhostObject.h"
|
|
|
|
#include "btCollisionWorld.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btConvexShape.h"
|
|
|
|
#include "LinearMath/btAabbUtil2.h"
|
|
|
|
|
|
|
|
btGhostObject::btGhostObject()
|
|
|
|
{
|
|
|
|
m_internalType = CO_GHOST_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
btGhostObject::~btGhostObject()
|
|
|
|
{
|
|
|
|
///btGhostObject should have been removed from the world, so no overlapping objects
|
|
|
|
btAssert(!m_overlappingObjects.size());
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btAssert(otherObject);
|
|
|
|
///if this linearSearch becomes too slow (too many overlapping objects) we should add a more appropriate data structure
|
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (index == m_overlappingObjects.size())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//not found
|
|
|
|
m_overlappingObjects.push_back(otherObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btDispatcher* dispatcher, btBroadphaseProxy* thisProxy)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btAssert(otherObject);
|
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (index < m_overlappingObjects.size())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size() - 1];
|
2017-08-01 14:30:58 +02:00
|
|
|
m_overlappingObjects.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btPairCachingGhostObject::btPairCachingGhostObject()
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_hashPairCache = new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache), 16)) btHashedOverlappingPairCache();
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
btPairCachingGhostObject::~btPairCachingGhostObject()
|
|
|
|
{
|
|
|
|
m_hashPairCache->~btHashedOverlappingPairCache();
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedFree(m_hashPairCache);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPairCachingGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btBroadphaseProxy* actualThisProxy = thisProxy ? thisProxy : getBroadphaseHandle();
|
2017-08-01 14:30:58 +02:00
|
|
|
btAssert(actualThisProxy);
|
|
|
|
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btAssert(otherObject);
|
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (index == m_overlappingObjects.size())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_overlappingObjects.push_back(otherObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_hashPairCache->addOverlappingPair(actualThisProxy, otherProxy);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPairCachingGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btDispatcher* dispatcher, btBroadphaseProxy* thisProxy1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
|
|
|
|
btBroadphaseProxy* actualThisProxy = thisProxy1 ? thisProxy1 : getBroadphaseHandle();
|
|
|
|
btAssert(actualThisProxy);
|
|
|
|
|
|
|
|
btAssert(otherObject);
|
|
|
|
int index = m_overlappingObjects.findLinearSearch(otherObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (index < m_overlappingObjects.size())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size() - 1];
|
2017-08-01 14:30:58 +02:00
|
|
|
m_overlappingObjects.pop_back();
|
2019-01-03 14:26:51 +01:00
|
|
|
m_hashPairCache->removeOverlappingPair(actualThisProxy, otherProxy, dispatcher);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btGhostObject::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btTransform convexFromTrans, convexToTrans;
|
2017-08-01 14:30:58 +02:00
|
|
|
convexFromTrans = convexFromWorld;
|
|
|
|
convexToTrans = convexToWorld;
|
|
|
|
btVector3 castShapeAabbMin, castShapeAabbMax;
|
|
|
|
/* Compute AABB that encompasses angular movement */
|
|
|
|
{
|
|
|
|
btVector3 linVel, angVel;
|
2019-01-03 14:26:51 +01:00
|
|
|
btTransformUtil::calculateVelocity(convexFromTrans, convexToTrans, 1.0, linVel, angVel);
|
2017-08-01 14:30:58 +02:00
|
|
|
btTransform R;
|
2019-01-03 14:26:51 +01:00
|
|
|
R.setIdentity();
|
|
|
|
R.setRotation(convexFromTrans.getRotation());
|
|
|
|
castShape->calculateTemporalAabb(R, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// go over all objects, and if the ray intersects their aabb + cast shape aabb,
|
|
|
|
// do a ray-shape query using convexCaster (CCD)
|
|
|
|
int i;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < m_overlappingObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionObject* collisionObject = m_overlappingObjects[i];
|
2017-08-01 14:30:58 +02:00
|
|
|
//only perform raycast if filterMask matches
|
2019-01-03 14:26:51 +01:00
|
|
|
if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
//RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 collisionObjectAabbMin, collisionObjectAabbMax;
|
|
|
|
collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(), collisionObjectAabbMin, collisionObjectAabbMax);
|
|
|
|
AabbExpand(collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
|
|
|
|
btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 hitNormal;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (btRayAabb(convexFromWorld.getOrigin(), convexToWorld.getOrigin(), collisionObjectAabbMin, collisionObjectAabbMax, hitLambda, hitNormal))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionWorld::objectQuerySingle(castShape, convexFromTrans, convexToTrans,
|
|
|
|
collisionObject,
|
|
|
|
collisionObject->getCollisionShape(),
|
|
|
|
collisionObject->getWorldTransform(),
|
|
|
|
resultCallback,
|
|
|
|
allowedCcdPenetration);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btGhostObject::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btTransform rayFromTrans;
|
|
|
|
rayFromTrans.setIdentity();
|
|
|
|
rayFromTrans.setOrigin(rayFromWorld);
|
2019-01-03 14:26:51 +01:00
|
|
|
btTransform rayToTrans;
|
2017-08-01 14:30:58 +02:00
|
|
|
rayToTrans.setIdentity();
|
|
|
|
rayToTrans.setOrigin(rayToWorld);
|
|
|
|
|
|
|
|
int i;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < m_overlappingObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionObject* collisionObject = m_overlappingObjects[i];
|
2017-08-01 14:30:58 +02:00
|
|
|
//only perform raycast if filterMask matches
|
2019-01-03 14:26:51 +01:00
|
|
|
if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionWorld::rayTestSingle(rayFromTrans, rayToTrans,
|
|
|
|
collisionObject,
|
|
|
|
collisionObject->getCollisionShape(),
|
|
|
|
collisionObject->getWorldTransform(),
|
|
|
|
resultCallback);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|