Normand Briere
2018-05-22 42107f9a01652cb2f47228d20c1148a2a22f6a63
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
/*
 * 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.collision.broadphase.CollisionAlgorithm;
import com.bulletphysics.collision.broadphase.CollisionAlgorithmConstructionInfo;
import com.bulletphysics.collision.broadphase.DispatcherInfo;
import com.bulletphysics.collision.narrowphase.PersistentManifold;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.util.ObjectArrayList;
import com.bulletphysics.util.ObjectPool;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
 
/**
 * Provides collision detection between two spheres.
 * 
 * @author jezek2
 */
public class SphereSphereCollisionAlgorithm extends CollisionAlgorithm {
   
   private boolean ownManifold;
   private PersistentManifold manifoldPtr;
   
   public void init(PersistentManifold mf, CollisionAlgorithmConstructionInfo ci, CollisionObject col0, CollisionObject col1) {
       super.init(ci);
       manifoldPtr = mf;
 
       if (manifoldPtr == null) {
           manifoldPtr = dispatcher.getNewManifold(col0, col1);
           ownManifold = true;
       }
   }
 
   @Override
   public void init(CollisionAlgorithmConstructionInfo ci) {
       super.init(ci);
   }
   
   @Override
   public void destroy() {
       if (ownManifold) {
           if (manifoldPtr != null) {
               dispatcher.releaseManifold(manifoldPtr);
           }
           manifoldPtr = null;
       }
   }
   
   @Override
   public void processCollision(CollisionObject col0, CollisionObject col1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
       if (manifoldPtr == null) {
           return;
       }
       
       Transform tmpTrans1 = Stack.alloc(Transform.class);
       Transform tmpTrans2 = Stack.alloc(Transform.class);
 
       resultOut.setPersistentManifold(manifoldPtr);
 
       SphereShape sphere0 = (SphereShape) col0.getCollisionShape();
       SphereShape sphere1 = (SphereShape) col1.getCollisionShape();
 
       Vector3f diff = Stack.alloc(Vector3f.class);
       diff.sub(col0.getWorldTransform(tmpTrans1).origin, col1.getWorldTransform(tmpTrans2).origin);
 
       float len = diff.length();
       float radius0 = sphere0.getRadius();
       float radius1 = sphere1.getRadius();
 
       //#ifdef CLEAR_MANIFOLD
       //manifoldPtr.clearManifold(); // don't do this, it disables warmstarting
       //#endif
 
       // if distance positive, don't generate a new contact
       if (len > (radius0 + radius1)) {
           //#ifndef CLEAR_MANIFOLD
           resultOut.refreshContactPoints();
           //#endif //CLEAR_MANIFOLD
           return;
       }
       // distance (negative means penetration)
       float dist = len - (radius0 + radius1);
 
       Vector3f normalOnSurfaceB = Stack.alloc(Vector3f.class);
       normalOnSurfaceB.set(1f, 0f, 0f);
       if (len > BulletGlobals.FLT_EPSILON) {
           normalOnSurfaceB.scale(1f / len, diff);
       }
 
       Vector3f tmp = Stack.alloc(Vector3f.class);
 
       // point on A (worldspace)
       Vector3f pos0 = Stack.alloc(Vector3f.class);
       tmp.scale(radius0, normalOnSurfaceB);
       pos0.sub(col0.getWorldTransform(tmpTrans1).origin, tmp);
 
       // point on B (worldspace)
       Vector3f pos1 = Stack.alloc(Vector3f.class);
       tmp.scale(radius1, normalOnSurfaceB);
       pos1.add(col1.getWorldTransform(tmpTrans2).origin, tmp);
 
       // report a contact. internally this will be kept persistent, and contact reduction is done
       resultOut.addContactPoint(normalOnSurfaceB, pos1, dist);
 
       //#ifndef CLEAR_MANIFOLD
       resultOut.refreshContactPoints();
       //#endif //CLEAR_MANIFOLD
   }
 
   @Override
   public float calculateTimeOfImpact(CollisionObject body0, CollisionObject body1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
       return 1f;
   }
 
   @Override
   public void getAllContactManifolds(ObjectArrayList<PersistentManifold> manifoldArray) {
       if (manifoldPtr != null && ownManifold) {
           manifoldArray.add(manifoldPtr);
       }
   }
   
   ////////////////////////////////////////////////////////////////////////////
 
   public static class CreateFunc extends CollisionAlgorithmCreateFunc {
       private final ObjectPool<SphereSphereCollisionAlgorithm> pool = ObjectPool.get(SphereSphereCollisionAlgorithm.class);
 
       @Override
       public CollisionAlgorithm createCollisionAlgorithm(CollisionAlgorithmConstructionInfo ci, CollisionObject body0, CollisionObject body1) {
           SphereSphereCollisionAlgorithm algo = pool.get();
           algo.init(null, ci, body0, body1);
           return algo;
       }
 
       @Override
       public void releaseCollisionAlgorithm(CollisionAlgorithm algo) {
           pool.release((SphereSphereCollisionAlgorithm)algo);
       }
   };
   
}