Blender  V3.3
btGjkConvexCast.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 "btGjkConvexCast.h"
18 #include "btGjkPairDetector.h"
19 #include "btPointCollector.h"
21 
22 #ifdef BT_USE_DOUBLE_PRECISION
23 #define MAX_ITERATIONS 64
24 #else
25 #define MAX_ITERATIONS 32
26 #endif
27 
29  : m_simplexSolver(simplexSolver),
30  m_convexA(convexA),
31  m_convexB(convexB)
32 {
33 }
34 
36  const btTransform& fromA,
37  const btTransform& toA,
38  const btTransform& fromB,
39  const btTransform& toB,
41 {
42  m_simplexSolver->reset();
43 
45  //assume no rotation/angular velocity, assert here?
46  btVector3 linVelA, linVelB;
47  linVelA = toA.getOrigin() - fromA.getOrigin();
48  linVelB = toB.getOrigin() - fromB.getOrigin();
49 
50  btScalar radius = btScalar(0.001);
51  btScalar lambda = btScalar(0.);
52  btVector3 v(1, 0, 0);
53 
54  int maxIter = MAX_ITERATIONS;
55 
56  btVector3 n;
57  n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
58  bool hasResult = false;
59  btVector3 c;
60  btVector3 r = (linVelA - linVelB);
61 
62  btScalar lastLambda = lambda;
63  //btScalar epsilon = btScalar(0.001);
64 
65  int numIter = 0;
66  //first solution, using GJK
67 
68  btTransform identityTrans;
69  identityTrans.setIdentity();
70 
71  // result.drawCoordSystem(sphereTr);
72 
73  btPointCollector pointCollector;
74 
75  btGjkPairDetector gjk(m_convexA, m_convexB, m_simplexSolver, 0); //m_penetrationDepthSolver);
77 
78  //we don't use margins during CCD
79  // gjk.setIgnoreMargin(true);
80 
81  input.m_transformA = fromA;
82  input.m_transformB = fromB;
83  gjk.getClosestPoints(input, pointCollector, 0);
84 
85  hasResult = pointCollector.m_hasResult;
86  c = pointCollector.m_pointInWorld;
87 
88  if (hasResult)
89  {
90  btScalar dist;
91  dist = pointCollector.m_distance;
92  n = pointCollector.m_normalOnBInWorld;
93 
94  //not close enough
95  while (dist > radius)
96  {
97  numIter++;
98  if (numIter > maxIter)
99  {
100  return false; //todo: report a failure
101  }
102  btScalar dLambda = btScalar(0.);
103 
104  btScalar projectedLinearVelocity = r.dot(n);
105 
106  dLambda = dist / (projectedLinearVelocity);
107 
108  lambda = lambda - dLambda;
109 
110  if (lambda > btScalar(1.))
111  return false;
112 
113  if (lambda < btScalar(0.))
114  return false;
115 
116  //todo: next check with relative epsilon
117  if (lambda <= lastLambda)
118  {
119  return false;
120  //n.setValue(0,0,0);
121  break;
122  }
123  lastLambda = lambda;
124 
125  //interpolate to next lambda
126  result.DebugDraw(lambda);
127  input.m_transformA.getOrigin().setInterpolate3(fromA.getOrigin(), toA.getOrigin(), lambda);
128  input.m_transformB.getOrigin().setInterpolate3(fromB.getOrigin(), toB.getOrigin(), lambda);
129 
130  gjk.getClosestPoints(input, pointCollector, 0);
131  if (pointCollector.m_hasResult)
132  {
133  if (pointCollector.m_distance < btScalar(0.))
134  {
135  result.m_fraction = lastLambda;
136  n = pointCollector.m_normalOnBInWorld;
137  result.m_normal = n;
138  result.m_hitPoint = pointCollector.m_pointInWorld;
139  return true;
140  }
141  c = pointCollector.m_pointInWorld;
142  n = pointCollector.m_normalOnBInWorld;
143  dist = pointCollector.m_distance;
144  }
145  else
146  {
147  //??
148  return false;
149  }
150  }
151 
152  //is n normalized?
153  //don't report time of impact for motion away from the contact normal (or causes minor penetration)
154  if (n.dot(r) >= -result.m_allowedPenetration)
155  return false;
156 
157  result.m_fraction = lambda;
158  result.m_normal = n;
159  result.m_hitPoint = c;
160  return true;
161  }
162 
163  return false;
164 }
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
ATTR_WARN_UNUSED_RESULT const BMVert * v
btConvexShape()
not supported on IBM SDK, until we fix the alignment of btVector3
#define MAX_ITERATIONS
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define btSimplexSolverInterface
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
cast a convex against another convex object
btGjkConvexCast(const btConvexShape *convexA, const btConvexShape *convexB, btSimplexSolverInterface *simplexSolver)
btGjkPairDetector uses GJK to implement the btDiscreteCollisionDetectorInterface
virtual void getClosestPoints(const ClosestPointInput &input, Result &output, class btIDebugDraw *debugDraw, bool swapResults=false)
ccl_global KernelShaderEvalInput * input
static unsigned c
Definition: RandGen.cpp:83
btVector3 m_normalOnBInWorld
btVector3 m_pointInWorld