-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.js
488 lines (308 loc) · 12 KB
/
Car.js
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
/**
* @author alteredq / http://alteredqualia.com/
* @author Tapani Jamsa
*/
THREE.Car = function() {
var scope = this;
this.lerp = function(a, b, t) {
return a + (b - a) * t;
};
// car geometry manual parameters
this.modelScale = 100;
this.backWheelOffset = 2;
this.autoWheelGeometry = true;
// car geometry parameters automatically set from wheel mesh
// - assumes wheel mesh is front left wheel in proper global
// position with respect to body mesh
// - other wheels are mirrored against car root
// - if necessary back wheels can be offset manually
this.wheelOffsetX = 0;
this.wheelOffsetY = 0;
this.wheelOffsetZ = 0;
this.wheelDiameter = 1;
// car "feel" parameters
// DEFAULT VALUES
this.MAX_SPEED = 2200; // 2200
this.MAX_REVERSE_SPEED = -1500;
this.MAX_WHEEL_ROTATION = 0.6;
this.FRONT_ACCELERATION = 1250;
this.BACK_ACCELERATION = 1500;
this.WHEEL_ANGULAR_ACCELERATION = 1.5;
this.FRONT_DECCELERATION = 750;
this.WHEEL_ANGULAR_DECCELERATION = 1.0;
this.STEERING_RADIUS_RATIO = 0.0023;
this.MAX_TILT_SIDES = 0.05;
this.MAX_TILT_FRONTBACK = 0.015;
// internal control variables
this.speed = 0;
this.acceleration = 0;
this.wheelOrientation = 0;
this.carOrientation = 0;
// car rigging
this.root = new THREE.Object3D();
this.frontLeftWheelRoot = new THREE.Object3D();
this.frontRightWheelRoot = new THREE.Object3D();
this.bodyMesh = null;
// this.frontLeftWheelMesh = null;
// this.frontRightWheelMesh = null;
// this.backLeftWheelMesh = null;
// this.backRightWheelMesh = null;
this.bodyGeometry = null;
this.wheelGeometry = null;
// internal helper variables
this.loaded = false;
this.meshes = [];
this.collisionCounter = 0;
// API
this.enableShadows = function(enable) {
for (var i = 0; i < this.meshes.length; i++) {
this.meshes[i].castShadow = enable;
this.meshes[i].receiveShadow = enable;
}
};
this.setVisible = function(enable) {
for (var i = 0; i < this.meshes.length; i++) {
this.meshes[i].visible = enable;
this.meshes[i].visible = enable;
}
};
this.loadPartsJSON = function(bodyURL, wheelURL) {
var loader = new THREE.JSONLoader();
loader.load(bodyURL, createBody);
// loader.load(wheelURL, createWheels);
};
this.loadPartsBinary = function(bodyURL, wheelURL) {
var loader = new THREE.BinaryLoader();
loader.load({
model: bodyURL,
callback: function(geometry) {
createBody(geometry)
}
});
loader.load({
model: wheelURL,
callback: function(geometry) {
createWheels(geometry)
}
});
};
this.updateCarModel = function(delta, controls) {
// speed and wheels based on controls
if (controls.moveForward) {
this.speed = clamp(this.speed + delta * this.FRONT_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED);
this.acceleration = clamp(this.acceleration + delta, -1, 1);
}
if (controls.moveBackward) {
this.speed = clamp(this.speed - delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, this.MAX_SPEED);
this.acceleration = clamp(this.acceleration - delta, -1, 1);
}
if (controls.moveLeft) {
this.wheelOrientation = clamp(this.wheelOrientation + delta * this.WHEEL_ANGULAR_ACCELERATION, -this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION);
}
if (controls.moveRight) {
this.wheelOrientation = clamp(this.wheelOrientation - delta * this.WHEEL_ANGULAR_ACCELERATION, -this.MAX_WHEEL_ROTATION, this.MAX_WHEEL_ROTATION);
}
// speed decay
if (!(controls.moveForward || controls.moveBackward)) {
if (this.speed > 0) {
var k = exponentialEaseOut(this.speed / this.MAX_SPEED);
this.speed = clamp(this.speed - k * delta * this.FRONT_DECCELERATION, 0, this.MAX_SPEED);
this.acceleration = clamp(this.acceleration - k * delta, 0, 1);
} else {
var k = exponentialEaseOut(this.speed / this.MAX_REVERSE_SPEED);
this.speed = clamp(this.speed + k * delta * this.BACK_ACCELERATION, this.MAX_REVERSE_SPEED, 0);
this.acceleration = clamp(this.acceleration + k * delta, -1, 0);
}
}
// steering decay
if (!(controls.moveLeft || controls.moveRight)) {
if (this.wheelOrientation > 0) {
this.wheelOrientation = clamp(this.wheelOrientation - delta * this.WHEEL_ANGULAR_DECCELERATION, 0, this.MAX_WHEEL_ROTATION);
} else {
this.wheelOrientation = clamp(this.wheelOrientation + delta * this.WHEEL_ANGULAR_DECCELERATION, -this.MAX_WHEEL_ROTATION, 0);
}
}
var forwardDelta;
// COLLISION
var foundCollision = true;
// Building colliders
// left back
var deltaX = Math.floor(Math.sin(this.root.rotation.y - 10) * 1000); // Using a little trigonometry calculate a point in space 1000 units in front of our character
var deltaZ = Math.floor(Math.cos(this.root.rotation.y - 10) * 1000);
var focusX = this.root.position.x + deltaX; // Calculate where this collision focus point is in relation to our character position
var focusZ = this.root.position.z + deltaZ;
if (isColliding(this.root.position, focusX, focusZ) == false) {
// right back
var deltaX = Math.floor(Math.sin(this.root.rotation.y + 10) * 1000);
var deltaZ = Math.floor(Math.cos(this.root.rotation.y + 10) * 1000);
var focusX = this.root.position.x + deltaX;
var focusZ = this.root.position.z + deltaZ;
if (isColliding(this.root.position, focusX, focusZ) == false) {
// right front
var deltaX = Math.floor(Math.sin(this.root.rotation.y - 10) * 1000);
var deltaZ = Math.floor(Math.cos(this.root.rotation.y - 10) * 1000);
var focusX = this.root.position.x - deltaX;
var focusZ = this.root.position.z - deltaZ;
if (isColliding(this.root.position, focusX, focusZ) == false) {
// left front
var deltaX = Math.floor(Math.sin(this.root.rotation.y + 10) * 1000);
var deltaZ = Math.floor(Math.cos(this.root.rotation.y + 10) * 1000);
var focusX = this.root.position.x - deltaX;
var focusZ = this.root.position.z - deltaZ;
if (isColliding(this.root.position, focusX, focusZ) == false) {
foundCollision = false;
}
}
}
}
if (foundCollision) {
forwardDelta = this.speed * -3;
this.speed *= -0.25;
// this.acceleration = 0;
}
// car update
else {
forwardDelta = this.speed;
}
this.carOrientation += (forwardDelta * this.STEERING_RADIUS_RATIO) * this.wheelOrientation;
// Ground collider
var raycaster = new THREE.Raycaster(new THREE.Vector3(this.root.position.x, this.root.position.y + 50, this.root.position.z), new THREE.Vector3(0, -1, 0), 0, 500);
for (var x = 0; x < gridManager.visibleBlocks.length; x++) {
for (var z = 0; z < gridManager.visibleBlocks.length; z++) {
var intersects = raycaster.intersectObject(gridManager.visibleBlocks[x][z].colliders[1]);
if (intersects.length > 0) {
// console.log("plane collision: " + intersects.length);
// console.log(intersects[0]);
// console.log(intersects[0].point);
// this begin = this.root.position.y;
// var target = intersects[0].point.y + 1;
this.root.position.y = this.lerp(this.root.position.y, intersects[0].point.y + 0.8, delta);
// this.root.position.y = intersects[0].point.y + 1;
break;
}
}
}
// displacement
this.root.position.x += Math.sin(this.carOrientation) * forwardDelta;
this.root.position.z += Math.cos(this.carOrientation) * forwardDelta;
// steering
this.root.rotation.y = this.carOrientation;
// tilt
if (this.loaded) {
this.bodyMesh.rotation.z = this.MAX_TILT_SIDES * this.wheelOrientation * (this.speed / this.MAX_SPEED);
this.bodyMesh.rotation.x = -this.MAX_TILT_FRONTBACK * this.acceleration;
}
// // wheels rolling
// var angularSpeedRatio = 1 / (this.modelScale * (this.wheelDiameter / 2));
// var wheelDelta = forwardDelta * angularSpeedRatio;
// if (this.loaded) {
// this.frontLeftWheelMesh.rotation.x += wheelDelta;
// this.frontRightWheelMesh.rotation.x += wheelDelta;
// this.backLeftWheelMesh.rotation.x += wheelDelta;
// this.backRightWheelMesh.rotation.x += wheelDelta;
// }
// // front wheels steering
// this.frontLeftWheelRoot.rotation.y = this.wheelOrientation;
// this.frontRightWheelRoot.rotation.y = this.wheelOrientation;
}
function isColliding(position, focusX, focusZ) {
var raycaster = new THREE.Raycaster(position, new THREE.Vector3(focusX, position.y, focusZ), 0, 4);
for (var x = 0; x < gridManager.visibleBlocks.length; x++) {
for (var z = 0; z < gridManager.visibleBlocks.length; z++) {
var intersects = raycaster.intersectObject(gridManager.visibleBlocks[x][z].colliders[0]);
if (intersects.length > 0) {
return true;
}
}
}
return false;
}
// internal helper methods
function createBody(geometry, materials) {
console.log("create body");
scope.bodyGeometry = geometry;
createCar(materials);
}
function createWheels(geometry) {
scope.wheelGeometry = geometry;
createCar();
}
function createCar(materials) {
if (scope.bodyGeometry /* && scope.wheelGeometry*/ ) {
// compute wheel geometry parameters
// if (scope.autoWheelGeometry) {
// scope.wheelGeometry.computeBoundingBox();
// var bb = scope.wheelGeometry.boundingBox;
// var dx = 0.5 * (bb.x[1] + bb.x[0]);
// var dy = 0.5 * (bb.y[1] + bb.y[0]);
// var dz = 0.5 * (bb.z[1] + bb.z[0]);
// scope.wheelOffsetX = dx;
// scope.wheelOffsetY = dy;
// scope.wheelOffsetZ = dz;
// scope.wheelDiameter = bb.y[1] - bb.y[0];
// THREE.GeometryUtils.center(scope.wheelGeometry);
// }
// rig the car
var delta,
s = scope.modelScale,
faceMaterial = new THREE.MeshFaceMaterial(materials);
// body
scope.bodyMesh = new THREE.Mesh(scope.bodyGeometry, faceMaterial);
scope.bodyMesh.scale.set(s, s, s);
scope.root.add(scope.bodyMesh);
// // front left wheel
// delta = new THREE.Vector3(s * scope.wheelOffsetX, s * scope.wheelOffsetY, s * scope.wheelOffsetZ);
// scope.frontLeftWheelRoot.position.add(delta);
// scope.frontLeftWheelMesh = new THREE.Mesh(scope.wheelGeometry, faceMaterial);
// scope.frontLeftWheelMesh.scale.set(s, s, s);
// scope.frontLeftWheelRoot.add(scope.frontLeftWheelMesh);
// scope.root.add(scope.frontLeftWheelRoot);
// // front right wheel
// delta = new THREE.Vector3(-s * scope.wheelOffsetX, s * scope.wheelOffsetY, s * scope.wheelOffsetZ);
// scope.frontRightWheelRoot.position.add(delta);
// scope.frontRightWheelMesh = new THREE.Mesh(scope.wheelGeometry, faceMaterial);
// scope.frontRightWheelMesh.scale.set(s, s, s);
// scope.frontRightWheelMesh.rotation.z = Math.PI;
// scope.frontRightWheelRoot.add(scope.frontRightWheelMesh);
// scope.root.add(scope.frontRightWheelRoot);
// // back left wheel
// delta = new THREE.Vector3(s * scope.wheelOffsetX, s * scope.wheelOffsetY, -s * scope.wheelOffsetZ - scope.backWheelOffset);
// scope.backLeftWheelMesh = new THREE.Mesh(scope.wheelGeometry, faceMaterial);
// scope.backLeftWheelMesh.position.add(delta);
// scope.backLeftWheelMesh.scale.set(s, s, s);
// scope.root.add(scope.backLeftWheelMesh);
// // back right wheel
// delta = new THREE.Vector3(-s * scope.wheelOffsetX, s * scope.wheelOffsetY, -s * scope.wheelOffsetZ - scope.backWheelOffset)
// scope.backRightWheelMesh = new THREE.Mesh(scope.wheelGeometry, faceMaterial);
// scope.backRightWheelMesh.position.add(delta);
// scope.backRightWheelMesh.scale.set(s, s, s);
// scope.backRightWheelMesh.rotation.z = Math.PI;
// scope.root.add(scope.backRightWheelMesh);
// cache meshes
scope.meshes = [scope.bodyMesh /*, scope.frontLeftWheelMesh, scope.frontRightWheelMesh, scope.backLeftWheelMesh, scope.backRightWheelMesh*/ ];
// callback
scope.loaded = true;
if (scope.callback) {
scope.callback(scope);
}
}
}
function clamp(x, a, b) {
return x < a ? a : (x > b ? b : x);
}
function quadraticEaseOut(k) {
return -k * (k - 2);
}
function cubicEaseOut(k) {
return --k * k * k + 1;
}
function circularEaseOut(k) {
return Math.sqrt(1 - --k * k);
}
function sinusoidalEaseOut(k) {
return Math.sin(k * Math.PI / 2);
}
function exponentialEaseOut(k) {
return k == 1 ? 1 : -Math.pow(2, -10 * k) + 1;
}
};