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 "SpuSubSimplexConvexCast.h" |
---|
17 | |
---|
18 | |
---|
19 | #include "BulletCollision/CollisionShapes/btConvexShape.h" |
---|
20 | #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h" |
---|
21 | #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h" |
---|
22 | |
---|
23 | |
---|
24 | SpuSubsimplexRayCast::SpuSubsimplexRayCast (void* shapeB, SpuConvexPolyhedronVertexData* convexDataB, int shapeTypeB, float marginB, |
---|
25 | SpuVoronoiSimplexSolver* simplexSolver) |
---|
26 | :m_simplexSolver(simplexSolver), m_shapeB(shapeB), m_convexDataB(convexDataB), m_shapeTypeB(shapeTypeB), m_marginB(marginB) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | ///Typically the conservative advancement reaches solution in a few iterations, clip it to 32 for degenerate cases. |
---|
31 | ///See discussion about this here http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=565 |
---|
32 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
33 | #define MAX_ITERATIONS 64 |
---|
34 | #else |
---|
35 | #define MAX_ITERATIONS 32 |
---|
36 | #endif |
---|
37 | |
---|
38 | /* Returns the support point of the minkowski sum: |
---|
39 | * MSUM(Pellet, ConvexShape) |
---|
40 | * |
---|
41 | */ |
---|
42 | void supportPoints (const btTransform xformRay, |
---|
43 | const btTransform xformB, |
---|
44 | const int shapeType, |
---|
45 | const void* shape, |
---|
46 | SpuConvexPolyhedronVertexData* convexVertexData, |
---|
47 | const btScalar marginB, |
---|
48 | const btVector3& seperatingAxis, |
---|
49 | btVector3& w, |
---|
50 | btVector3& supVertexRay, |
---|
51 | btVector3& supVertexB) |
---|
52 | { |
---|
53 | btVector3 saUnit = seperatingAxis; |
---|
54 | saUnit.normalize(); |
---|
55 | btVector3 SupportPellet = xformRay(0.0001 * -saUnit); |
---|
56 | btVector3 rotatedSeperatingAxis = seperatingAxis * xformB.getBasis(); |
---|
57 | btVector3 SupportShape = xformB(localGetSupportingVertexWithoutMargin(shapeType, (void*)shape, rotatedSeperatingAxis, convexVertexData)); |
---|
58 | SupportShape += saUnit * marginB; |
---|
59 | w = SupportPellet - SupportShape; |
---|
60 | supVertexRay = SupportPellet; |
---|
61 | supVertexB = SupportShape; |
---|
62 | } |
---|
63 | |
---|
64 | bool SpuSubsimplexRayCast::calcTimeOfImpact(const btTransform& fromRay, |
---|
65 | const btTransform& toRay, |
---|
66 | const btTransform& fromB, |
---|
67 | const btTransform& toB, |
---|
68 | SpuCastResult& result) |
---|
69 | { |
---|
70 | m_simplexSolver->reset(); |
---|
71 | |
---|
72 | btVector3 linVelRay, linVelB; |
---|
73 | linVelRay = toRay.getOrigin() - fromRay.getOrigin(); |
---|
74 | linVelB = toB.getOrigin() - fromB.getOrigin (); |
---|
75 | |
---|
76 | btScalar lambda = btScalar(0.); |
---|
77 | |
---|
78 | btTransform interpolatedTransRay = fromRay; |
---|
79 | btTransform interpolatedTransB = fromB; |
---|
80 | |
---|
81 | btVector3 r = (linVelRay-linVelB); |
---|
82 | btVector3 supVertexRay; |
---|
83 | btVector3 supVertexB; |
---|
84 | btVector3 v; |
---|
85 | supportPoints (fromRay, fromB, m_shapeTypeB, m_shapeB, m_convexDataB, m_marginB, r, v, supVertexRay, supVertexB); |
---|
86 | |
---|
87 | btVector3 n; |
---|
88 | n.setValue(btScalar(0.), btScalar(0.), btScalar(0.)); |
---|
89 | bool hasResult = false; |
---|
90 | btVector3 c; |
---|
91 | int maxIter = MAX_ITERATIONS; |
---|
92 | |
---|
93 | btScalar lastLambda = lambda; |
---|
94 | |
---|
95 | btScalar dist2 = v.length2(); |
---|
96 | |
---|
97 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
98 | btScalar epsilon = btScalar(0.0001); |
---|
99 | #else |
---|
100 | btScalar epsilon = btScalar(0.0001); |
---|
101 | #endif //BT_USE_DOUBLE_PRECISION |
---|
102 | btVector3 w,p; |
---|
103 | btScalar VdotR; |
---|
104 | |
---|
105 | while ( (dist2 > epsilon) && maxIter--) |
---|
106 | { |
---|
107 | supportPoints (interpolatedTransRay, interpolatedTransB, m_shapeTypeB, m_shapeB, m_convexDataB, m_marginB, v, w, supVertexRay, supVertexB); |
---|
108 | |
---|
109 | btScalar VdotW = v.dot(w); |
---|
110 | |
---|
111 | if (lambda > btScalar(1.0)) |
---|
112 | { |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | if ( VdotW > btScalar(0.)) |
---|
117 | { |
---|
118 | VdotR = v.dot(r); |
---|
119 | |
---|
120 | if (VdotR >= -(SIMD_EPSILON*SIMD_EPSILON)) |
---|
121 | return false; |
---|
122 | else |
---|
123 | { |
---|
124 | lambda = lambda - VdotW / VdotR; |
---|
125 | interpolatedTransRay.getOrigin().setInterpolate3(fromRay.getOrigin(), toRay.getOrigin(), lambda); |
---|
126 | interpolatedTransB.getOrigin().setInterpolate3(fromB.getOrigin(), toB.getOrigin(), lambda); |
---|
127 | lastLambda = lambda; |
---|
128 | n = v; |
---|
129 | hasResult = true; |
---|
130 | } |
---|
131 | } |
---|
132 | m_simplexSolver->addVertex(w, supVertexRay, supVertexB); |
---|
133 | if (m_simplexSolver->closest(v)) |
---|
134 | { |
---|
135 | dist2 = v.length2(); |
---|
136 | hasResult = true; |
---|
137 | //printf("V=%f , %f, %f\n",v[0],v[1],v[2]); |
---|
138 | //printf("DIST2=%f\n",dist2); |
---|
139 | //printf("numverts = %i\n",m_simplexSolver->numVertices()); |
---|
140 | } else |
---|
141 | { |
---|
142 | dist2 = btScalar(0.); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | result.m_fraction = lambda; |
---|
147 | result.m_normal = n; |
---|
148 | btVector3 hitRay, hitB; |
---|
149 | m_simplexSolver->compute_points (hitRay, hitB); |
---|
150 | /* TODO: We could output hit point here (hitB) */ |
---|
151 | return true; |
---|
152 | } |
---|