Bullet Collision Detection & Physics Library
btSubSimplexConvexCast.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4 
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:
10 
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.
14 */
15 
16 #include "btSubSimplexConvexCast.h"
18 
21 #include "btPointCollector.h"
23 
25  : m_simplexSolver(simplexSolver),
26  m_convexA(convexA),
27  m_convexB(convexB)
28 {
29 }
30 
31 
33  const btTransform& fromA,
34  const btTransform& toA,
35  const btTransform& fromB,
36  const btTransform& toB,
37  CastResult& result)
38 {
39  m_simplexSolver->reset();
40 
41  btVector3 linVelA, linVelB;
42  linVelA = toA.getOrigin() - fromA.getOrigin();
43  linVelB = toB.getOrigin() - fromB.getOrigin();
44 
45  btScalar lambda = btScalar(0.);
46 
47  btTransform interpolatedTransA = fromA;
48  btTransform interpolatedTransB = fromB;
49 
51  btVector3 r = (linVelA - linVelB);
52  btVector3 v;
53 
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;
58 
59  btVector3 n;
60  n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
61 
62  btVector3 c;
63 
64  btScalar dist2 = v.length2();
65 
66 
67 
68  btVector3 w, p;
69  btScalar VdotR;
70 
71  while ((dist2 > result.m_subSimplexCastEpsilon) && maxIter--)
72  {
73  supVertexA = interpolatedTransA(m_convexA->localGetSupportingVertex(-v * interpolatedTransA.getBasis()));
74  supVertexB = interpolatedTransB(m_convexB->localGetSupportingVertex(v * interpolatedTransB.getBasis()));
75  w = supVertexA - supVertexB;
76 
77  btScalar VdotW = v.dot(w);
78 
79  if (lambda > btScalar(1.0))
80  {
81  return false;
82  }
83 
84  if (VdotW > btScalar(0.))
85  {
86  VdotR = v.dot(r);
87 
88  if (VdotR >= -(SIMD_EPSILON * SIMD_EPSILON))
89  return false;
90  else
91  {
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();
98  //check next line
99  w = supVertexA - supVertexB;
100 
101  n = v;
102  }
103  }
105  if (!m_simplexSolver->inSimplex(w))
106  m_simplexSolver->addVertex(w, supVertexA, supVertexB);
107 
108  if (m_simplexSolver->closest(v))
109  {
110  dist2 = v.length2();
111 
112  //todo: check this normal for validity
113  //n=v;
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());
117  }
118  else
119  {
120  dist2 = btScalar(0.);
121  }
122  }
123 
124  //int numiter = MAX_ITERATIONS - maxIter;
125  // printf("number of iterations: %d", numiter);
126 
127  //don't report a time of impact when moving 'away' from the hitnormal
128 
129  result.m_fraction = lambda;
130  if (n.length2() >= (SIMD_EPSILON * SIMD_EPSILON))
131  result.m_normal = n.normalized();
132  else
133  result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0));
134 
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)
137  return false;
138 
139  btVector3 hitA, hitB;
140  m_simplexSolver->compute_points(hitA, hitB);
141  result.m_hitPoint = hitB;
142  return true;
143 }
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_EPSILON
Definition: btScalar.h:543
#define btSimplexSolverInterface
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:33
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
btSubsimplexConvexCast(const btConvexShape *shapeA, const btConvexShape *shapeB, btSimplexSolverInterface *simplexSolver)
const btConvexShape * m_convexA
btSimplexSolverInterface * m_simplexSolver
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
SimsimplexConvexCast calculateTimeOfImpact calculates the time of impact+normal for the linear cast (...
const btConvexShape * m_convexB
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:108
btVector3 & getOrigin()
Return the origin vector translation.
Definition: btTransform.h:113
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:492
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btVector3 normalized() const
Return a normalized version of this vector.
Definition: btVector3.h:949
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...
Definition: btConvexCast.h:47
btScalar m_subSimplexCastEpsilon
Definition: btConvexCast.h:77