[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletCollision / NarrowPhaseCollision / btSubSimplexConvexCast.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  https://bulletphysics.org
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"
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
24 btSubsimplexConvexCast::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
50         ///take relative motion
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                 }
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);
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 }