Normand Briere
2018-07-01 89c1ad67bc65d24ceadfa9e95f8c5515283f1e97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * Bullet Continuous Collision Detection and Physics Library
 * Copyright (c) 2003-2008 Erwin Coumans  http://www.bulletphysics.com/
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose, 
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 * 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.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
 
package com.bulletphysics.collision.dispatch;
 
import com.bulletphysics.util.ObjectPool;
import com.bulletphysics.collision.broadphase.CollisionAlgorithm;
import com.bulletphysics.collision.broadphase.CollisionAlgorithmConstructionInfo;
import com.bulletphysics.collision.broadphase.DispatcherInfo;
import com.bulletphysics.collision.narrowphase.ConvexCast;
import com.bulletphysics.collision.narrowphase.ConvexPenetrationDepthSolver;
import com.bulletphysics.collision.narrowphase.DiscreteCollisionDetectorInterface.ClosestPointInput;
import com.bulletphysics.collision.narrowphase.GjkConvexCast;
import com.bulletphysics.collision.narrowphase.GjkPairDetector;
import com.bulletphysics.collision.narrowphase.PersistentManifold;
import com.bulletphysics.collision.narrowphase.SimplexSolverInterface;
import com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver;
import com.bulletphysics.collision.shapes.ConvexShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.util.ObjectArrayList;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
 
/**
 * ConvexConvexAlgorithm collision algorithm implements time of impact, convex
 * closest points and penetration depth calculations.
 * 
 * @author jezek2
 */
public class ConvexConvexAlgorithm extends CollisionAlgorithm {
   
   protected final ObjectPool<ClosestPointInput> pointInputsPool = ObjectPool.get(ClosestPointInput.class);
 
   private GjkPairDetector gjkPairDetector = new GjkPairDetector();
 
   public boolean ownManifold;
   public PersistentManifold manifoldPtr;
   public boolean lowLevelOfDetail;
   
   public void init(PersistentManifold mf, CollisionAlgorithmConstructionInfo ci, CollisionObject body0, CollisionObject body1, SimplexSolverInterface simplexSolver, ConvexPenetrationDepthSolver pdSolver) {
       super.init(ci);
       gjkPairDetector.init(null, null, simplexSolver, pdSolver);
       this.manifoldPtr = mf;
       this.ownManifold = false;
       this.lowLevelOfDetail = false;
   }
   
   @Override
   public void destroy() {
       if (ownManifold) {
           if (manifoldPtr != null) {
               dispatcher.releaseManifold(manifoldPtr);
           }
           manifoldPtr = null;
       }
   }
 
   public void setLowLevelOfDetail(boolean useLowLevel) {
       this.lowLevelOfDetail = useLowLevel;
   }
 
   /**
    * Convex-Convex collision algorithm.
    */
   @Override
   public void processCollision(CollisionObject body0, CollisionObject body1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
       if (manifoldPtr == null) {
           // swapped?
           manifoldPtr = dispatcher.getNewManifold(body0, body1);
           ownManifold = true;
       }
       resultOut.setPersistentManifold(manifoldPtr);
 
//    #ifdef USE_BT_GJKEPA
//        btConvexShape*                shape0(static_cast<btConvexShape*>(body0->getCollisionShape()));
//        btConvexShape*                shape1(static_cast<btConvexShape*>(body1->getCollisionShape()));
//        const btScalar                radialmargin(0/*shape0->getMargin()+shape1->getMargin()*/);
//        btGjkEpaSolver::sResults    results;
//        if(btGjkEpaSolver::Collide(    shape0,body0->getWorldTransform(),
//                                    shape1,body1->getWorldTransform(),
//                                    radialmargin,results))
//            {
//            dispatchInfo.m_debugDraw->drawLine(results.witnesses[1],results.witnesses[1]+results.normal,btVector3(255,0,0));
//            resultOut->addContactPoint(results.normal,results.witnesses[1],-results.depth);
//            }
//    #else
 
       ConvexShape min0 = (ConvexShape) body0.getCollisionShape();
       ConvexShape min1 = (ConvexShape) body1.getCollisionShape();
 
       ClosestPointInput input = pointInputsPool.get();
       input.init();
 
       // JAVA NOTE: original: TODO: if (dispatchInfo.m_useContinuous)
       gjkPairDetector.setMinkowskiA(min0);
       gjkPairDetector.setMinkowskiB(min1);
       input.maximumDistanceSquared = min0.getMargin() + min1.getMargin() + manifoldPtr.getContactBreakingThreshold();
       input.maximumDistanceSquared *= input.maximumDistanceSquared;
       //input.m_stackAlloc = dispatchInfo.m_stackAllocator;
 
       //    input.m_maximumDistanceSquared = btScalar(1e30);
 
       body0.getWorldTransform(input.transformA);
       body1.getWorldTransform(input.transformB);
 
       gjkPairDetector.getClosestPoints(input, resultOut, dispatchInfo.debugDraw);
       
       pointInputsPool.release(input);
       //    #endif
 
       if (ownManifold) {
           resultOut.refreshContactPoints();
       }
   }
 
   private static boolean disableCcd = false;
   
   @Override
   public float calculateTimeOfImpact(CollisionObject col0, CollisionObject col1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
       Vector3f tmp = Stack.alloc(Vector3f.class);
       
       Transform tmpTrans1 = Stack.alloc(Transform.class);
       Transform tmpTrans2 = Stack.alloc(Transform.class);
 
       // Rather then checking ALL pairs, only calculate TOI when motion exceeds threshold
 
       // Linear motion for one of objects needs to exceed m_ccdSquareMotionThreshold
       // col0->m_worldTransform,
       float resultFraction = 1f;
 
       tmp.sub(col0.getInterpolationWorldTransform(tmpTrans1).origin, col0.getWorldTransform(tmpTrans2).origin);
       float squareMot0 = tmp.lengthSquared();
 
       tmp.sub(col1.getInterpolationWorldTransform(tmpTrans1).origin, col1.getWorldTransform(tmpTrans2).origin);
       float squareMot1 = tmp.lengthSquared();
 
       if (squareMot0 < col0.getCcdSquareMotionThreshold() &&
               squareMot1 < col1.getCcdSquareMotionThreshold()) {
           return resultFraction;
       }
 
       if (disableCcd) {
           return 1f;
       }
       
       Transform tmpTrans3 = Stack.alloc(Transform.class);
       Transform tmpTrans4 = Stack.alloc(Transform.class);
 
       // An adhoc way of testing the Continuous Collision Detection algorithms
       // One object is approximated as a sphere, to simplify things
       // Starting in penetration should report no time of impact
       // For proper CCD, better accuracy and handling of 'allowed' penetration should be added
       // also the mainloop of the physics should have a kind of toi queue (something like Brian Mirtich's application of Timewarp for Rigidbodies)
 
       // Convex0 against sphere for Convex1
       {
           ConvexShape convex0 = (ConvexShape) col0.getCollisionShape();
 
           SphereShape sphere1 = new SphereShape(col1.getCcdSweptSphereRadius()); // todo: allow non-zero sphere sizes, for better approximation
           ConvexCast.CastResult result = new ConvexCast.CastResult();
           VoronoiSimplexSolver voronoiSimplex = new VoronoiSimplexSolver();
           //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
           ///Simplification, one object is simplified as a sphere
           GjkConvexCast ccd1 = new GjkConvexCast(convex0, sphere1, voronoiSimplex);
           //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
           if (ccd1.calcTimeOfImpact(col0.getWorldTransform(tmpTrans1), col0.getInterpolationWorldTransform(tmpTrans2),
                   col1.getWorldTransform(tmpTrans3), col1.getInterpolationWorldTransform(tmpTrans4), result)) {
               // store result.m_fraction in both bodies
 
               if (col0.getHitFraction() > result.fraction) {
                   col0.setHitFraction(result.fraction);
               }
 
               if (col1.getHitFraction() > result.fraction) {
                   col1.setHitFraction(result.fraction);
               }
 
               if (resultFraction > result.fraction) {
                   resultFraction = result.fraction;
               }
           }
       }
 
       // Sphere (for convex0) against Convex1
       {
           ConvexShape convex1 = (ConvexShape) col1.getCollisionShape();
 
           SphereShape sphere0 = new SphereShape(col0.getCcdSweptSphereRadius()); // todo: allow non-zero sphere sizes, for better approximation
           ConvexCast.CastResult result = new ConvexCast.CastResult();
           VoronoiSimplexSolver voronoiSimplex = new VoronoiSimplexSolver();
           //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
           ///Simplification, one object is simplified as a sphere
           GjkConvexCast ccd1 = new GjkConvexCast(sphere0, convex1, voronoiSimplex);
           //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
           if (ccd1.calcTimeOfImpact(col0.getWorldTransform(tmpTrans1), col0.getInterpolationWorldTransform(tmpTrans2),
                   col1.getWorldTransform(tmpTrans3), col1.getInterpolationWorldTransform(tmpTrans4), result)) {
               //store result.m_fraction in both bodies
 
               if (col0.getHitFraction() > result.fraction) {
                   col0.setHitFraction(result.fraction);
               }
 
               if (col1.getHitFraction() > result.fraction) {
                   col1.setHitFraction(result.fraction);
               }
 
               if (resultFraction > result.fraction) {
                   resultFraction = result.fraction;
               }
 
           }
       }
 
       return resultFraction;
   }
 
   @Override
   public void getAllContactManifolds(ObjectArrayList<PersistentManifold> manifoldArray) {
       // should we use ownManifold to avoid adding duplicates?
       if (manifoldPtr != null && ownManifold) {
           manifoldArray.add(manifoldPtr);
       }
   }
   
   public PersistentManifold getManifold() {
       return manifoldPtr;
   }
   
   ////////////////////////////////////////////////////////////////////////////
   
   public static class CreateFunc extends CollisionAlgorithmCreateFunc {
       private final ObjectPool<ConvexConvexAlgorithm> pool = ObjectPool.get(ConvexConvexAlgorithm.class);
 
       public ConvexPenetrationDepthSolver pdSolver;
       public SimplexSolverInterface simplexSolver;
 
       public CreateFunc(SimplexSolverInterface simplexSolver, ConvexPenetrationDepthSolver pdSolver) {
           this.simplexSolver = simplexSolver;
           this.pdSolver = pdSolver;
       }
 
       @Override
       public CollisionAlgorithm createCollisionAlgorithm(CollisionAlgorithmConstructionInfo ci, CollisionObject body0, CollisionObject body1) {
           ConvexConvexAlgorithm algo = pool.get();
           algo.init(ci.manifold, ci, body0, body1, simplexSolver, pdSolver);
           return algo;
       }
 
       @Override
       public void releaseCollisionAlgorithm(CollisionAlgorithm algo) {
           pool.release((ConvexConvexAlgorithm)algo);
       }
   }
   
}