Normand Briere
2019-09-17 d248db5fc21c4b0ab24968974e5e590f413ef8fc
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
import com.bulletphysics.BulletGlobals;
import com.bulletphysics.BulletStats;
import com.bulletphysics.collision.dispatch.CollisionObject;
 
import com.bulletphysics.util.ObjectArrayList;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.BroadphaseNativeType;
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.collision.shapes.StaticPlaneShape;
import com.bulletphysics.collision.shapes.CylinderShape;
import com.bulletphysics.collision.shapes.ConvexShape;
import com.bulletphysics.collision.shapes.ShapeHull;
import com.bulletphysics.collision.shapes.PolyhedralConvexShape;
import com.bulletphysics.collision.shapes.ConcaveShape;
import com.bulletphysics.collision.shapes.TriangleCallback;
import com.bulletphysics.collision.shapes.InternalTriangleIndexCallback;
//import com.bulletphysics.demos.opengl.DemoApplication;
//import com.bulletphysics.demos.opengl.GLDebugDrawer;
//import com.bulletphysics.demos.opengl.IGL;
//import com.bulletphysics.demos.opengl.LWJGL;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.constraintsolver.ConstraintSolver;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.DebugDrawModes;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.linearmath.TransformUtil;
import com.bulletphysics.linearmath.VectorUtil;
import com.bulletphysics.linearmath.Clock;
import com.bulletphysics.util.ObjectPool;
import com.bulletphysics.util.IntArrayList;
import javax.vecmath.Vector3f;
import javax.vecmath.Color3f;
//import org.lwjgl.LWJGLException;
//import static com.bulletphysics.demos.opengl.IGL.*;
 
import com.bulletphysics.linearmath.MiscUtil;
//import com.bulletphysics.dynamics.constraintsolver.TypedConstraint;
import com.bulletphysics.dynamics.constraintsolver.Generic6DofConstraint;
 
import java.util.Map;
import java.util.HashMap;
 
/**
 *
 * @author nbriere
 */
public class PhysicsNode extends Composite // Object3D
{
    static final long serialVersionUID = 7284096213977007708L;
    
transient
        DynamicsWorld transientDynamicsWorld = null;
    
        float gravity;
        float floor;
        float wind;
        float friction; // global plane
        
    PhysicsNode()
    {
        super("Physics");
        
        live = true;
    }
    
    void Reset()
    {
        initPhysics();
    }
    
    DynamicsWorld GetDynamics()
    {
        if (transientDynamicsWorld == null)
            initPhysics();
        
        return transientDynamicsWorld;
    }
    
    static protected Clock clock = new Clock();
 
    static public float getDeltaTimeMicroseconds()
    {
        //#ifdef USE_BT_CLOCK
        float dt = clock.getTimeMicroseconds();
        clock.reset();
        return dt;
    //#else
    //return btScalar(16666.);
    //#endif
    }
    
    float totalms = 0;
    
    void drawSelf(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
    {
        if (Globals.isLIVE() && live && display.DrawMode() == display.SHADOW) // FUCK
        {
            float ms = getDeltaTimeMicroseconds();
            float minFPS = 1000000f / 60f;
            if (ms > minFPS)
            {
                ms = minFPS;
            }
 
            if (CameraPane.AntialiasingEnabled())
                ms /= CameraPane.ACSIZE;
        
            totalms += ms;
 
            if (totalms > 2000000)
            {
            //    spawnRagdoll(true);
                totalms = 0;
            }
 
//            assert (dynamicsWorld != null);
//            
//            if (transientDynamicsWorld == null)
////                staticdynamicsWorld = dynamicsWorld;
//                initPhysics();
//            
//            assert(dynamicsWorld == staticdynamicsWorld);
            
            GetDynamics().stepSimulation(ms / 1000000.f);
        }
        
        super.drawSelf(display, root, selected, blocked);
    }
    
    void Step()
    {
        float ms = getDeltaTimeMicroseconds();
        
        float minFPS = 1000000f / 60f;
        if (ms > minFPS)
        {
            ms = minFPS;
        }
 
//        if (transientDynamicsWorld == null)
//            initPhysics();
            
        GetDynamics().stepSimulation(ms / 1000000.f);
    }
    
    public void initPhysics()
    {
        //if (dynamicsWorld != null) // && staticdynamicsWorld == null)
        //    staticdynamicsWorld = dynamicsWorld;
        
        // Setup the basic world
        //if (staticdynamicsWorld == null)
        //if (transientDynamicsWorld == null) // && ragdolls.size() == 0)
        {
            DefaultCollisionConfiguration collision_config = new DefaultCollisionConfiguration();
 
            CollisionDispatcher dispatcher = new CollisionDispatcher(collision_config);
 
            Vector3f worldAabbMin = new Vector3f(-10000, -10000, -10000);
            Vector3f worldAabbMax = new Vector3f(10000, 10000, 10000);
            //BroadphaseInterface overlappingPairCache = new AxisSweep3(worldAabbMin, worldAabbMax);
            //BroadphaseInterface overlappingPairCache = new SimpleBroadphase();
            BroadphaseInterface overlappingPairCache = new DbvtBroadphase();
 
            //#ifdef USE_ODE_QUICKSTEP
            //btConstraintSolver* constraintSolver = new OdeConstraintSolver();
            //#else
            ConstraintSolver constraintSolver = new SequentialImpulseConstraintSolver();
            //#endif
 
            if (transientDynamicsWorld != null)
                ResetDynamics(transientDynamicsWorld);
 
            transientDynamicsWorld = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, constraintSolver, collision_config);
 
            //staticdynamicsWorld = dynamicsWorld;
//        dynamicsWorld.setDebugDrawer(new GLDebugDrawer(gl));
 
        // Setup a big ground box
//        {
            CollisionShape groundShape = new BoxShape(new Vector3f(200f, 10f, 200f));
            Transform groundTransform = new Transform();
            groundTransform.setIdentity();
            groundTransform.origin.set(0f, -floor - 10, 0f);
            localCreateRigidBody(0f, groundTransform, groundShape);
            
            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
            groundTransform = new Transform();
            groundTransform.setIdentity();
            groundTransform.origin.set(0f, 100, -201f);
            //localCreateRigidBody(0f, groundTransform, groundShape);
            
            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
            groundTransform = new Transform();
            groundTransform.setIdentity();
            groundTransform.origin.set(-201f, 100, 0f);
            //localCreateRigidBody(0f, groundTransform, groundShape);
            
            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
            groundTransform = new Transform();
            groundTransform.setIdentity();
            groundTransform.origin.set(201f, 100, 0f);
            //localCreateRigidBody(0f, groundTransform, groundShape);
            
            groundShape = new BoxShape(new Vector3f(200f, 200f, 200f));
            groundTransform = new Transform();
            groundTransform.setIdentity();
            groundTransform.origin.set(0f, 100, 201f);
            //localCreateRigidBody(0f, groundTransform, groundShape);
            
            transientDynamicsWorld.setGravity(new Vector3f(0, -gravity, wind));
            
            SetDynamics(transientDynamicsWorld);
        }
//        else
        {
//            if (transientDynamicsWorld == null)
//            {
//                staticdynamicsWorld = ragdolls.get(0).ownerWorld;
//                clientResetScene();
//            }
//            else
//            {
//                if (ragdolls.size() > 0)
//                    assert(staticdynamicsWorld == ragdolls.get(0).ownerWorld);
//            }
        }
 
        //dynamicsWorld = staticdynamicsWorld;
            
        // Spawn one ragdoll
//      spawnRagdoll();
//
//        myoffset = dynamicsWorld.getNumCollisionObjects() - 1;
//        
//        if (myoffset == 0)
            clientResetScene();
    }
 
    public RigidBody localCreateRigidBody(float mass, Transform startTransform, CollisionShape shape)
    {
        // rigidbody is dynamic if and only if mass is non zero, otherwise static
        boolean isDynamic = (mass != 0f);
 
        Vector3f localInertia = new Vector3f(0f, 0f, 0f);
        if (isDynamic)
        {
            shape.calculateLocalInertia(mass, localInertia);
        }
 
        // using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
 
        //#define USE_MOTIONSTATE 1
        //#ifdef USE_MOTIONSTATE
        DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
 
        float friction = 1;
        
        RigidBodyConstructionInfo cInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia, friction);
 
        RigidBody body = new RigidBody(cInfo);
        //#else
        //btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);    
        //body->setWorldTransform(startTransform);
        //#endif//
 
        transientDynamicsWorld.addRigidBody(body);
 
        return body;
    }
 
    public void clientResetScene()
    {
//        System.exit(0);
        //#ifdef SHOW_NUM_DEEP_PENETRATIONS
        BulletStats.gNumDeepPenetrationChecks = 0;
        BulletStats.gNumGjkChecks = 0;
        //#endif //SHOW_NUM_DEEP_PENETRATIONS
 
        int numObjects = 0;
        if (transientDynamicsWorld != null)
        {
            transientDynamicsWorld.stepSimulation(1f / 60f, 0);
            numObjects = transientDynamicsWorld.getNumCollisionObjects();
        }
 
        for (int i = 0; i < numObjects; i++)
        {
//            if (i > 0)
//            System.exit(0);
            CollisionObject colObj = transientDynamicsWorld.getCollisionObjectArray().getQuick(i);
            RigidBody body = RigidBody.upcast(colObj);
            if (body != null)
            {
                DefaultMotionState myMotionState = (DefaultMotionState) body.getMotionState();
                if (//false)
                        myMotionState != null)
                {
                    myMotionState.graphicsWorldTrans.set(myMotionState.startWorldTrans);
                    colObj.setWorldTransform(myMotionState.graphicsWorldTrans);
                    colObj.setInterpolationWorldTransform(myMotionState.startWorldTrans);
                    colObj.activate();
                }
                // removed cached contact points
                transientDynamicsWorld.getBroadphase().getOverlappingPairCache().cleanProxyFromPairs(colObj.getBroadphaseHandle(), transientDynamicsWorld.getDispatcher());
 
                // ??? body = RigidBody.upcast(colObj);
                if (body != null && !body.isStaticObject())
                {
                    RigidBody.upcast(colObj).setLinearVelocity(new Vector3f(0f, 0f, 0f));
                    RigidBody.upcast(colObj).setAngularVelocity(new Vector3f(0f, 0f, 0f));
                }
                
//                if (body.getUserPointer() != null)
//                    ((RagDoll)body.getUserPointer()).init(transientDynamicsWorld, new Vector3f(0f, 0f, 0f), 0.45f, true);
                
            }
 
        /*
        //quickly search some issue at a certain simulation frame, pressing space to reset
        int fixed=18;
        for (int i=0;i<fixed;i++)
        {
        getDynamicsWorld()->stepSimulation(1./60.f,1);
        }
         */
        }
    }
 
    void createEditWindow(GroupEditor callee, boolean newWindow)
    {
        //editWindow = (new SphereEditor(this, deepCopy(), callee)).GetEditor();
        if (newWindow)
        {
            objectUI = new PhysicsEditor(this, deepCopy(), callee);
        } else
        {
            objectUI = new PhysicsEditor(this, callee);
        }
        editWindow = objectUI.GetEditor();
    }
 
}