Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
 * 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.BulletGlobals;
import com.bulletphysics.BulletStats;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.BroadphaseNativeType;
import com.bulletphysics.collision.broadphase.BroadphaseProxy;
import com.bulletphysics.collision.broadphase.CollisionFilterGroups;
import com.bulletphysics.collision.broadphase.Dispatcher;
import com.bulletphysics.collision.broadphase.DispatcherInfo;
import com.bulletphysics.collision.broadphase.OverlappingPairCache;
import com.bulletphysics.collision.narrowphase.ConvexCast;
import com.bulletphysics.collision.narrowphase.ConvexCast.CastResult;
import com.bulletphysics.collision.narrowphase.GjkConvexCast;
import com.bulletphysics.collision.narrowphase.GjkEpaPenetrationDepthSolver;
import com.bulletphysics.collision.narrowphase.SubsimplexConvexCast;
import com.bulletphysics.collision.narrowphase.TriangleConvexcastCallback;
import com.bulletphysics.collision.narrowphase.TriangleRaycastCallback;
import com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver;
import com.bulletphysics.collision.shapes.BvhTriangleMeshShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.collision.shapes.ConcaveShape;
import com.bulletphysics.collision.shapes.ConvexShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.collision.shapes.TriangleMeshShape;
import com.bulletphysics.linearmath.AabbUtil2;
import com.bulletphysics.linearmath.IDebugDraw;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.linearmath.TransformUtil;
import com.bulletphysics.linearmath.VectorUtil;
import com.bulletphysics.util.ObjectArrayList;
import cz.advel.stack.Stack;
import javax.vecmath.Matrix3f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
 
/**
 * CollisionWorld is interface and container for the collision detection.
 * 
 * @author jezek2
 */
public class CollisionWorld  implements java.io.Serializable {
 
   //protected final BulletStack stack = BulletStack.get();
   
   protected ObjectArrayList<CollisionObject> collisionObjects = new ObjectArrayList<CollisionObject>();
   protected Dispatcher dispatcher1;
   protected DispatcherInfo dispatchInfo = new DispatcherInfo();
   //protected btStackAlloc*    m_stackAlloc;
   protected BroadphaseInterface broadphasePairCache;
   protected IDebugDraw debugDrawer;
   
   /**
    * This constructor doesn't own the dispatcher and paircache/broadphase.
    */
   public CollisionWorld(Dispatcher dispatcher,BroadphaseInterface broadphasePairCache, CollisionConfiguration collisionConfiguration) {
       this.dispatcher1 = dispatcher;
       this.broadphasePairCache = broadphasePairCache;
   }
   
   public void destroy() {
       // clean up remaining objects
       for (int i = 0; i < collisionObjects.size(); i++) {
           CollisionObject collisionObject = collisionObjects.getQuick(i);
 
           BroadphaseProxy bp = collisionObject.getBroadphaseHandle();
           if (bp != null) {
               //
               // only clear the cached algorithms
               //
               getBroadphase().getOverlappingPairCache().cleanProxyFromPairs(bp, dispatcher1);
               getBroadphase().destroyProxy(bp, dispatcher1);
           }
       }
   }
   
   public void addCollisionObject(CollisionObject collisionObject) {
       addCollisionObject(collisionObject, CollisionFilterGroups.DEFAULT_FILTER, CollisionFilterGroups.ALL_FILTER);
   }
 
   public void addCollisionObject(CollisionObject collisionObject, short collisionFilterGroup, short collisionFilterMask) {
       // check that the object isn't already added
       assert (!collisionObjects.contains(collisionObject));
 
       collisionObjects.add(collisionObject);
 
       // calculate new AABB
       // TODO: check if it's overwritten or not
       Transform trans = collisionObject.getWorldTransform(Stack.alloc(Transform.class));
 
       Vector3f minAabb = Stack.alloc(Vector3f.class);
       Vector3f maxAabb = Stack.alloc(Vector3f.class);
       collisionObject.getCollisionShape().getAabb(trans, minAabb, maxAabb);
 
       BroadphaseNativeType type = collisionObject.getCollisionShape().getShapeType();
       collisionObject.setBroadphaseHandle(getBroadphase().createProxy(
               minAabb,
               maxAabb,
               type,
               collisionObject,
               collisionFilterGroup,
               collisionFilterMask,
               dispatcher1, null));
   }
 
   public void performDiscreteCollisionDetection() {
       BulletStats.pushProfile("performDiscreteCollisionDetection");
       try {
           //DispatcherInfo dispatchInfo = getDispatchInfo();
 
           updateAabbs();
 
           BulletStats.pushProfile("calculateOverlappingPairs");
           try {
               broadphasePairCache.calculateOverlappingPairs(dispatcher1);
           }
           finally {
               BulletStats.popProfile();
           }
 
           Dispatcher dispatcher = getDispatcher();
           {
               BulletStats.pushProfile("dispatchAllCollisionPairs");
               try {
                   if (dispatcher != null) {
                       dispatcher.dispatchAllCollisionPairs(broadphasePairCache.getOverlappingPairCache(), dispatchInfo, dispatcher1);
                   }
               }
               finally {
                   BulletStats.popProfile();
               }
           }
       }
       finally {
           BulletStats.popProfile();
       }
   }
   
   public void removeCollisionObject(CollisionObject collisionObject) {
       //bool removeFromBroadphase = false;
 
       {
           BroadphaseProxy bp = collisionObject.getBroadphaseHandle();
           if (bp != null) {
               //
               // only clear the cached algorithms
               //
               getBroadphase().getOverlappingPairCache().cleanProxyFromPairs(bp, dispatcher1);
               getBroadphase().destroyProxy(bp, dispatcher1);
               collisionObject.setBroadphaseHandle(null);
           }
       }
 
       //swapremove
       collisionObjects.remove(collisionObject);
   }
 
   public void setBroadphase(BroadphaseInterface pairCache) {
       broadphasePairCache = pairCache;
   }
   
   public BroadphaseInterface getBroadphase() {
       return broadphasePairCache;
   }
   
   public OverlappingPairCache getPairCache() {
       return broadphasePairCache.getOverlappingPairCache();
   }
 
   public Dispatcher getDispatcher() {
       return dispatcher1;
   }
 
   public DispatcherInfo getDispatchInfo() {
       return dispatchInfo;
   }
   
   private static boolean updateAabbs_reportMe = true;
 
   // JAVA NOTE: ported from 2.74, missing contact threshold stuff
   public void updateSingleAabb(CollisionObject colObj) {
       Vector3f minAabb = Stack.alloc(Vector3f.class), maxAabb = Stack.alloc(Vector3f.class);
       Vector3f tmp = Stack.alloc(Vector3f.class);
       Transform tmpTrans = Stack.alloc(Transform.class);
 
       colObj.getCollisionShape().getAabb(colObj.getWorldTransform(tmpTrans), minAabb, maxAabb);
       // need to increase the aabb for contact thresholds
       Vector3f contactThreshold = Stack.alloc(Vector3f.class);
       contactThreshold.set(BulletGlobals.getContactBreakingThreshold(), BulletGlobals.getContactBreakingThreshold(), BulletGlobals.getContactBreakingThreshold());
       minAabb.sub(contactThreshold);
       maxAabb.add(contactThreshold);
 
       BroadphaseInterface bp = broadphasePairCache;
 
       // moving objects should be moderately sized, probably something wrong if not
       tmp.sub(maxAabb, minAabb); // TODO: optimize
       if (colObj.isStaticObject() || (tmp.lengthSquared() < 1e12f)) {
           bp.setAabb(colObj.getBroadphaseHandle(), minAabb, maxAabb, dispatcher1);
       }
       else {
           // something went wrong, investigate
           // this assert is unwanted in 3D modelers (danger of loosing work)
           colObj.setActivationState(CollisionObject.DISABLE_SIMULATION);
 
           if (updateAabbs_reportMe && debugDrawer != null) {
               updateAabbs_reportMe = false;
               debugDrawer.reportErrorWarning("Overflow in AABB, object removed from simulation");
               debugDrawer.reportErrorWarning("If you can reproduce this, please email bugs@continuousphysics.com\n");
               debugDrawer.reportErrorWarning("Please include above information, your Platform, version of OS.\n");
               debugDrawer.reportErrorWarning("Thanks.\n");
           }
       }
   }
 
   public void updateAabbs() {
       BulletStats.pushProfile("updateAabbs");
       try {
           for (int i=0; i<collisionObjects.size(); i++) {
               CollisionObject colObj = collisionObjects.getQuick(i);
 
               // only update aabb of active objects
               if (colObj.isActive()) {
                   updateSingleAabb(colObj);
               }
           }
       }
       finally {
           BulletStats.popProfile();
       }
   }
 
   public IDebugDraw getDebugDrawer() {
       return debugDrawer;
   }
 
   public void setDebugDrawer(IDebugDraw debugDrawer) {
       this.debugDrawer = debugDrawer;
   }
   
   public int getNumCollisionObjects() {
       return collisionObjects.size();
   }
 
   // TODO
   public static void rayTestSingle(Transform rayFromTrans, Transform rayToTrans,
           CollisionObject collisionObject,
           CollisionShape collisionShape,
           Transform colObjWorldTransform,
           RayResultCallback resultCallback) {
       SphereShape pointShape = new SphereShape(0f);
       pointShape.setMargin(0f);
       ConvexShape castShape = pointShape;
 
       if (collisionShape.isConvex()) {
           CastResult castResult = new CastResult();
           castResult.fraction = resultCallback.closestHitFraction;
 
           ConvexShape convexShape = (ConvexShape) collisionShape;
           VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
 
           //#define USE_SUBSIMPLEX_CONVEX_CAST 1
           //#ifdef USE_SUBSIMPLEX_CONVEX_CAST
           SubsimplexConvexCast convexCaster = new SubsimplexConvexCast(castShape, convexShape, simplexSolver);
           //#else
           //btGjkConvexCast    convexCaster(castShape,convexShape,&simplexSolver);
           //btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0);
           //#endif //#USE_SUBSIMPLEX_CONVEX_CAST
 
           if (convexCaster.calcTimeOfImpact(rayFromTrans, rayToTrans, colObjWorldTransform, colObjWorldTransform, castResult)) {
               //add hit
               if (castResult.normal.lengthSquared() > 0.0001f) {
                   if (castResult.fraction < resultCallback.closestHitFraction) {
                       //#ifdef USE_SUBSIMPLEX_CONVEX_CAST
                       //rotate normal into worldspace
                       rayFromTrans.basis.transform(castResult.normal);
                       //#endif //USE_SUBSIMPLEX_CONVEX_CAST
 
                       castResult.normal.normalize();
                       LocalRayResult localRayResult = new LocalRayResult(
                               collisionObject,
                               null,
                               castResult.normal,
                               castResult.fraction);
 
                       boolean normalInWorldSpace = true;
                       resultCallback.addSingleResult(localRayResult, normalInWorldSpace);
                   }
               }
           }
       }
       else {
           if (collisionShape.isConcave()) {
               if (collisionShape.getShapeType() == BroadphaseNativeType.TRIANGLE_MESH_SHAPE_PROXYTYPE) {
                   // optimized version for BvhTriangleMeshShape
                   BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape)collisionShape;
                   Transform worldTocollisionObject = Stack.alloc(Transform.class);
                   worldTocollisionObject.inverse(colObjWorldTransform);
                   Vector3f rayFromLocal = (Vector3f) Stack.alloc(rayFromTrans.origin);
                   worldTocollisionObject.transform(rayFromLocal);
                   Vector3f rayToLocal = (Vector3f) Stack.alloc(rayToTrans.origin);
                   worldTocollisionObject.transform(rayToLocal);
 
                   BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
                   rcb.hitFraction = resultCallback.closestHitFraction;
                   triangleMesh.performRaycast(rcb, rayFromLocal, rayToLocal);
               }
               else {
                   ConcaveShape triangleMesh = (ConcaveShape)collisionShape;
 
                   Transform worldTocollisionObject = Stack.alloc(Transform.class);
                   worldTocollisionObject.inverse(colObjWorldTransform);
 
                   Vector3f rayFromLocal = (Vector3f)Stack.alloc(rayFromTrans.origin);
                   worldTocollisionObject.transform(rayFromLocal);
                   Vector3f rayToLocal = (Vector3f)Stack.alloc(rayToTrans.origin);
                   worldTocollisionObject.transform(rayToLocal);
 
                   BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
                   rcb.hitFraction = resultCallback.closestHitFraction;
 
                   Vector3f rayAabbMinLocal = (Vector3f)Stack.alloc(rayFromLocal);
                   VectorUtil.setMin(rayAabbMinLocal, rayToLocal);
                   Vector3f rayAabbMaxLocal = (Vector3f)Stack.alloc(rayFromLocal);
                   VectorUtil.setMax(rayAabbMaxLocal, rayToLocal);
 
                   triangleMesh.processAllTriangles(rcb, rayAabbMinLocal, rayAabbMaxLocal);
               }
           }
           else {
               // todo: use AABB tree or other BVH acceleration structure!
               if (collisionShape.isCompound()) {
                   CompoundShape compoundShape = (CompoundShape) collisionShape;
                   int i = 0;
                   Transform childTrans = Stack.alloc(Transform.class);
                   for (i = 0; i < compoundShape.getNumChildShapes(); i++) {
                       compoundShape.getChildTransform(i, childTrans);
                       CollisionShape childCollisionShape = compoundShape.getChildShape(i);
                       Transform childWorldTrans = (Transform) Stack.alloc(colObjWorldTransform);
                       childWorldTrans.mul(childTrans);
                       // replace collision shape so that callback can determine the triangle
                       CollisionShape saveCollisionShape = collisionObject.getCollisionShape();
                       collisionObject.internalSetTemporaryCollisionShape(childCollisionShape);
                       rayTestSingle(rayFromTrans, rayToTrans,
                               collisionObject,
                               childCollisionShape,
                               childWorldTrans,
                               resultCallback);
                       // restore
                       collisionObject.internalSetTemporaryCollisionShape(saveCollisionShape);
                   }
               }
           }
       }
   }
 
   private static class BridgeTriangleConvexcastCallback extends TriangleConvexcastCallback {
       public ConvexResultCallback resultCallback;
       public CollisionObject collisionObject;
       public TriangleMeshShape triangleMesh;
       public boolean normalInWorldSpace;
 
       public BridgeTriangleConvexcastCallback(ConvexShape castShape, Transform from, Transform to, ConvexResultCallback resultCallback, CollisionObject collisionObject, TriangleMeshShape triangleMesh, Transform triangleToWorld) {
           super(castShape, from, to, triangleToWorld, triangleMesh.getMargin());
           this.resultCallback = resultCallback;
           this.collisionObject = collisionObject;
           this.triangleMesh = triangleMesh;
       }
 
       @Override
       public float reportHit(Vector3f hitNormalLocal, Vector3f hitPointLocal, float hitFraction, int partId, int triangleIndex) {
           LocalShapeInfo shapeInfo = new LocalShapeInfo();
           shapeInfo.shapePart = partId;
           shapeInfo.triangleIndex = triangleIndex;
           if (hitFraction <= resultCallback.closestHitFraction) {
               LocalConvexResult convexResult = new LocalConvexResult(collisionObject, shapeInfo, hitNormalLocal, hitPointLocal, hitFraction);
               return resultCallback.addSingleResult(convexResult, normalInWorldSpace);
           }
           return hitFraction;
       }
   }
 
   /**
    * objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
    */
   public static void objectQuerySingle(ConvexShape castShape, Transform convexFromTrans, Transform convexToTrans, CollisionObject collisionObject, CollisionShape collisionShape, Transform colObjWorldTransform, ConvexResultCallback resultCallback, float allowedPenetration) {
       if (collisionShape.isConvex()) {
           CastResult castResult = new CastResult();
           castResult.allowedPenetration = allowedPenetration;
           castResult.fraction = 1f; // ??
 
           ConvexShape convexShape = (ConvexShape) collisionShape;
           VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
           GjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver = new GjkEpaPenetrationDepthSolver();
 
           // JAVA TODO: should be convexCaster1
           //ContinuousConvexCollision convexCaster1(castShape,convexShape,&simplexSolver,&gjkEpaPenetrationSolver);
           GjkConvexCast convexCaster2 = new GjkConvexCast(castShape, convexShape, simplexSolver);
           //btSubsimplexConvexCast convexCaster3(castShape,convexShape,&simplexSolver);
 
           ConvexCast castPtr = convexCaster2;
 
           if (castPtr.calcTimeOfImpact(convexFromTrans, convexToTrans, colObjWorldTransform, colObjWorldTransform, castResult)) {
               // add hit
               if (castResult.normal.lengthSquared() > 0.0001f) {
                   if (castResult.fraction < resultCallback.closestHitFraction) {
                       castResult.normal.normalize();
                       LocalConvexResult localConvexResult = new LocalConvexResult(collisionObject, null, castResult.normal, castResult.hitPoint, castResult.fraction);
 
                       boolean normalInWorldSpace = true;
                       resultCallback.addSingleResult(localConvexResult, normalInWorldSpace);
                   }
               }
           }
       }
       else {
           if (collisionShape.isConcave()) {
               if (collisionShape.getShapeType() == BroadphaseNativeType.TRIANGLE_MESH_SHAPE_PROXYTYPE) {
                   BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape)collisionShape;
                   Transform worldTocollisionObject = Stack.alloc(Transform.class);
                   worldTocollisionObject.inverse(colObjWorldTransform);
 
                   Vector3f convexFromLocal = Stack.alloc(Vector3f.class);
                   convexFromLocal.set(convexFromTrans.origin);
                   worldTocollisionObject.transform(convexFromLocal);
 
                   Vector3f convexToLocal = Stack.alloc(Vector3f.class);
                   convexToLocal.set(convexToTrans.origin);
                   worldTocollisionObject.transform(convexToLocal);
 
                   // rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation
                   Transform rotationXform = Stack.alloc(Transform.class);
                   Matrix3f tmpMat = Stack.alloc(Matrix3f.class);
                   tmpMat.mul(worldTocollisionObject.basis, convexToTrans.basis);
                   rotationXform.set(tmpMat);
 
                   BridgeTriangleConvexcastCallback tccb = new BridgeTriangleConvexcastCallback(castShape, convexFromTrans, convexToTrans, resultCallback, collisionObject, triangleMesh, colObjWorldTransform);
                   tccb.hitFraction = resultCallback.closestHitFraction;
                   tccb.normalInWorldSpace = true;
                   
                   Vector3f boxMinLocal = Stack.alloc(Vector3f.class);
                   Vector3f boxMaxLocal = Stack.alloc(Vector3f.class);
                   castShape.getAabb(rotationXform, boxMinLocal, boxMaxLocal);
                   triangleMesh.performConvexcast(tccb, convexFromLocal, convexToLocal, boxMinLocal, boxMaxLocal);
               }
               else {
                   BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape)collisionShape;
                   Transform worldTocollisionObject = Stack.alloc(Transform.class);
                   worldTocollisionObject.inverse(colObjWorldTransform);
 
                   Vector3f convexFromLocal = Stack.alloc(Vector3f.class);
                   convexFromLocal.set(convexFromTrans.origin);
                   worldTocollisionObject.transform(convexFromLocal);
 
                   Vector3f convexToLocal = Stack.alloc(Vector3f.class);
                   convexToLocal.set(convexToTrans.origin);
                   worldTocollisionObject.transform(convexToLocal);
 
                   // rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation
                   Transform rotationXform = Stack.alloc(Transform.class);
                   Matrix3f tmpMat = Stack.alloc(Matrix3f.class);
                   tmpMat.mul(worldTocollisionObject.basis, convexToTrans.basis);
                   rotationXform.set(tmpMat);
 
                   BridgeTriangleConvexcastCallback tccb = new BridgeTriangleConvexcastCallback(castShape, convexFromTrans, convexToTrans, resultCallback, collisionObject, triangleMesh, colObjWorldTransform);
                   tccb.hitFraction = resultCallback.closestHitFraction;
                   tccb.normalInWorldSpace = false;
                   Vector3f boxMinLocal = Stack.alloc(Vector3f.class);
                   Vector3f boxMaxLocal = Stack.alloc(Vector3f.class);
                   castShape.getAabb(rotationXform, boxMinLocal, boxMaxLocal);
 
                   Vector3f rayAabbMinLocal = (Vector3f) Stack.alloc(convexFromLocal);
                   VectorUtil.setMin(rayAabbMinLocal, convexToLocal);
                   Vector3f rayAabbMaxLocal = (Vector3f) Stack.alloc(convexFromLocal);
                   VectorUtil.setMax(rayAabbMaxLocal, convexToLocal);
                   rayAabbMinLocal.add(boxMinLocal);
                   rayAabbMaxLocal.add(boxMaxLocal);
                   triangleMesh.processAllTriangles(tccb, rayAabbMinLocal, rayAabbMaxLocal);
               }
           }
           else {
               // todo: use AABB tree or other BVH acceleration structure!
               if (collisionShape.isCompound()) {
                   CompoundShape compoundShape = (CompoundShape) collisionShape;
                   for (int i = 0; i < compoundShape.getNumChildShapes(); i++) {
                       Transform childTrans = compoundShape.getChildTransform(i, Stack.alloc(Transform.class));
                       CollisionShape childCollisionShape = compoundShape.getChildShape(i);
                       Transform childWorldTrans = Stack.alloc(Transform.class);
                       childWorldTrans.mul(colObjWorldTransform, childTrans);
                       // replace collision shape so that callback can determine the triangle
                       CollisionShape saveCollisionShape = collisionObject.getCollisionShape();
                       collisionObject.internalSetTemporaryCollisionShape(childCollisionShape);
                       objectQuerySingle(castShape, convexFromTrans, convexToTrans,
                                         collisionObject,
                                         childCollisionShape,
                                         childWorldTrans,
                                         resultCallback, allowedPenetration);
                       // restore
                       collisionObject.internalSetTemporaryCollisionShape(saveCollisionShape);
                   }
               }
           }
       }
   }
 
   /**
    * rayTest performs a raycast on all objects in the CollisionWorld, and calls the resultCallback.
    * This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback.
    */
   public void rayTest(Vector3f rayFromWorld, Vector3f rayToWorld, RayResultCallback resultCallback) {
       Transform rayFromTrans = Stack.alloc(Transform.class), rayToTrans = Stack.alloc(Transform.class);
       rayFromTrans.setIdentity();
       rayFromTrans.origin.set(rayFromWorld);
       rayToTrans.setIdentity();
 
       rayToTrans.origin.set(rayToWorld);
 
       // go over all objects, and if the ray intersects their aabb, do a ray-shape query using convexCaster (CCD)
       Vector3f collisionObjectAabbMin = Stack.alloc(Vector3f.class), collisionObjectAabbMax = Stack.alloc(Vector3f.class);
       float[] hitLambda = new float[1];
 
       Transform tmpTrans = Stack.alloc(Transform.class);
       
       for (int i = 0; i < collisionObjects.size(); i++) {
           // terminate further ray tests, once the closestHitFraction reached zero
           if (resultCallback.closestHitFraction == 0f) {
               break;
           }
 
           CollisionObject collisionObject = collisionObjects.getQuick(i);
           // only perform raycast if filterMask matches
           if (resultCallback.needsCollision(collisionObject.getBroadphaseHandle())) {
               //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
               collisionObject.getCollisionShape().getAabb(collisionObject.getWorldTransform(tmpTrans), collisionObjectAabbMin, collisionObjectAabbMax);
 
               hitLambda[0] = resultCallback.closestHitFraction;
               Vector3f hitNormal = Stack.alloc(Vector3f.class);
               if (AabbUtil2.rayAabb(rayFromWorld, rayToWorld, collisionObjectAabbMin, collisionObjectAabbMax, hitLambda, hitNormal)) {
                   rayTestSingle(rayFromTrans, rayToTrans,
                           collisionObject,
                           collisionObject.getCollisionShape(),
                           collisionObject.getWorldTransform(tmpTrans),
                           resultCallback);
               }
           }
 
       }
   }
 
   /**
    * convexTest performs a swept convex cast on all objects in the {@link CollisionWorld}, and calls the resultCallback
    * This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback.
    */
   public void convexSweepTest(ConvexShape castShape, Transform convexFromWorld, Transform convexToWorld, ConvexResultCallback resultCallback) {
       Transform convexFromTrans = Stack.alloc(Transform.class);
       Transform convexToTrans = Stack.alloc(Transform.class);
 
       convexFromTrans.set(convexFromWorld);
       convexToTrans.set(convexToWorld);
 
       Vector3f castShapeAabbMin = Stack.alloc(Vector3f.class);
       Vector3f castShapeAabbMax = Stack.alloc(Vector3f.class);
 
       // Compute AABB that encompasses angular movement
       {
           Vector3f linVel = Stack.alloc(Vector3f.class);
           Vector3f angVel = Stack.alloc(Vector3f.class);
           TransformUtil.calculateVelocity(convexFromTrans, convexToTrans, 1f, linVel, angVel);
           Transform R = Stack.alloc(Transform.class);
           R.setIdentity();
           R.setRotation(convexFromTrans.getRotation(Stack.alloc(Quat4f.class)));
           castShape.calculateTemporalAabb(R, linVel, angVel, 1f, castShapeAabbMin, castShapeAabbMax);
       }
 
       Transform tmpTrans = Stack.alloc(Transform.class);
       Vector3f collisionObjectAabbMin = Stack.alloc(Vector3f.class);
       Vector3f collisionObjectAabbMax = Stack.alloc(Vector3f.class);
       float[] hitLambda = new float[1];
 
       // go over all objects, and if the ray intersects their aabb + cast shape aabb,
       // do a ray-shape query using convexCaster (CCD)
       for (int i = 0; i < collisionObjects.size(); i++) {
           CollisionObject collisionObject = collisionObjects.getQuick(i);
 
           // only perform raycast if filterMask matches
           if (resultCallback.needsCollision(collisionObject.getBroadphaseHandle())) {
               //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
               collisionObject.getWorldTransform(tmpTrans);
               collisionObject.getCollisionShape().getAabb(tmpTrans, collisionObjectAabbMin, collisionObjectAabbMax);
               AabbUtil2.aabbExpand(collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
               hitLambda[0] = 1f; // could use resultCallback.closestHitFraction, but needs testing
               Vector3f hitNormal = Stack.alloc(Vector3f.class);
               if (AabbUtil2.rayAabb(convexFromWorld.origin, convexToWorld.origin, collisionObjectAabbMin, collisionObjectAabbMax, hitLambda, hitNormal)) {
                   objectQuerySingle(castShape, convexFromTrans, convexToTrans,
                                     collisionObject,
                                     collisionObject.getCollisionShape(),
                                     tmpTrans,
                                     resultCallback,
                                     getDispatchInfo().allowedCcdPenetration);
               }
           }
       }
   }
 
   public ObjectArrayList<CollisionObject> getCollisionObjectArray() {
       return collisionObjects;
   }
   
   ////////////////////////////////////////////////////////////////////////////
   
   /**
    * LocalShapeInfo gives extra information for complex shapes.
    * Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart.
    */
   public static class LocalShapeInfo {
       public int shapePart;
       public int triangleIndex;
       //const btCollisionShape*    m_shapeTemp;
       //const btTransform*    m_shapeLocalTransform;
   }
   
   public static class LocalRayResult {
       public CollisionObject collisionObject;
       public LocalShapeInfo localShapeInfo;
       public final Vector3f hitNormalLocal = new Vector3f();
       public float hitFraction;
 
       public LocalRayResult(CollisionObject collisionObject, LocalShapeInfo localShapeInfo, Vector3f hitNormalLocal, float hitFraction) {
           this.collisionObject = collisionObject;
           this.localShapeInfo = localShapeInfo;
           this.hitNormalLocal.set(hitNormalLocal);
           this.hitFraction = hitFraction;
       }
   }
   
   /**
    * RayResultCallback is used to report new raycast results.
    */
   public static abstract class RayResultCallback {
       public float closestHitFraction = 1f;
       public CollisionObject collisionObject;
       public short collisionFilterGroup = CollisionFilterGroups.DEFAULT_FILTER;
       public short collisionFilterMask = CollisionFilterGroups.ALL_FILTER;
       
       public boolean hasHit() {
           return (collisionObject != null);
       }
 
       public boolean needsCollision(BroadphaseProxy proxy0) {
           boolean collides = ((proxy0.collisionFilterGroup & collisionFilterMask) & 0xFFFF) != 0;
           collides = collides && ((collisionFilterGroup & proxy0.collisionFilterMask) & 0xFFFF) != 0;
           return collides;
       }
       
       public abstract float addSingleResult(LocalRayResult rayResult, boolean normalInWorldSpace);
   }
   
   public static class ClosestRayResultCallback extends RayResultCallback {
       public final Vector3f rayFromWorld = new Vector3f(); //used to calculate hitPointWorld from hitFraction
       public final Vector3f rayToWorld = new Vector3f();
 
       public final Vector3f hitNormalWorld = new Vector3f();
       public final Vector3f hitPointWorld = new Vector3f();
       
       public ClosestRayResultCallback(Vector3f rayFromWorld, Vector3f rayToWorld) {
           this.rayFromWorld.set(rayFromWorld);
           this.rayToWorld.set(rayToWorld);
       }
       
       @Override
       public float addSingleResult(LocalRayResult rayResult, boolean normalInWorldSpace) {
           // caller already does the filter on the closestHitFraction
           assert (rayResult.hitFraction <= closestHitFraction);
 
           closestHitFraction = rayResult.hitFraction;
           collisionObject = rayResult.collisionObject;
           if (normalInWorldSpace) {
               hitNormalWorld.set(rayResult.hitNormalLocal);
           }
           else {
               // need to transform normal into worldspace
               hitNormalWorld.set(rayResult.hitNormalLocal);
               collisionObject.getWorldTransform(Stack.alloc(Transform.class)).basis.transform(hitNormalWorld);
           }
 
           VectorUtil.setInterpolate3(hitPointWorld, rayFromWorld, rayToWorld, rayResult.hitFraction);
           return rayResult.hitFraction;
       }
   }
   
   public static class LocalConvexResult {
       public CollisionObject hitCollisionObject;
       public LocalShapeInfo localShapeInfo;
       public final Vector3f hitNormalLocal = new Vector3f();
       public final Vector3f hitPointLocal = new Vector3f();
       public float hitFraction;
 
       public LocalConvexResult(CollisionObject hitCollisionObject, LocalShapeInfo localShapeInfo, Vector3f hitNormalLocal, Vector3f hitPointLocal, float hitFraction) {
           this.hitCollisionObject = hitCollisionObject;
           this.localShapeInfo = localShapeInfo;
           this.hitNormalLocal.set(hitNormalLocal);
           this.hitPointLocal.set(hitPointLocal);
           this.hitFraction = hitFraction;
       }
   }
   
   public static abstract class ConvexResultCallback  implements java.io.Serializable {
       public float closestHitFraction = 1f;
       public short collisionFilterGroup = CollisionFilterGroups.DEFAULT_FILTER;
       public short collisionFilterMask = CollisionFilterGroups.ALL_FILTER;
       
       public boolean hasHit() {
           return (closestHitFraction < 1f);
       }
       
       public boolean needsCollision(BroadphaseProxy proxy0) {
           boolean collides = ((proxy0.collisionFilterGroup & collisionFilterMask) & 0xFFFF) != 0;
           collides = collides && ((collisionFilterGroup & proxy0.collisionFilterMask) & 0xFFFF) != 0;
           return collides;
       }
       
       public abstract float addSingleResult(LocalConvexResult convexResult, boolean normalInWorldSpace);
   }
   
   public static class ClosestConvexResultCallback extends ConvexResultCallback {
       public final Vector3f convexFromWorld = new Vector3f(); // used to calculate hitPointWorld from hitFraction
       public final Vector3f convexToWorld = new Vector3f();
       public final Vector3f hitNormalWorld = new Vector3f();
       public final Vector3f hitPointWorld = new Vector3f();
       public CollisionObject hitCollisionObject;
 
       public ClosestConvexResultCallback(Vector3f convexFromWorld, Vector3f convexToWorld) {
           this.convexFromWorld.set(convexFromWorld);
           this.convexToWorld.set(convexToWorld);
           this.hitCollisionObject = null;
       }
 
       @Override
       public float addSingleResult(LocalConvexResult convexResult, boolean normalInWorldSpace) {
           // caller already does the filter on the m_closestHitFraction
           assert (convexResult.hitFraction <= closestHitFraction);
 
           closestHitFraction = convexResult.hitFraction;
           hitCollisionObject = convexResult.hitCollisionObject;
           if (normalInWorldSpace) {
               hitNormalWorld.set(convexResult.hitNormalLocal);
               if (hitNormalWorld.length() > 2) {
                   System.out.println("CollisionWorld.addSingleResult world " + hitNormalWorld);
               }
           }
           else {
               // need to transform normal into worldspace
               hitNormalWorld.set(convexResult.hitNormalLocal);
               hitCollisionObject.getWorldTransform(Stack.alloc(Transform.class)).basis.transform(hitNormalWorld);
               if (hitNormalWorld.length() > 2) {
                   System.out.println("CollisionWorld.addSingleResult world " + hitNormalWorld);
               }
           }
 
           hitPointWorld.set(convexResult.hitPointLocal);
           return convexResult.hitFraction;
       }
   }
   
   private static class BridgeTriangleRaycastCallback extends TriangleRaycastCallback {
       public RayResultCallback resultCallback;
       public CollisionObject collisionObject;
       public ConcaveShape triangleMesh;
 
       public BridgeTriangleRaycastCallback(Vector3f from, Vector3f to, RayResultCallback resultCallback, CollisionObject collisionObject, ConcaveShape triangleMesh) {
           super(from, to);
           this.resultCallback = resultCallback;
           this.collisionObject = collisionObject;
           this.triangleMesh = triangleMesh;
       }
   
       public float reportHit(Vector3f hitNormalLocal, float hitFraction, int partId, int triangleIndex) {
           LocalShapeInfo shapeInfo = new LocalShapeInfo();
           shapeInfo.shapePart = partId;
           shapeInfo.triangleIndex = triangleIndex;
 
           LocalRayResult rayResult = new LocalRayResult(collisionObject, shapeInfo, hitNormalLocal, hitFraction);
 
           boolean normalInWorldSpace = false;
           return resultCallback.addSingleResult(rayResult, normalInWorldSpace);
       }
   }
   
}