Revert rev16344 on trunk, bullet work is done in sim_physics branch as discussed...
[blender.git] / extern / bullet2 / src / BulletCollision / NarrowPhaseCollision / btSubSimplexConvexCast.cpp
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
17 #include "btSubSimplexConvexCast.h"
18 #include "BulletCollision/CollisionShapes/btConvexShape.h"
19 #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h"
20 #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
21
22
23 btSubsimplexConvexCast::btSubsimplexConvexCast (const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver)
24 :m_simplexSolver(simplexSolver),
25 m_convexA(convexA),m_convexB(convexB)
26 {
27 }
28
29 ///Typically the conservative advancement reaches solution in a few iterations, clip it to 32 for degenerate cases.
30 ///See discussion about this here http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=565
31 #ifdef BT_USE_DOUBLE_PRECISION
32 #define MAX_ITERATIONS 64
33 #else
34 #define MAX_ITERATIONS 32
35 #endif
36 bool    btSubsimplexConvexCast::calcTimeOfImpact(
37                 const btTransform& fromA,
38                 const btTransform& toA,
39                 const btTransform& fromB,
40                 const btTransform& toB,
41                 CastResult& result)
42 {
43
44         btMinkowskiSumShape combi(m_convexA,m_convexB);
45         btMinkowskiSumShape* convex = &combi;
46
47         btTransform     rayFromLocalA;
48         btTransform     rayToLocalA;
49
50         rayFromLocalA = fromA.inverse()* fromB;
51         rayToLocalA = toA.inverse()* toB;
52
53
54         m_simplexSolver->reset();
55
56         convex->setTransformB(btTransform(rayFromLocalA.getBasis()));
57
58         //btScalar radius = btScalar(0.01);
59
60         btScalar lambda = btScalar(0.);
61         //todo: need to verify this:
62         //because of minkowski difference, we need the inverse direction
63         
64         btVector3 s = -rayFromLocalA.getOrigin();
65         btVector3 r = -(rayToLocalA.getOrigin()-rayFromLocalA.getOrigin());
66         btVector3 x = s;
67         btVector3 v;
68         btVector3 arbitraryPoint = convex->localGetSupportingVertex(r);
69         
70         v = x - arbitraryPoint;
71
72         int maxIter = MAX_ITERATIONS;
73
74         btVector3 n;
75         n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
76         bool hasResult = false;
77         btVector3 c;
78
79         btScalar lastLambda = lambda;
80
81
82         btScalar dist2 = v.length2();
83 #ifdef BT_USE_DOUBLE_PRECISION
84         btScalar epsilon = btScalar(0.0001);
85 #else
86         btScalar epsilon = btScalar(0.0001);
87 #endif //BT_USE_DOUBLE_PRECISION
88         btVector3       w,p;
89         btScalar VdotR;
90         
91         while ( (dist2 > epsilon) && maxIter--)
92         {
93                 p = convex->localGetSupportingVertex( v);
94                  w = x - p;
95
96                 btScalar VdotW = v.dot(w);
97
98                 if ( VdotW > btScalar(0.))
99                 {
100                         VdotR = v.dot(r);
101
102                         if (VdotR >= -(SIMD_EPSILON*SIMD_EPSILON))
103                                 return false;
104                         else
105                         {
106                                 lambda = lambda - VdotW / VdotR;
107                                 x = s + lambda * r;
108                                 m_simplexSolver->reset();
109                                 //check next line
110                                 w = x-p;
111                                 lastLambda = lambda;
112                                 n = v;
113                                 hasResult = true;
114                         }
115                 } 
116                 m_simplexSolver->addVertex( w, x , p);
117                 if (m_simplexSolver->closest(v))
118                 {
119                         dist2 = v.length2();
120                         hasResult = true;
121                         //printf("V=%f , %f, %f\n",v[0],v[1],v[2]);
122                         //printf("DIST2=%f\n",dist2);
123                         //printf("numverts = %i\n",m_simplexSolver->numVertices());
124                 } else
125                 {
126                         dist2 = btScalar(0.);
127                 } 
128         }
129
130         //int numiter = MAX_ITERATIONS - maxIter;
131 //      printf("number of iterations: %d", numiter);
132         result.m_fraction = lambda;
133         result.m_normal = n;
134
135         return true;
136 }
137
138
139