-
Notifications
You must be signed in to change notification settings - Fork 6
/
jiglibjs2_vehicle_physics.html
471 lines (362 loc) · 17.3 KB
/
jiglibjs2_vehicle_physics.html
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>JigLibJS2 Javascript Vehicle Physics</title>
<meta charset="utf-8">
<style type="text/css">
body {
background:#fff;
padding:0;
margin:0;
font-weight: bold;
overflow:hidden;
}
</style>
<script type="text/javascript" src="three.min.js"></script>
<script type="text/javascript" src="RequestAnimationFrame.js"></script>
<script type="text/javascript" src="./JigLib.js"></script>
<script type="text/javascript" src="./geom/glMatrix.js"></script>
<script type="text/javascript" src="./geom/Vector3D.js"></script>
<script type="text/javascript" src="./geom/Matrix3D.js"></script>
<script type="text/javascript" src="./cof/JConfig.js"></script>
<script type="text/javascript" src="./vehicles/JCar.js"></script>
<script type="text/javascript" src="./vehicles/JWheel.js"></script>
<script type="text/javascript" src="./physics/RigidBody.js"></script>
<script type="text/javascript" src="./geometry/JBox.js"></script>
<script type="text/javascript" src="./vehicles/JChassis.js"></script>
<script type="text/javascript" src="./collision/CollDetectFunctor.js"></script>
<script type="text/javascript" src="./collision/CollDetectBoxPlane.js"></script>
<script type="text/javascript" src="./collision/CollDetectBoxMesh.js"></script>
<script type="text/javascript" src="./collision/CollDetectBoxBox.js"></script>
<script type="text/javascript" src="./collision/CollDetectSphereTerrain.js"></script>
<script type="text/javascript" src="./collision/CollDetectSphereBox.js"></script>
<script type="text/javascript" src="./collision/CollDetectCapsuleTerrain.js"></script>
<script type="text/javascript" src="./collision/CollDetectSphereCapsule.js"></script>
<script type="text/javascript" src="./collision/CollisionSystemAbstract.js"></script>
<script type="text/javascript" src="./collision/CollisionSystemBrute.js"></script>
<script type="text/javascript" src="./collision/CollDetectCapsuleBox.js"></script>
<script type="text/javascript" src="./collision/CollDetectSphereMesh.js"></script>
<script type="text/javascript" src="./collision/CollDetectBoxTerrain.js"></script>
<script type="text/javascript" src="./collision/CollisionSystemGrid.js"></script>
<script type="text/javascript" src="./collision/CollDetectCapsuleCapsule.js"></script>
<script type="text/javascript" src="./collision/CollPointInfo.js"></script>
<script type="text/javascript" src="./collision/CollisionInfo.js"></script>
<script type="text/javascript" src="./collision/CollDetectCapsulePlane.js"></script>
<script type="text/javascript" src="./collision/CollDetectInfo.js"></script>
<script type="text/javascript" src="./collision/CollDetectSphereSphere.js"></script>
<script type="text/javascript" src="./collision/CollisionSystemGridEntry.js"></script>
<script type="text/javascript" src="./collision/CollDetectSpherePlane.js"></script>
<script type="text/javascript" src="./math/JMatrix3D.js"></script>
<script type="text/javascript" src="./math/JMath3D.js"></script>
<script type="text/javascript" src="./math/JNumber3D.js"></script>
<script type="text/javascript" src="./geometry/JIndexedTriangle.js"></script>
<script type="text/javascript" src="./geometry/JOctree.js"></script>
<script type="text/javascript" src="./geometry/JCapsule.js"></script>
<script type="text/javascript" src="./geometry/JRay.js"></script>
<script type="text/javascript" src="./geometry/JAABox.js"></script>
<script type="text/javascript" src="./geometry/JTerrain.js"></script>
<script type="text/javascript" src="./geometry/JPlane.js"></script>
<script type="text/javascript" src="./geometry/JTriangleMesh.js"></script>
<script type="text/javascript" src="./geometry/JTriangle.js"></script>
<script type="text/javascript" src="./geometry/JSphere.js"></script>
<script type="text/javascript" src="./geometry/JSegment.js"></script>
<script type="text/javascript" src="./data/CollOutData.js"></script>
<script type="text/javascript" src="./data/ContactData.js"></script>
<script type="text/javascript" src="./data/PlaneData.js"></script>
<script type="text/javascript" src="./data/EdgeData.js"></script>
<script type="text/javascript" src="./data/TerrainData.js"></script>
<script type="text/javascript" src="./data/OctreeCell.js"></script>
<script type="text/javascript" src="./data/CollOutBodyData.js"></script>
<script type="text/javascript" src="./data/TriangleVertexIndices.js"></script>
<script type="text/javascript" src="./data/SpanData.js"></script>
<script type="text/javascript" src="./physics/constraint/JConstraint.js"></script>
<script type="text/javascript" src="./physics/constraint/JConstraintWorldPoint.js"></script>
<script type="text/javascript" src="./physics/constraint/JConstraintMaxDistance.js"></script>
<script type="text/javascript" src="./physics/constraint/JConstraintPoint.js"></script>
<script type="text/javascript" src="./physics/MaterialProperties.js"></script>
<script type="text/javascript" src="./physics/PhysicsController.js"></script>
<script type="text/javascript" src="./physics/CachedImpulse.js"></script>
<script type="text/javascript" src="./physics/PhysicsState.js"></script>
<script type="text/javascript" src="./physics/HingeJoint.js"></script>
<script type="text/javascript" src="./physics/BodyPair.js"></script>
<script type="text/javascript" src="./physics/PhysicsSystem.js"></script>
<script type="text/javascript" src="./debug/Stats.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22245928-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<script type="text/javascript">
var container, stats;
var camera, scene, renderer;
var projector;
var material;
var geometry, group;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var t0 = new Date().getTime();
var system;
var JChassis;
var JWheel;
var Vector3D = JigLib.Vector3D;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
init_jiglib();
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
projector = new THREE.Projector();
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 5;
camera.position.x = 10;
camera.position.y = 10;
scene = new THREE.Scene();
material = new THREE.MeshNormalMaterial();
//var ground = new THREE.Mesh( new Cube( 40, 0, 40 ), material );
//ground.position.x = 0;
//ground.position.y = -10;
//ground.position.z = 0;
//scene.addObject( ground );
for ( var i = 0; i < 30; i ++ ) {
if (Math.random() > .5) {
var width = Math.random() * 4;
var height = Math.random() * 4;
var depth = Math.random() * 4;
var geometry = new THREE.CubeGeometry( width, height, depth );
var mesh = new THREE.Mesh( geometry, material );
mesh.matrixAutoUpdate = false;
mesh.overdraw = true;
mesh.position.x = Math.random() * 10 - 5;
mesh.position.y = 6 + 6 * i;
mesh.position.z = Math.random() * 10 - 5;
scene.add( mesh );
var cube = new JigLib.JBox( null, width, depth, height );
cube.set_mass( width * height * depth );
cube.moveTo( new Vector3D( mesh.position.x, mesh.position.y, mesh.position.z, 0 ) );
system.addBody( cube );
mesh.rigidBody = cube;
} else {
var radius = Math.random() * 4 / 2;
var geometry = new THREE.SphereGeometry( radius, 5, 5 );
var mesh = new THREE.Mesh( geometry, material );
mesh.matrixAutoUpdate = false;
mesh.overdraw = true;
mesh.position.x = Math.random() * 10 - 5;
mesh.position.y = 6 + 6 * i;
mesh.position.z = Math.random() * 10 - 5;
scene.add( mesh );
var sphere = new JigLib.JSphere( null, radius );
sphere.set_mass( 4 / 3 * Math.PI * Math.pow( radius, 3 ) );
sphere.moveTo( new Vector3D( mesh.position.x, mesh.position.y, mesh.position.z, 0 ) );
system.addBody( sphere );
mesh.rigidBody = sphere;
}
}
var width = 4;
var height = 1;
var depth = 8;
var mesh = new THREE.Mesh( new THREE.CubeGeometry( width, height, depth ), material );
mesh.matrixAutoUpdate = false;
mesh.overdraw = true;
mesh.position.x = 0;
mesh.position.y = 0;
mesh.position.z = 0;
var numSegs = 10;
var topRad = 1;
var botRad = 1;
var height = 1;
var topOffset = 0;
var botOffset = 0;
mesh.wheelFL = new THREE.Object3D();
mesh.wheelFR = new THREE.Object3D();
mesh.wheelBL = new THREE.Object3D();
mesh.wheelBR = new THREE.Object3D();
mesh.wheelFL.position.x = -2.2; mesh.wheelFL.position.y = -0.5; mesh.wheelFL.position.z = 4;
mesh.wheelFR.position.x = 2.2; mesh.wheelFR.position.y = -0.5; mesh.wheelFR.position.z = 4;
mesh.wheelBL.position.x = -2.2; mesh.wheelBL.position.y = -0.5; mesh.wheelBL.position.z = -4;
mesh.wheelBR.position.x = 2.2; mesh.wheelBR.position.y = -0.5; mesh.wheelBR.position.z = -4;
mesh.wheelFL.add(new THREE.Mesh( new THREE.CylinderGeometry( topRad, botRad, height ), material ));
mesh.wheelFR.add(new THREE.Mesh( new THREE.CylinderGeometry( topRad, botRad, height ), material ));
mesh.wheelBL.add(new THREE.Mesh( new THREE.CylinderGeometry( topRad, botRad, height ), material ));
mesh.wheelBR.add(new THREE.Mesh( new THREE.CylinderGeometry( topRad, botRad, height ), material ));
mesh.wheelFL.children[0].rotation.z = degToRad(90);
mesh.wheelFR.children[0].rotation.z = degToRad(90);
mesh.wheelBL.children[0].rotation.z = degToRad(90);
mesh.wheelBR.children[0].rotation.z = degToRad(90);
mesh.add( mesh.wheelFL );
mesh.add( mesh.wheelFR );
mesh.add( mesh.wheelBL );
mesh.add( mesh.wheelBR );
scene.add( mesh );
var carBody = new JigLib.JCar(null);
var maxSteerAngle = 50;
var steerRate = 3;
var driveTorque = 300;
carBody.setCar(maxSteerAngle, steerRate, driveTorque);
carBody.get_chassis().set_sideLengths(new Vector3D(width, height, depth, 0));
carBody.get_chassis().moveTo(new Vector3D(0, -0.8, -20));
carBody.get_chassis().set_mass(20);
system.addBody(carBody.get_chassis());
mesh.carBody = carBody;
mesh.rigidBody = carBody.get_chassis();
var wheelRadius = 1;
var travel = .5;
var sideFriction = 1.6; // 1.2;
var fwdFriction = 1.6; // 1.2;
var restingFrac = 0.3; // 0.6; // 0.5;
var dampingFrac = 0.3; // 0.6;
var rays = 2;
carBody.setupWheel(0, new Vector3D(-2.2, -1, 4), sideFriction, fwdFriction, travel, wheelRadius, restingFrac, dampingFrac, rays, false);
carBody.setupWheel(1, new Vector3D( 2.2, -1, 4), sideFriction, fwdFriction, travel, wheelRadius, restingFrac, dampingFrac, rays, false);
carBody.setupWheel(2, new Vector3D(-2.2, -1, -4), sideFriction, fwdFriction, travel, wheelRadius, restingFrac, dampingFrac, rays, true);
carBody.setupWheel(3, new Vector3D( 2.2, -1, -4), sideFriction, fwdFriction, travel, wheelRadius, restingFrac, dampingFrac, rays, true);
carBody.wheelFL = carBody.get_wheels()[0];
carBody.wheelFR = carBody.get_wheels()[1];
carBody.wheelBL = carBody.get_wheels()[2];
carBody.wheelBR = carBody.get_wheels()[3];
document.onkeydown = function( e ){
switch(e.keyCode)
{
case 32:
carBody.setHBrake(1);
return false;
case 38:
carBody.setAccelerate(1);
return false;
case 40:
carBody.setAccelerate(-1);
return false;
case 37:
carBody.setSteer([0, 1], 1);
return false;
case 39:
carBody.setSteer([0, 1], -1);
return false;
}
}
document.onkeyup = function( e ){
switch(e.keyCode)
{
case 32:
carBody.setHBrake(0);
return false;
case 38:
carBody.setAccelerate(0);
return false;
case 40:
carBody.setAccelerate(0);
return false;
case 37:
carBody.setSteer([0, 1], 0);
return false;
case 39:
carBody.setSteer([0, 1], 0);
return false;
}
}
// renderer = new THREE.WebGLRenderer();
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.sortObjects = false;
container.appendChild( renderer.domElement );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
}
function init_jiglib() {
system = JigLib.PhysicsSystem.getInstance();
system.setCollisionSystem(); // CollisionSystemBrute
// system.setCollisionSystem(true); // CollisionSystemGrid
// system.setSolverType("FAST");
// system.setSolverType("NORMAL");
system.setSolverType("ACCUMULATED");
system.setGravity( new Vector3D( 0, -9.8, 0, 0 ) );
var ground = new JigLib.JPlane();
ground.set_y(-10);
ground.set_rotationX(90);
ground.set_movable(false);
system.addBody( ground );
}
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX ) * .1;
mouseY = ( event.clientY - windowHalfY ) * .1;
}
function animate() {
requestAnimationFrame( animate );
updateDynamicsWorld();
render();
}
function degToRad(deg) {
return (deg * 3.14159) / 180;
}
function radToDeg(rad) {
return rad * (180 / 3.14159);
}
function updateDynamicsWorld() {
var t1 = new Date().getTime();
var ms = t1 - t0;
t0 = t1;
system.integrate( ms / 1000 );
for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
var mesh = scene.children[i];
if (mesh.rigidBody) {
var pos = mesh.rigidBody.get_currentState().position;
var dir = mesh.rigidBody.get_currentState().orientation.get_rawData();
var matrix = new THREE.Matrix4();
matrix.makeTranslation( pos.x, pos.y, pos.z );
var rotate = new THREE.Matrix4(dir[0], dir[1], dir[2], dir[3], dir[4], dir[5], dir[6], dir[7], dir[8], dir[9], dir[10], dir[11], dir[12], dir[13], dir[14], dir[15]);
matrix.multiplySelf(rotate);
mesh.matrix = matrix;
mesh.updateMatrixWorld(true);
}
if (mesh.carBody) {
mesh.wheelFL.position.y = mesh.carBody.wheelFL.getActualPos().y;
mesh.wheelFR.position.y = mesh.carBody.wheelFR.getActualPos().y;
mesh.wheelBL.position.y = mesh.carBody.wheelBL.getActualPos().y;
mesh.wheelBR.position.y = mesh.carBody.wheelBR.getActualPos().y;
mesh.wheelFL.rotation.y = degToRad(mesh.carBody.wheelFL.getSteerAngle());
mesh.wheelFR.rotation.y = degToRad(mesh.carBody.wheelFR.getSteerAngle());
mesh.wheelBL.rotation.y = degToRad(mesh.carBody.wheelBL.getSteerAngle());
mesh.wheelBR.rotation.y = degToRad(mesh.carBody.wheelBR.getSteerAngle());
mesh.wheelFL.children[0].rotation.x = degToRad(mesh.carBody.wheelFL.getAxisAngle());
mesh.wheelFR.children[0].rotation.x = degToRad(mesh.carBody.wheelFR.getAxisAngle());
mesh.wheelBL.children[0].rotation.x = degToRad(mesh.carBody.wheelBL.getAxisAngle());
mesh.wheelBR.children[0].rotation.x = degToRad(mesh.carBody.wheelBR.getAxisAngle());
var dir = mesh.carBody.get_chassis().get_currentState().orientation.get_rawData();
var rotate = new THREE.Matrix4(dir[0], dir[1], dir[2], dir[3], dir[4], dir[5], dir[6], dir[7], dir[8], dir[9], dir[10], dir[11], dir[12], dir[13], dir[14], dir[15]);
mesh.updateMatrixWorld(true);
camera.lookAt(mesh.position);
}
}
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
renderer.render(scene, camera);
}
function onDocumentMouseDown( event ) {
event.preventDefault();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObject(scene);
console.log(intersects);
if ( intersects.length > 0 ) {
var object = intersects[ 0 ].object;
if ( object.rigidBody ) {
object.rigidBody.setLineVelocity( new Vector3D( 0, 20, 0 ), false );
}
}
}
</script>
</body>
</html>