-
Notifications
You must be signed in to change notification settings - Fork 512
/
physics.ts
522 lines (438 loc) · 15.9 KB
/
physics.ts
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
/*{# Copyright (c) 2010-2013 Turbulenz Limited #}*/
/*
* @title: 3D Physics
* @description:
* This sample shows how to create and use the Turbulenz physics device.
* The sample creates a floor plane and multiple cube shaped rigid bodies attached to nodes in the rendering scene.
* Click on the rendering window to move and rotate the camera around,
* click again to pick up and move the rigid bodies using the mouse forces object.
*/
/*{{ javascript("jslib/camera.js") }}*/
/*{{ javascript("jslib/floor.js") }}*/
/*{{ javascript("jslib/mouseforces.js") }}*/
/*{{ javascript("jslib/observer.js") }}*/
/*{{ javascript("jslib/utilities.js") }}*/
/*{{ javascript("jslib/requesthandler.js") }}*/
/*{{ javascript("jslib/services/turbulenzservices.js") }}*/
/*{{ javascript("jslib/services/turbulenzbridge.js") }}*/
/*{{ javascript("jslib/services/gamesession.js") }}*/
/*{{ javascript("jslib/services/mappingtable.js") }}*/
/*{{ javascript("scripts/htmlcontrols.js") }}*/
/*global TurbulenzEngine: true */
/*global RequestHandler: false */
/*global window: false */
/*global Camera: false */
/*global CameraController: false */
/*global Floor: false */
/*global RequestHandler: false */
/*global HTMLControls: false */
/*global MouseForces: false */
/*global TurbulenzServices: false */
TurbulenzEngine.onload = function onloadFn()
{
var errorCallback = function errorCallback(msg)
{
window.alert(msg);
};
var graphicsDeviceParameters = { };
var graphicsDevice = TurbulenzEngine.createGraphicsDevice(graphicsDeviceParameters);
if (!graphicsDevice.shadingLanguageVersion)
{
errorCallback("No shading language support detected.\nPlease check your graphics drivers are up to date.");
graphicsDevice = null;
return;
}
var mathDeviceParameters = {};
var mathDevice = TurbulenzEngine.createMathDevice(mathDeviceParameters);
var inputDeviceParameters = { };
var inputDevice = TurbulenzEngine.createInputDevice(inputDeviceParameters);
var assetsToLoad = 3;
// Setup camera & controller
var camera = Camera.create(mathDevice);
var cameraInitialize = function cameraInitializeFn()
{
var worldUp = mathDevice.v3BuildYAxis();
camera.lookAt(worldUp, worldUp, mathDevice.v3Build(0.0, 3.0, -15.0));
camera.updateViewMatrix();
};
cameraInitialize();
var cameraController = CameraController.create(graphicsDevice, inputDevice, camera);
var floor = Floor.create(graphicsDevice, mathDevice);
var requestHandlerParameters = {};
var requestHandler = RequestHandler.create(requestHandlerParameters);
var shader3d = null;
var technique3d = null;
var shader3dLoaded = function shader3dLoadedFn(shaderText)
{
if (shaderText)
{
var shaderParameters = JSON.parse(shaderText);
shader3d = graphicsDevice.createShader(shaderParameters);
if (shader3d)
{
technique3d = shader3d.getTechnique("textured3D");
assetsToLoad -= 1;
}
}
};
var sharedTechniqueParameters = graphicsDevice.createTechniqueParameters({
diffuse : null
});
/*jshint white: false*/
// Vertex buffer parameters for crate
var vertexbufferParameters =
{
numVertices: 24,
attributes: ['FLOAT3', 'SHORT2N'],
dynamic: false,
data: [
-0.5, -0.5, 0.5, 0, 0,
0.5, -0.5, 0.5, 1, 0,
0.5, 0.5, 0.5, 1, 1,
-0.5, 0.5, 0.5, 0, 1,
-0.5, 0.5, 0.5, 0, 0,
0.5, 0.5, 0.5, 1, 0,
0.5, 0.5, -0.5, 1, 1,
-0.5, 0.5, -0.5, 0, 1,
-0.5, 0.5, -0.5, 1, 1,
0.5, 0.5, -0.5, 0, 1,
0.5, -0.5, -0.5, 0, 0,
-0.5, -0.5, -0.5, 1, 0,
-0.5, -0.5, -0.5, 0, 0,
0.5, -0.5, -0.5, 1, 0,
0.5, -0.5, 0.5, 1, 1,
-0.5, -0.5, 0.5, 0, 1,
0.5, -0.5, 0.5, 0, 0,
0.5, -0.5, -0.5, 1, 0,
0.5, 0.5, -0.5, 1, 1,
0.5, 0.5, 0.5, 0, 1,
-0.5, -0.5, -0.5, 0, 0,
-0.5, -0.5, 0.5, 1, 0,
-0.5, 0.5, 0.5, 1, 1,
-0.5, 0.5, -0.5, 0, 1
]
};
/*jshint white: true*/
var vertexbuffer = graphicsDevice.createVertexBuffer(vertexbufferParameters);
var semantics = graphicsDevice.createSemantics([graphicsDevice.SEMANTIC_POSITION, graphicsDevice.SEMANTIC_TEXCOORD]);
/*jshint white: false*/
var indexbufferParameters =
{
numIndices: 36,
format: 'USHORT',
dynamic: false,
data: [
2, 0, 1,
3, 0, 2,
6, 4, 5,
7, 4, 6,
10, 8, 9,
11, 8, 10,
14, 12, 13,
15, 12, 14,
18, 16, 17,
19, 16, 18,
22, 20, 21,
23, 20, 22
]
};
/*jshint white: true*/
var indexbuffer = graphicsDevice.createIndexBuffer(indexbufferParameters);
var primitive = graphicsDevice.PRIMITIVE_TRIANGLES;
var numIndices = 36;
// Cache mathDevice functions
var m43MulM44 = mathDevice.m43MulM44;
var isVisibleBoxOrigin = mathDevice.isVisibleBoxOrigin;
var v4Build = mathDevice.v4Build;
var m43BuildTranslation = mathDevice.m43BuildTranslation;
var shader2d = null;
var technique2d = null;
var shader2dLoaded = function shader2dLoadedFn(shaderText)
{
if (shaderText)
{
var shaderParameters = JSON.parse(shaderText);
shader2d = graphicsDevice.createShader(shaderParameters);
if (shader2d)
{
technique2d = shader2d.getTechnique("constantColor2D");
assetsToLoad -= 1;
}
}
};
var techniqueParameters2d = graphicsDevice.createTechniqueParameters({
clipSpace : null,
constantColor : mathDevice.v4Build(0, 0, 0, 1)
});
var linePrim = graphicsDevice.PRIMITIVE_LINES;
var cursorFormat = [graphicsDevice.VERTEXFORMAT_FLOAT3];
var cursorSemantic = graphicsDevice.createSemantics([graphicsDevice.SEMANTIC_POSITION]);
var clearColor = mathDevice.v4Build(0.95, 0.95, 1.0, 1.0);
//
// Physics
//
var physicsDeviceParameters = { };
var physicsDevice = TurbulenzEngine.createPhysicsDevice(physicsDeviceParameters);
var dynamicsWorldParameters = { };
var dynamicsWorld = physicsDevice.createDynamicsWorld(dynamicsWorldParameters);
// Specify the generic settings for the collision objects
var collisionMargin = 0.005;
var mass = 20.0;
var numCubes = 200;
var cubeExtents = mathDevice.v3Build(0.5, 0.5, 0.5);
// Floor is represented by a plane
var floorShape = physicsDevice.createPlaneShape({
normal : mathDevice.v3Build(0, 1, 0),
distance : 0,
margin : collisionMargin
});
var floorObject = physicsDevice.createCollisionObject({
shape : floorShape,
transform : mathDevice.m43BuildIdentity(),
friction : 0.5,
restitution : 0.3,
group: physicsDevice.FILTER_STATIC,
mask: physicsDevice.FILTER_ALL
});
// Adds the floor collision object to the physicsDevice
dynamicsWorld.addCollisionObject(floorObject);
var boxShape = physicsDevice.createBoxShape({
halfExtents : cubeExtents,
margin : collisionMargin
});
var inertia = boxShape.inertia;
inertia = mathDevice.v3ScalarMul(inertia, mass);
var boxBodies = [];
// Initial box is created as a rigid body
boxBodies[0] = physicsDevice.createRigidBody({
shape : boxShape,
mass : mass,
inertia : inertia,
transform : mathDevice.m43BuildTranslation(0.0, 1.0, 0.0),
friction : 0.5,
restitution : 0.3,
angularDamping: 0.9,
frozen : false
});
dynamicsWorld.addRigidBody(boxBodies[0]);
for (var n = 1; n < numCubes; n += 1)
{
// Each additional box is cloned from the original
boxBodies[n] = boxBodies[0].clone();
boxBodies[n].transform = m43BuildTranslation.call(mathDevice, 0.0, 1.0 + 1.5 * n, 0.0);
dynamicsWorld.addRigidBody(boxBodies[n]);
}
var doReset = false;
var reset = function resetFn()
{
var transform = mathDevice.m43BuildIdentity();
var v3Zero = mathDevice.v3BuildZero();
for (n = 0; n < numCubes; n += 1)
{
var body = boxBodies[n];
body.transform = mathDevice.m43BuildTranslation(0.0, 1.0 + 1.5 * n, 0.0, transform);
body.linearVelocity = v3Zero;
body.angularVelocity = v3Zero;
body.active = true;
}
cameraInitialize();
};
// Controls
var htmlControls = HTMLControls.create();
htmlControls.addButtonControl({
id: "button01",
value: "Reset",
fn: function schedulResetFn()
{
doReset = true;
}
});
htmlControls.register();
var keyCodes = inputDevice.keyCodes;
var mouseCodes = inputDevice.mouseCodes;
var onKeyUp = function physicsOnkeyupFn(keynum)
{
// If the key R is released we reset the positions
if (keynum === keyCodes.R)
{
reset();
}
};
// Mouse forces
var dragMin = mathDevice.v3Build(-1000, 0, -1000);
var dragMax = mathDevice.v3Build(1000, 1000, 1000);
var mouseForces = MouseForces.create(graphicsDevice, inputDevice, mathDevice,
physicsDevice, dragMin, dragMax);
var onMouseDown = function (button)
{
if (mouseCodes.BUTTON_0 === button || mouseCodes.BUTTON_1 === button)
{
mouseForces.onmousedown();
}
};
var onMouseUp = function (button)
{
if (mouseCodes.BUTTON_0 === button || mouseCodes.BUTTON_1 === button)
{
mouseForces.onmouseup();
}
};
// Add event listeners
inputDevice.addEventListener("keyup", onKeyUp);
inputDevice.addEventListener("mousedown", onMouseDown);
inputDevice.addEventListener("mouseup", onMouseUp);
//
// Update
//
var update = function updateFn()
{
inputDevice.update();
if (doReset)
{
reset();
doReset = false;
}
if (mouseForces.pickedBody)
{
// If we're dragging a body don't apply the movement to the camera
cameraController.pitch = 0;
cameraController.turn = 0;
cameraController.step = 0;
}
cameraController.update();
var aspectRatio = (graphicsDevice.width / graphicsDevice.height);
if (aspectRatio !== camera.aspectRatio)
{
camera.aspectRatio = aspectRatio;
camera.updateProjectionMatrix();
}
camera.updateViewProjectionMatrix();
if (0 >= assetsToLoad)
{
mouseForces.update(dynamicsWorld, camera, 0.1);
dynamicsWorld.update();
}
var vp = camera.viewProjectionMatrix;
var transform = mathDevice.m43BuildIdentity();
var wvp;
if (graphicsDevice.beginFrame())
{
graphicsDevice.clear(clearColor, 1.0, 0);
floor.render(graphicsDevice, camera);
if (0 >= assetsToLoad)
{
graphicsDevice.setStream(vertexbuffer, semantics);
graphicsDevice.setIndexBuffer(indexbuffer);
graphicsDevice.setTechnique(technique3d);
graphicsDevice.setTechniqueParameters(sharedTechniqueParameters);
for (n = 0; n < numCubes; n += 1)
{
boxBodies[n].calculateTransform(transform);
wvp = m43MulM44.call(mathDevice, transform, vp, wvp);
if (isVisibleBoxOrigin.call(mathDevice, cubeExtents, wvp))
{
// Use directly the active technique when just a single property changes
technique3d.worldViewProjection = wvp;
graphicsDevice.drawIndexed(primitive, numIndices);
}
}
if (!mouseForces.pickedBody)
{
graphicsDevice.setTechnique(technique2d);
var screenWidth = graphicsDevice.width;
var screenHeight = graphicsDevice.height;
techniqueParameters2d['clipSpace'] =
v4Build.call(mathDevice, 2.0 / screenWidth, -2.0 / screenHeight, -1.0, 1.0);
graphicsDevice.setTechniqueParameters(techniqueParameters2d);
var writer = graphicsDevice.beginDraw(linePrim,
4,
cursorFormat,
cursorSemantic);
if (writer)
{
var halfWidth = screenWidth * 0.5;
var halfHeight = screenHeight * 0.5;
writer([halfWidth - 10, halfHeight]);
writer([halfWidth + 10, halfHeight]);
writer([halfWidth, halfHeight - 10]);
writer([halfWidth, halfHeight + 10]);
graphicsDevice.endDraw(writer);
}
}
}
graphicsDevice.endFrame();
}
};
var intervalID = TurbulenzEngine.setInterval(update, 1000 / 60);
var mappingTableReceived = function mappingTableReceivedFn(mappingTable)
{
var textureParameters =
{
src : mappingTable.getURL("textures/crate.jpg"),
mipmaps : true,
onload : function (texture)
{
sharedTechniqueParameters['diffuse'] = texture;
assetsToLoad -= 1;
}
};
graphicsDevice.createTexture(textureParameters);
requestHandler.request({
src: mappingTable.getURL("shaders/generic3D.cgfx"),
onload: shader3dLoaded
});
requestHandler.request({
src: mappingTable.getURL("shaders/generic2D.cgfx"),
onload: shader2dLoaded
});
};
var gameSessionCreated = function gameSessionCreatedFn(gameSession)
{
TurbulenzServices.createMappingTable(requestHandler,
gameSession,
mappingTableReceived);
};
var gameSession = TurbulenzServices.createGameSession(requestHandler, gameSessionCreated);
TurbulenzEngine.onunload = function destroyScene()
{
TurbulenzEngine.clearInterval(intervalID);
if (gameSession)
{
gameSession.destroy();
gameSession = null;
}
requestHandler = null;
indexbuffer = null;
vertexbuffer = null;
semantics = null;
cursorSemantic = null;
primitive = null;
linePrim = null;
technique3d = null;
shader3d = null;
technique2d = null;
shader2d = null;
techniqueParameters2d = null;
sharedTechniqueParameters = null;
mouseForces = null;
floor = null;
cameraController = null;
camera = null;
boxBodies = null;
inertia = null;
clearColor = null;
m43MulM44 = null;
m43BuildTranslation = null;
isVisibleBoxOrigin = null;
v4Build = null;
dragMax = null;
dragMin = null;
mouseCodes = null;
keyCodes = null;
TurbulenzEngine.flush();
physicsDevice = null;
inputDevice = null;
graphicsDevice = null;
mathDevice = null;
};
};