Bullet Collision Detection & Physics Library: btSubSimplexConvexCast.cpp Source File

Bullet Collision Detection & Physics Library

btSubSimplexConvexCast.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btSubSimplexConvexCast.h"
17#include "BulletCollision/CollisionShapes/btConvexShape.h"
18
19#include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h"
20#include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
21#include "btPointCollector.h"
22#include "LinearMath/btTransformUtil.h"
23
24btSubsimplexConvexCast::btSubsimplexConvexCast(const btConvexShape* convexA, const btConvexShape* convexB, btSimplexSolverInterface* simplexSolver)
25 : m_simplexSolver(simplexSolver),
26 m_convexA(convexA),
27 m_convexB(convexB)
28{
29}
30
31
32 bool btSubsimplexConvexCast::calcTimeOfImpact(
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}
btConvexShape.h
btMinkowskiSumShape.h
btPointCollector.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:543
btSimplexSolverInterface.h
btSimplexSolverInterface
#define btSimplexSolverInterface
Definition: btSimplexSolverInterface.h:24
btSubSimplexConvexCast.h
btTransformUtil.h
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:33
btConvexShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
btSubsimplexConvexCast::btSubsimplexConvexCast
btSubsimplexConvexCast(const btConvexShape *shapeA, const btConvexShape *shapeB, btSimplexSolverInterface *simplexSolver)
Definition: btSubSimplexConvexCast.cpp:24
btSubsimplexConvexCast::m_convexA
const btConvexShape * m_convexA
Definition: btSubSimplexConvexCast.h:30
btSubsimplexConvexCast::m_simplexSolver
btSimplexSolverInterface * m_simplexSolver
Definition: btSubSimplexConvexCast.h:29
btSubsimplexConvexCast::calcTimeOfImpact
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 (...
Definition: btSubSimplexConvexCast.cpp:32
btSubsimplexConvexCast::m_convexB
const btConvexShape * m_convexB
Definition: btSubSimplexConvexCast.h:31
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:109
btTransform::getOrigin
btVector3 & getOrigin()
Return the origin vector translation.
Definition: btTransform.h:114
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btVector3::setInterpolate3
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:492
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btVector3::normalized
btVector3 normalized() const
Return a normalized version of this vector.
Definition: btVector3.h:949
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251
btConvexCast::CastResult
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...
Definition: btConvexCast.h:47
btConvexCast::CastResult::m_normal
btVector3 m_normal
Definition: btConvexCast.h:70
btConvexCast::CastResult::m_fraction
btScalar m_fraction
Definition: btConvexCast.h:72
btConvexCast::CastResult::m_subSimplexCastMaxIterations
int m_subSimplexCastMaxIterations
Definition: btConvexCast.h:76
btConvexCast::CastResult::m_subSimplexCastEpsilon
btScalar m_subSimplexCastEpsilon
Definition: btConvexCast.h:77
btConvexCast::CastResult::m_allowedPenetration
btScalar m_allowedPenetration
Definition: btConvexCast.h:74
btConvexCast::CastResult::m_hitPoint
btVector3 m_hitPoint
Definition: btConvexCast.h:71