2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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 #include "btContinuousConvexCollision.h"
17 #include "BulletCollision/CollisionShapes/btConvexShape.h"
18 #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
19 #include "LinearMath/btTransformUtil.h"
20 #include "BulletCollision/CollisionShapes/btSphereShape.h"
22 #include "btGjkPairDetector.h"
23 #include "btPointCollector.h"
24 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
26 btContinuousConvexCollision::btContinuousConvexCollision(const btConvexShape* convexA, const btConvexShape* convexB, btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* penetrationDepthSolver)
27 : m_simplexSolver(simplexSolver),
28 m_penetrationDepthSolver(penetrationDepthSolver),
35 btContinuousConvexCollision::btContinuousConvexCollision(const btConvexShape* convexA, const btStaticPlaneShape* plane)
37 m_penetrationDepthSolver(0),
44 /// This maximum should not be necessary. It allows for untested/degenerate cases in production code.
45 /// You don't want your game ever to lock-up.
46 #define MAX_ITERATIONS 64
48 void btContinuousConvexCollision::computeClosestPoints(const btTransform& transA, const btTransform& transB, btPointCollector& pointCollector)
52 m_simplexSolver->reset();
53 btGjkPairDetector gjk(m_convexA, m_convexB1, m_convexA->getShapeType(), m_convexB1->getShapeType(), m_convexA->getMargin(), m_convexB1->getMargin(), m_simplexSolver, m_penetrationDepthSolver);
54 btGjkPairDetector::ClosestPointInput input;
55 input.m_transformA = transA;
56 input.m_transformB = transB;
57 gjk.getClosestPoints(input, pointCollector, 0);
62 const btConvexShape* convexShape = m_convexA;
63 const btStaticPlaneShape* planeShape = m_planeShape;
65 const btVector3& planeNormal = planeShape->getPlaneNormal();
66 const btScalar& planeConstant = planeShape->getPlaneConstant();
68 btTransform convexWorldTransform = transA;
69 btTransform convexInPlaneTrans;
70 convexInPlaneTrans = transB.inverse() * convexWorldTransform;
71 btTransform planeInConvex;
72 planeInConvex = convexWorldTransform.inverse() * transB;
74 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
76 btVector3 vtxInPlane = convexInPlaneTrans(vtx);
77 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
79 btVector3 vtxInPlaneProjected = vtxInPlane - distance * planeNormal;
80 btVector3 vtxInPlaneWorld = transB * vtxInPlaneProjected;
81 btVector3 normalOnSurfaceB = transB.getBasis() * planeNormal;
83 pointCollector.addContactPoint(
90 bool btContinuousConvexCollision::calcTimeOfImpact(
91 const btTransform& fromA,
92 const btTransform& toA,
93 const btTransform& fromB,
94 const btTransform& toB,
97 /// compute linear and angular velocity for this interval, to interpolate
98 btVector3 linVelA, angVelA, linVelB, angVelB;
99 btTransformUtil::calculateVelocity(fromA, toA, btScalar(1.), linVelA, angVelA);
100 btTransformUtil::calculateVelocity(fromB, toB, btScalar(1.), linVelB, angVelB);
102 btScalar boundingRadiusA = m_convexA->getAngularMotionDisc();
103 btScalar boundingRadiusB = m_convexB1 ? m_convexB1->getAngularMotionDisc() : 0.f;
105 btScalar maxAngularProjectedVelocity = angVelA.length() * boundingRadiusA + angVelB.length() * boundingRadiusB;
106 btVector3 relLinVel = (linVelB - linVelA);
108 btScalar relLinVelocLength = (linVelB - linVelA).length();
110 if ((relLinVelocLength + maxAngularProjectedVelocity) == 0.f)
113 btScalar lambda = btScalar(0.);
116 n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
117 bool hasResult = false;
120 btScalar lastLambda = lambda;
121 //btScalar epsilon = btScalar(0.001);
124 //first solution, using GJK
126 btScalar radius = 0.001f;
127 // result.drawCoordSystem(sphereTr);
129 btPointCollector pointCollector1;
132 computeClosestPoints(fromA, fromB, pointCollector1);
134 hasResult = pointCollector1.m_hasResult;
135 c = pointCollector1.m_pointInWorld;
141 dist = pointCollector1.m_distance + result.m_allowedPenetration;
142 n = pointCollector1.m_normalOnBInWorld;
143 btScalar projectedLinearVelocity = relLinVel.dot(n);
144 if ((projectedLinearVelocity + maxAngularProjectedVelocity) <= SIMD_EPSILON)
148 while (dist > radius)
150 if (result.m_debugDrawer)
152 result.m_debugDrawer->drawSphere(c, 0.2f, btVector3(1, 1, 1));
154 btScalar dLambda = btScalar(0.);
156 projectedLinearVelocity = relLinVel.dot(n);
158 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
159 if ((projectedLinearVelocity + maxAngularProjectedVelocity) <= SIMD_EPSILON)
162 dLambda = dist / (projectedLinearVelocity + maxAngularProjectedVelocity);
166 if (lambda > btScalar(1.) || lambda < btScalar(0.))
169 //todo: next check with relative epsilon
170 if (lambda <= lastLambda)
178 //interpolate to next lambda
179 btTransform interpolatedTransA, interpolatedTransB, relativeTrans;
181 btTransformUtil::integrateTransform(fromA, linVelA, angVelA, lambda, interpolatedTransA);
182 btTransformUtil::integrateTransform(fromB, linVelB, angVelB, lambda, interpolatedTransB);
183 relativeTrans = interpolatedTransB.inverseTimes(interpolatedTransA);
185 if (result.m_debugDrawer)
187 result.m_debugDrawer->drawSphere(interpolatedTransA.getOrigin(), 0.2f, btVector3(1, 0, 0));
190 result.DebugDraw(lambda);
192 btPointCollector pointCollector;
193 computeClosestPoints(interpolatedTransA, interpolatedTransB, pointCollector);
195 if (pointCollector.m_hasResult)
197 dist = pointCollector.m_distance + result.m_allowedPenetration;
198 c = pointCollector.m_pointInWorld;
199 n = pointCollector.m_normalOnBInWorld;
203 result.reportFailure(-1, numIter);
208 if (numIter > MAX_ITERATIONS)
210 result.reportFailure(-2, numIter);
215 result.m_fraction = lambda;
217 result.m_hitPoint = c;