2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
|
|
|
|
|
|
|
|
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 "btMinkowskiPenetrationDepthSolver.h"
|
|
|
|
#include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
|
|
|
|
#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
|
|
|
|
#include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btConvexShape.h"
|
|
|
|
|
|
|
|
#define NUM_UNITSPHERE_POINTS 42
|
|
|
|
|
|
|
|
bool btMinkowskiPenetrationDepthSolver::calcPenDepth(btSimplexSolverInterface& simplexSolver,
|
2019-01-03 13:26:51 +00:00
|
|
|
const btConvexShape* convexA, const btConvexShape* convexB,
|
|
|
|
const btTransform& transA, const btTransform& transB,
|
|
|
|
btVector3& v, btVector3& pa, btVector3& pb,
|
|
|
|
class btIDebugDraw* debugDraw)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
(void)v;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
bool check2d = convexA->isConvex2d() && convexB->isConvex2d();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
struct btIntermediateResult : public btDiscreteCollisionDetectorInterface::Result
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btIntermediateResult() : m_hasResult(false)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 m_normalOnBInWorld;
|
|
|
|
btVector3 m_pointInWorld;
|
|
|
|
btScalar m_depth;
|
2019-01-03 13:26:51 +00:00
|
|
|
bool m_hasResult;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
virtual void setShapeIdentifiersA(int partId0, int index0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
(void)partId0;
|
|
|
|
(void)index0;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
virtual void setShapeIdentifiersB(int partId1, int index1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
(void)partId1;
|
|
|
|
(void)index1;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
void addContactPoint(const btVector3& normalOnBInWorld, const btVector3& pointInWorld, btScalar depth)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_normalOnBInWorld = normalOnBInWorld;
|
|
|
|
m_pointInWorld = pointInWorld;
|
|
|
|
m_depth = depth;
|
|
|
|
m_hasResult = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//just take fixed number of orientation, and sample the penetration depth in that direction
|
|
|
|
btScalar minProj = btScalar(BT_LARGE_FLOAT);
|
|
|
|
btVector3 minNorm(btScalar(0.), btScalar(0.), btScalar(0.));
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 minA, minB;
|
2020-01-08 17:05:43 +00:00
|
|
|
btVector3 separatingAxisInA, separatingAxisInB;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 pInA, qInB, pWorld, qWorld, w;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
#ifndef __SPU__
|
|
|
|
#define USE_BATCHED_SUPPORT 1
|
|
|
|
#endif
|
|
|
|
#ifdef USE_BATCHED_SUPPORT
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 supportVerticesABatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
|
|
|
|
btVector3 supportVerticesBBatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
|
2020-01-08 17:05:43 +00:00
|
|
|
btVector3 separatingAxisInABatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
|
|
|
|
btVector3 separatingAxisInBBatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
|
2017-08-01 12:30:58 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
int numSampleDirections = NUM_UNITSPHERE_POINTS;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < numSampleDirections; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm = getPenetrationDirections()[i];
|
2020-01-08 17:05:43 +00:00
|
|
|
separatingAxisInABatch[i] = (-norm) * transA.getBasis();
|
|
|
|
separatingAxisInBBatch[i] = norm * transB.getBasis();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int numPDA = convexA->getNumPreferredPenetrationDirections();
|
|
|
|
if (numPDA)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numPDA; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm;
|
2019-01-03 13:26:51 +00:00
|
|
|
convexA->getPreferredPenetrationDirection(i, norm);
|
|
|
|
norm = transA.getBasis() * norm;
|
2017-08-01 12:30:58 +00:00
|
|
|
getPenetrationDirections()[numSampleDirections] = norm;
|
2020-01-08 17:05:43 +00:00
|
|
|
separatingAxisInABatch[numSampleDirections] = (-norm) * transA.getBasis();
|
|
|
|
separatingAxisInBBatch[numSampleDirections] = norm * transB.getBasis();
|
2017-08-01 12:30:58 +00:00
|
|
|
numSampleDirections++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int numPDB = convexB->getNumPreferredPenetrationDirections();
|
|
|
|
if (numPDB)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numPDB; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm;
|
2019-01-03 13:26:51 +00:00
|
|
|
convexB->getPreferredPenetrationDirection(i, norm);
|
|
|
|
norm = transB.getBasis() * norm;
|
2017-08-01 12:30:58 +00:00
|
|
|
getPenetrationDirections()[numSampleDirections] = norm;
|
2020-01-08 17:05:43 +00:00
|
|
|
separatingAxisInABatch[numSampleDirections] = (-norm) * transA.getBasis();
|
|
|
|
separatingAxisInBBatch[numSampleDirections] = norm * transB.getBasis();
|
2017-08-01 12:30:58 +00:00
|
|
|
numSampleDirections++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
convexA->batchedUnitVectorGetSupportingVertexWithoutMargin(separatingAxisInABatch, supportVerticesABatch, numSampleDirections);
|
|
|
|
convexB->batchedUnitVectorGetSupportingVertexWithoutMargin(separatingAxisInBBatch, supportVerticesBBatch, numSampleDirections);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < numSampleDirections; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm = getPenetrationDirections()[i];
|
|
|
|
if (check2d)
|
|
|
|
{
|
|
|
|
norm[2] = 0.f;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (norm.length2() > 0.01)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2020-01-08 17:05:43 +00:00
|
|
|
separatingAxisInA = separatingAxisInABatch[i];
|
|
|
|
separatingAxisInB = separatingAxisInBBatch[i];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
pInA = supportVerticesABatch[i];
|
|
|
|
qInB = supportVerticesBBatch[i];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
pWorld = transA(pInA);
|
2017-08-01 12:30:58 +00:00
|
|
|
qWorld = transB(qInB);
|
|
|
|
if (check2d)
|
|
|
|
{
|
|
|
|
pWorld[2] = 0.f;
|
|
|
|
qWorld[2] = 0.f;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
w = qWorld - pWorld;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar delta = norm.dot(w);
|
|
|
|
//find smallest delta
|
|
|
|
if (delta < minProj)
|
|
|
|
{
|
|
|
|
minProj = delta;
|
|
|
|
minNorm = norm;
|
|
|
|
minA = pWorld;
|
|
|
|
minB = qWorld;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
int numSampleDirections = NUM_UNITSPHERE_POINTS;
|
|
|
|
|
|
|
|
#ifndef __SPU__
|
|
|
|
{
|
|
|
|
int numPDA = convexA->getNumPreferredPenetrationDirections();
|
|
|
|
if (numPDA)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numPDA; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm;
|
2019-01-03 13:26:51 +00:00
|
|
|
convexA->getPreferredPenetrationDirection(i, norm);
|
|
|
|
norm = transA.getBasis() * norm;
|
2017-08-01 12:30:58 +00:00
|
|
|
getPenetrationDirections()[numSampleDirections] = norm;
|
|
|
|
numSampleDirections++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int numPDB = convexB->getNumPreferredPenetrationDirections();
|
|
|
|
if (numPDB)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numPDB; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 norm;
|
2019-01-03 13:26:51 +00:00
|
|
|
convexB->getPreferredPenetrationDirection(i, norm);
|
|
|
|
norm = transB.getBasis() * norm;
|
2017-08-01 12:30:58 +00:00
|
|
|
getPenetrationDirections()[numSampleDirections] = norm;
|
|
|
|
numSampleDirections++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif // __SPU__
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numSampleDirections; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3& norm = getPenetrationDirections()[i];
|
2020-01-08 17:05:43 +00:00
|
|
|
separatingAxisInA = (-norm) * transA.getBasis();
|
|
|
|
separatingAxisInB = norm * transB.getBasis();
|
|
|
|
pInA = convexA->localGetSupportVertexWithoutMarginNonVirtual(separatingAxisInA);
|
|
|
|
qInB = convexB->localGetSupportVertexWithoutMarginNonVirtual(separatingAxisInB);
|
2019-01-03 13:26:51 +00:00
|
|
|
pWorld = transA(pInA);
|
2017-08-01 12:30:58 +00:00
|
|
|
qWorld = transB(qInB);
|
2019-01-03 13:26:51 +00:00
|
|
|
w = qWorld - pWorld;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar delta = norm.dot(w);
|
|
|
|
//find smallest delta
|
|
|
|
if (delta < minProj)
|
|
|
|
{
|
|
|
|
minProj = delta;
|
|
|
|
minNorm = norm;
|
|
|
|
minA = pWorld;
|
|
|
|
minB = qWorld;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //USE_BATCHED_SUPPORT
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//add the margins
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
minA += minNorm * convexA->getMarginNonVirtual();
|
|
|
|
minB -= minNorm * convexB->getMarginNonVirtual();
|
2017-08-01 12:30:58 +00:00
|
|
|
//no penetration
|
|
|
|
if (minProj < btScalar(0.))
|
|
|
|
return false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar extraSeparation = 0.5f; ///scale dependent
|
|
|
|
minProj += extraSeparation + (convexA->getMarginNonVirtual() + convexB->getMarginNonVirtual());
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//#define DEBUG_DRAW 1
|
|
|
|
#ifdef DEBUG_DRAW
|
|
|
|
if (debugDraw)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 color(0, 1, 0);
|
|
|
|
debugDraw->drawLine(minA, minB, color);
|
|
|
|
color = btVector3(1, 1, 1);
|
|
|
|
btVector3 vec = minB - minA;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar prj2 = minNorm.dot(vec);
|
2019-01-03 13:26:51 +00:00
|
|
|
debugDraw->drawLine(minA, minA + (minNorm * minProj), color);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //DEBUG_DRAW
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btGjkPairDetector gjkdet(convexA, convexB, &simplexSolver, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar offsetDist = minProj;
|
|
|
|
btVector3 offset = minNorm * offsetDist;
|
|
|
|
|
|
|
|
btGjkPairDetector::ClosestPointInput input;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 newOrg = transA.getOrigin() + offset;
|
|
|
|
|
|
|
|
btTransform displacedTrans = transA;
|
|
|
|
displacedTrans.setOrigin(newOrg);
|
|
|
|
|
|
|
|
input.m_transformA = displacedTrans;
|
|
|
|
input.m_transformB = transB;
|
2019-01-03 13:26:51 +00:00
|
|
|
input.m_maximumDistanceSquared = btScalar(BT_LARGE_FLOAT); //minProj;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btIntermediateResult res;
|
2020-01-08 17:05:43 +00:00
|
|
|
gjkdet.setCachedSeparatingAxis(-minNorm);
|
2019-01-03 13:26:51 +00:00
|
|
|
gjkdet.getClosestPoints(input, res, debugDraw);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar correctedMinNorm = minProj - res.m_depth;
|
|
|
|
|
|
|
|
//the penetration depth is over-estimated, relax it
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar penetration_relaxation = btScalar(1.);
|
|
|
|
minNorm *= penetration_relaxation;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (res.m_hasResult)
|
|
|
|
{
|
|
|
|
pa = res.m_pointInWorld - minNorm * correctedMinNorm;
|
|
|
|
pb = res.m_pointInWorld;
|
|
|
|
v = minNorm;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
#ifdef DEBUG_DRAW
|
|
|
|
if (debugDraw)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 color(1, 0, 0);
|
|
|
|
debugDraw->drawLine(pa, pb, color);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //DEBUG_DRAW
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
return res.m_hasResult;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3* btMinkowskiPenetrationDepthSolver::getPenetrationDirections()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
static btVector3 sPenetrationDirections[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2] =
|
|
|
|
{
|
|
|
|
btVector3(btScalar(0.000000), btScalar(-0.000000), btScalar(-1.000000)),
|
|
|
|
btVector3(btScalar(0.723608), btScalar(-0.525725), btScalar(-0.447219)),
|
|
|
|
btVector3(btScalar(-0.276388), btScalar(-0.850649), btScalar(-0.447219)),
|
|
|
|
btVector3(btScalar(-0.894426), btScalar(-0.000000), btScalar(-0.447216)),
|
|
|
|
btVector3(btScalar(-0.276388), btScalar(0.850649), btScalar(-0.447220)),
|
|
|
|
btVector3(btScalar(0.723608), btScalar(0.525725), btScalar(-0.447219)),
|
|
|
|
btVector3(btScalar(0.276388), btScalar(-0.850649), btScalar(0.447220)),
|
|
|
|
btVector3(btScalar(-0.723608), btScalar(-0.525725), btScalar(0.447219)),
|
|
|
|
btVector3(btScalar(-0.723608), btScalar(0.525725), btScalar(0.447219)),
|
|
|
|
btVector3(btScalar(0.276388), btScalar(0.850649), btScalar(0.447219)),
|
|
|
|
btVector3(btScalar(0.894426), btScalar(0.000000), btScalar(0.447216)),
|
|
|
|
btVector3(btScalar(-0.000000), btScalar(0.000000), btScalar(1.000000)),
|
|
|
|
btVector3(btScalar(0.425323), btScalar(-0.309011), btScalar(-0.850654)),
|
|
|
|
btVector3(btScalar(-0.162456), btScalar(-0.499995), btScalar(-0.850654)),
|
|
|
|
btVector3(btScalar(0.262869), btScalar(-0.809012), btScalar(-0.525738)),
|
|
|
|
btVector3(btScalar(0.425323), btScalar(0.309011), btScalar(-0.850654)),
|
|
|
|
btVector3(btScalar(0.850648), btScalar(-0.000000), btScalar(-0.525736)),
|
|
|
|
btVector3(btScalar(-0.525730), btScalar(-0.000000), btScalar(-0.850652)),
|
|
|
|
btVector3(btScalar(-0.688190), btScalar(-0.499997), btScalar(-0.525736)),
|
|
|
|
btVector3(btScalar(-0.162456), btScalar(0.499995), btScalar(-0.850654)),
|
|
|
|
btVector3(btScalar(-0.688190), btScalar(0.499997), btScalar(-0.525736)),
|
|
|
|
btVector3(btScalar(0.262869), btScalar(0.809012), btScalar(-0.525738)),
|
|
|
|
btVector3(btScalar(0.951058), btScalar(0.309013), btScalar(0.000000)),
|
|
|
|
btVector3(btScalar(0.951058), btScalar(-0.309013), btScalar(0.000000)),
|
|
|
|
btVector3(btScalar(0.587786), btScalar(-0.809017), btScalar(0.000000)),
|
|
|
|
btVector3(btScalar(0.000000), btScalar(-1.000000), btScalar(0.000000)),
|
|
|
|
btVector3(btScalar(-0.587786), btScalar(-0.809017), btScalar(0.000000)),
|
|
|
|
btVector3(btScalar(-0.951058), btScalar(-0.309013), btScalar(-0.000000)),
|
|
|
|
btVector3(btScalar(-0.951058), btScalar(0.309013), btScalar(-0.000000)),
|
|
|
|
btVector3(btScalar(-0.587786), btScalar(0.809017), btScalar(-0.000000)),
|
|
|
|
btVector3(btScalar(-0.000000), btScalar(1.000000), btScalar(-0.000000)),
|
|
|
|
btVector3(btScalar(0.587786), btScalar(0.809017), btScalar(-0.000000)),
|
|
|
|
btVector3(btScalar(0.688190), btScalar(-0.499997), btScalar(0.525736)),
|
|
|
|
btVector3(btScalar(-0.262869), btScalar(-0.809012), btScalar(0.525738)),
|
|
|
|
btVector3(btScalar(-0.850648), btScalar(0.000000), btScalar(0.525736)),
|
|
|
|
btVector3(btScalar(-0.262869), btScalar(0.809012), btScalar(0.525738)),
|
|
|
|
btVector3(btScalar(0.688190), btScalar(0.499997), btScalar(0.525736)),
|
|
|
|
btVector3(btScalar(0.525730), btScalar(0.000000), btScalar(0.850652)),
|
|
|
|
btVector3(btScalar(0.162456), btScalar(-0.499995), btScalar(0.850654)),
|
|
|
|
btVector3(btScalar(-0.425323), btScalar(-0.309011), btScalar(0.850654)),
|
|
|
|
btVector3(btScalar(-0.425323), btScalar(0.309011), btScalar(0.850654)),
|
|
|
|
btVector3(btScalar(0.162456), btScalar(0.499995), btScalar(0.850654))};
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return sPenetrationDirections;
|
|
|
|
}
|