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 "btSubSimplexConvexCast.h"
17 #include "BulletCollision/CollisionShapes/btConvexShape.h"
19 #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h"
20 #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
21 #include "btPointCollector.h"
22 #include "LinearMath/btTransformUtil.h"
24 btSubsimplexConvexCast::btSubsimplexConvexCast(const btConvexShape* convexA, const btConvexShape* convexB, btSimplexSolverInterface* simplexSolver)
25 : m_simplexSolver(simplexSolver),
32 bool btSubsimplexConvexCast::calcTimeOfImpact(
33 const btTransform& fromA,
34 const btTransform& toA,
35 const btTransform& fromB,
36 const btTransform& toB,
39 m_simplexSolver->reset();
41 btVector3 linVelA, linVelB;
42 linVelA = toA.getOrigin() - fromA.getOrigin();
43 linVelB = toB.getOrigin() - fromB.getOrigin();
45 btScalar lambda = btScalar(0.);
47 btTransform interpolatedTransA = fromA;
48 btTransform interpolatedTransB = fromB;
50 ///take relative motion
51 btVector3 r = (linVelA - linVelB);
54 btVector3 supVertexA = fromA(m_convexA->localGetSupportingVertex(-r * fromA.getBasis()));
55 btVector3 supVertexB = fromB(m_convexB->localGetSupportingVertex(r * fromB.getBasis()));
56 v = supVertexA - supVertexB;
57 int maxIter = result.m_subSimplexCastMaxIterations;
60 n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
64 btScalar dist2 = v.length2();
71 while ((dist2 > result.m_subSimplexCastEpsilon) && maxIter--)
73 supVertexA = interpolatedTransA(m_convexA->localGetSupportingVertex(-v * interpolatedTransA.getBasis()));
74 supVertexB = interpolatedTransB(m_convexB->localGetSupportingVertex(v * interpolatedTransB.getBasis()));
75 w = supVertexA - supVertexB;
77 btScalar VdotW = v.dot(w);
79 if (lambda > btScalar(1.0))
84 if (VdotW > btScalar(0.))
88 if (VdotR >= -(SIMD_EPSILON * SIMD_EPSILON))
92 lambda = lambda - VdotW / VdotR;
93 //interpolate to next lambda
94 // x = s + lambda * r;
95 interpolatedTransA.getOrigin().setInterpolate3(fromA.getOrigin(), toA.getOrigin(), lambda);
96 interpolatedTransB.getOrigin().setInterpolate3(fromB.getOrigin(), toB.getOrigin(), lambda);
97 //m_simplexSolver->reset();
99 w = supVertexA - supVertexB;
104 ///Just like regular GJK only add the vertex if it isn't already (close) to current vertex, it would lead to divisions by zero and NaN etc.
105 if (!m_simplexSolver->inSimplex(w))
106 m_simplexSolver->addVertex(w, supVertexA, supVertexB);
108 if (m_simplexSolver->closest(v))
112 //todo: check this normal for validity
114 //printf("V=%f , %f, %f\n",v[0],v[1],v[2]);
115 //printf("DIST2=%f\n",dist2);
116 //printf("numverts = %i\n",m_simplexSolver->numVertices());
120 dist2 = btScalar(0.);
124 //int numiter = MAX_ITERATIONS - maxIter;
125 // printf("number of iterations: %d", numiter);
127 //don't report a time of impact when moving 'away' from the hitnormal
129 result.m_fraction = lambda;
130 if (n.length2() >= (SIMD_EPSILON * SIMD_EPSILON))
131 result.m_normal = n.normalized();
133 result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0));
135 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
136 if (result.m_normal.dot(r) >= -result.m_allowedPenetration)
139 btVector3 hitA, hitB;
140 m_simplexSolver->compute_points(hitA, hitB);
141 result.m_hitPoint = hitB;