Examples : Car Physics

Description

This examples demonstrates,

Code

./src/server/server.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
import express from "express"
import path from "path"
import http from "http"

const port: number = 3000

class App {
    private server: http.Server
    private port: number

    constructor(port: number) {
        this.port = port
        const app = express()
        app.use(express.static(path.join(__dirname, '../client')))
        app.use('/build/three.module.js', express.static(path.join(__dirname, '../../node_modules/three/build/three.module.js')))
        app.use('/jsm/libs/stats.module', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/libs/stats.module.js')))
        app.use('/jsm/libs/dat.gui.module', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/libs/dat.gui.module.js')))
        app.use('/cannon/cannon.min', express.static(path.join(__dirname, '../../node_modules/cannon/build/cannon.min.js')))

        this.server = new http.Server(app);
    }

    public Start() {
        this.server.listen(this.port, () => {
            console.log(`Server listening on port ${this.port}.`)
        })
    }
}

new App(port).Start()

./src/client/client.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
import * as THREE from '/build/three.module.js'
import Stats from '/jsm/libs/stats.module'
import { GUI } from '/jsm/libs/dat.gui.module'
import '/cannon/cannon.min'
import CannonDebugRenderer from './utils/cannonDebugRenderer.js'

const scene: THREE.Scene = new THREE.Scene()

var light = new THREE.DirectionalLight();
light.position.set(25, 50, 25)
light.castShadow = true;
light.shadow.mapSize.width = 16384;
light.shadow.mapSize.height = 16384;
light.shadow.camera.near = 0.5;
light.shadow.camera.far = 100
light.shadow.camera.top = 100
light.shadow.camera.bottom = -100
light.shadow.camera.left = -100
light.shadow.camera.right = 100
scene.add(light);

const helper = new THREE.CameraHelper(light.shadow.camera);
scene.add(helper);

const camera: THREE.PerspectiveCamera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
const chaseCam = new THREE.Object3D()
chaseCam.position.set(0, 0, 0)
const chaseCamPivot = new THREE.Object3D()
chaseCamPivot.position.set(0, 2, 4)
chaseCam.add(chaseCamPivot)
scene.add(chaseCam)

const renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
document.body.appendChild(renderer.domElement)

const phongMaterial: THREE.MeshPhongMaterial = new THREE.MeshPhongMaterial()

const world = new CANNON.World()
world.gravity.set(0, -9.82, 0)
//world.broadphase = new CANNON.NaiveBroadphase() //
//world.solver.iterations = 10
//world.allowSleep = true

const groundMaterial = new CANNON.Material("groundMaterial");
groundMaterial.friction = .25
groundMaterial.restitution = .25

var wheelMaterial = new CANNON.Material("wheelMaterial");
wheelMaterial.friction = .25
wheelMaterial.restitution = .25

//ground
const groundGeometry: THREE.PlaneGeometry = new THREE.PlaneGeometry(100, 100)
const groundMesh: THREE.Mesh = new THREE.Mesh(groundGeometry, phongMaterial)
groundMesh.rotateX(-Math.PI / 2)
groundMesh.receiveShadow = true;
scene.add(groundMesh)
const groundShape = new CANNON.Box(new CANNON.Vec3(50, 1, 50))
const groundBody = new CANNON.Body({ mass: 0, material: groundMaterial })
groundBody.addShape(groundShape)
groundBody.position.set(0, -1, 0)
world.addBody(groundBody)

//jumps
for (var i = 0; i < 100; i++) {
    const jump = new THREE.Mesh(
        new THREE.CylinderGeometry(0, 1, .5, 5),
        phongMaterial
    )
    jump.position.x = (Math.random() * 100) - 50
    jump.position.y = .5
    jump.position.z = (Math.random() * 100) - 50
    scene.add(jump);

    const cylinderShape = new CANNON.Cylinder(.01, 1, .5, 5)
    const cylinderBody = new CANNON.Body({ mass: 0 });
    const cylinderQuaternion = new CANNON.Quaternion()
    cylinderQuaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2)
    cylinderBody.addShape(cylinderShape, new CANNON.Vec3, cylinderQuaternion)
    cylinderBody.position.x = jump.position.x
    cylinderBody.position.y = jump.position.y
    cylinderBody.position.z = jump.position.z
    world.addBody(cylinderBody)
}

const carBodyGeometry: THREE.BoxGeometry = new THREE.BoxGeometry(1, 1, 2)
const carBodyMesh: THREE.Mesh = new THREE.Mesh(carBodyGeometry, phongMaterial)
carBodyMesh.position.y = 3
carBodyMesh.castShadow = true
scene.add(carBodyMesh)
carBodyMesh.add(chaseCam)

const carBodyShape = new CANNON.Box(new CANNON.Vec3(.5, .5, 1))
const carBody = new CANNON.Body({ mass: 1 });
carBody.addShape(carBodyShape)
carBody.position.x = carBodyMesh.position.x
carBody.position.y = carBodyMesh.position.y
carBody.position.z = carBodyMesh.position.z
world.addBody(carBody)


//front left wheel
const wheelLFGeometry: THREE.CylinderGeometry = new THREE.CylinderGeometry(.33, .33, .2)
wheelLFGeometry.rotateZ(Math.PI / 2)
const wheelLFMesh: THREE.Mesh = new THREE.Mesh(wheelLFGeometry, phongMaterial)
wheelLFMesh.position.x = -1
wheelLFMesh.position.y = 3
wheelLFMesh.position.z = -1
wheelLFMesh.castShadow = true
scene.add(wheelLFMesh)
const wheelLFShape = new CANNON.Sphere(.33)
const wheelLFBody = new CANNON.Body({ mass: 1, material: wheelMaterial });
wheelLFBody.addShape(wheelLFShape)
wheelLFBody.position.x = wheelLFMesh.position.x
wheelLFBody.position.y = wheelLFMesh.position.y
wheelLFBody.position.z = wheelLFMesh.position.z
world.addBody(wheelLFBody)

//front right wheel
const wheelRFGeometry: THREE.CylinderGeometry = new THREE.CylinderGeometry(.33, .33, .2)
wheelRFGeometry.rotateZ(Math.PI / 2)
const wheelRFMesh: THREE.Mesh = new THREE.Mesh(wheelRFGeometry, phongMaterial)
wheelRFMesh.position.y = 3
wheelRFMesh.position.x = 1
wheelRFMesh.position.z = -1
wheelRFMesh.castShadow = true
scene.add(wheelRFMesh)
const wheelRFShape = new CANNON.Sphere(.33)
const wheelRFBody = new CANNON.Body({ mass: 1, material: wheelMaterial });
wheelRFBody.addShape(wheelRFShape)
wheelRFBody.position.x = wheelRFMesh.position.x
wheelRFBody.position.y = wheelRFMesh.position.y
wheelRFBody.position.z = wheelRFMesh.position.z
world.addBody(wheelRFBody)

//back left wheel
const wheelLBGeometry: THREE.CylinderGeometry = new THREE.CylinderGeometry(.4, .4, .33)
wheelLBGeometry.rotateZ(Math.PI / 2)
const wheelLBMesh: THREE.Mesh = new THREE.Mesh(wheelLBGeometry, phongMaterial)
wheelLBMesh.position.y = 3
wheelLBMesh.position.x = -1
wheelLBMesh.position.z = 1
wheelLBMesh.castShadow = true
scene.add(wheelLBMesh)
const wheelLBShape = new CANNON.Sphere(.4)
const wheelLBBody = new CANNON.Body({ mass: 1, material: wheelMaterial });
wheelLBBody.addShape(wheelLBShape)
wheelLBBody.position.x = wheelLBMesh.position.x
wheelLBBody.position.y = wheelLBMesh.position.y
wheelLBBody.position.z = wheelLBMesh.position.z
world.addBody(wheelLBBody)

//back right wheel
const wheelRBGeometry: THREE.CylinderGeometry = new THREE.CylinderGeometry(.4, .4, .33)
wheelRBGeometry.rotateZ(Math.PI / 2)
const wheelRBMesh: THREE.Mesh = new THREE.Mesh(wheelRBGeometry, phongMaterial)
wheelRBMesh.position.y = 3
wheelRBMesh.position.x = 1
wheelRBMesh.position.z = 1
wheelRBMesh.castShadow = true
scene.add(wheelRBMesh)
const wheelRBShape = new CANNON.Sphere(.4)
const wheelRBBody = new CANNON.Body({ mass: 1, material: wheelMaterial });
wheelRBBody.addShape(wheelRBShape)
wheelRBBody.position.x = wheelRBMesh.position.x
wheelRBBody.position.y = wheelRBMesh.position.y
wheelRBBody.position.z = wheelRBMesh.position.z
world.addBody(wheelRBBody)


const leftFrontAxis = new CANNON.Vec3(1, 0, 0);
const rightFrontAxis = new CANNON.Vec3(1, 0, 0);
const leftBackAxis = new CANNON.Vec3(1, 0, 0);
const rightBackAxis = new CANNON.Vec3(1, 0, 0);

const constraintLF = new CANNON.HingeConstraint(carBody, wheelLFBody, { pivotA: new CANNON.Vec3(-1, -.5, -1), axisA: leftFrontAxis, maxForce: .99 });
world.addConstraint(constraintLF);
const constraintRF = new CANNON.HingeConstraint(carBody, wheelRFBody, { pivotA: new CANNON.Vec3(1, -.5, -1), axisA: rightFrontAxis, maxForce: .99 });
world.addConstraint(constraintRF);
const constraintLB = new CANNON.HingeConstraint(carBody, wheelLBBody, { pivotA: new CANNON.Vec3(-1, -.5, 1), axisA: leftBackAxis, maxForce: .99 });
world.addConstraint(constraintLB);
const constraintRB = new CANNON.HingeConstraint(carBody, wheelRBBody, { pivotA: new CANNON.Vec3(1, -.5, 1), axisA: rightBackAxis, maxForce: .99 });
world.addConstraint(constraintRB);

//rear wheel drive
constraintLB.enableMotor()
constraintRB.enableMotor()

const keyMap: { [id: string]: boolean } = {}
const onDocumentKey = (e: KeyboardEvent) => {
    keyMap[e.key] = e.type === 'keydown';
}

let forwardVelocity = 0
let rightVelocity = 0

document.addEventListener("keydown", onDocumentKey, false);
document.addEventListener("keyup", onDocumentKey, false);

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    render()
}

const stats = Stats()
document.body.appendChild(stats.dom)

const gui = new GUI()
const physicsFolder = gui.addFolder("Physics")
physicsFolder.add(world.gravity, "x", -10.0, 10.0, 0.1)
physicsFolder.add(world.gravity, "y", -10.0, 10.0, 0.1)
physicsFolder.add(world.gravity, "z", -10.0, 10.0, 0.1)
physicsFolder.open()

const clock: THREE.Clock = new THREE.Clock()

const cannonDebugRenderer = new CannonDebugRenderer(scene, world)

const v = new THREE.Vector3()
let thrusting = false

var animate = function () {
    requestAnimationFrame(animate)

    helper.update()

    let delta = clock.getDelta()
    if (delta > .1) delta = .1
    world.step(delta)
    cannonDebugRenderer.update()

    // Copy coordinates from Cannon.js to Three.js
    carBodyMesh.position.set(carBody.position.x, carBody.position.y, carBody.position.z);
    carBodyMesh.quaternion.set(carBody.quaternion.x, carBody.quaternion.y, carBody.quaternion.z, carBody.quaternion.w);

    wheelLFMesh.position.set(wheelLFBody.position.x, wheelLFBody.position.y, wheelLFBody.position.z);
    wheelLFMesh.quaternion.set(wheelLFBody.quaternion.x, wheelLFBody.quaternion.y, wheelLFBody.quaternion.z, wheelLFBody.quaternion.w);

    wheelRFMesh.position.set(wheelRFBody.position.x, wheelRFBody.position.y, wheelRFBody.position.z);
    wheelRFMesh.quaternion.set(wheelRFBody.quaternion.x, wheelRFBody.quaternion.y, wheelRFBody.quaternion.z, wheelRFBody.quaternion.w);

    wheelLBMesh.position.set(wheelLBBody.position.x, wheelLBBody.position.y, wheelLBBody.position.z);
    wheelLBMesh.quaternion.set(wheelLBBody.quaternion.x, wheelLBBody.quaternion.y, wheelLBBody.quaternion.z, wheelLBBody.quaternion.w);

    wheelRBMesh.position.set(wheelRBBody.position.x, wheelRBBody.position.y, wheelRBBody.position.z);
    wheelRBMesh.quaternion.set(wheelRBBody.quaternion.x, wheelRBBody.quaternion.y, wheelRBBody.quaternion.z, wheelRBBody.quaternion.w);

    thrusting = false
    if (keyMap['w'] || keyMap["ArrowUp"]) {
        if (forwardVelocity < 100.0) forwardVelocity += 1
        thrusting = true
    }
    if (keyMap['s'] || keyMap["ArrowDown"]) {
        if (forwardVelocity > -100.0) forwardVelocity -= 1
        thrusting = true
    }
    if (keyMap['a'] || keyMap["ArrowLeft"]) {
        if (rightVelocity > -1.0) rightVelocity -= .1
    }
    if (keyMap['d'] || keyMap["ArrowRight"]) {
        if (rightVelocity < 1.0) rightVelocity += .1
    }
    if (keyMap[' ']) {
        if (forwardVelocity > 0) { forwardVelocity -= 1 }
        if (forwardVelocity < 0) { forwardVelocity += 1 }
    }

    if (!thrusting) {
        //not going forward or backwards so gradually slow down
        if (forwardVelocity > 0) { forwardVelocity -= .25 }
        if (forwardVelocity < 0) { forwardVelocity += .25 }
    }

    constraintLB.setMotorSpeed(forwardVelocity)
    constraintRB.setMotorSpeed(forwardVelocity)
    constraintLF.axisA.z = rightVelocity
    constraintRF.axisA.z = rightVelocity

    camera.lookAt(carBodyMesh.position)

    chaseCamPivot.getWorldPosition(v)
    if (v.y < 1) { v.y = 1 }
    camera.position.lerpVectors(camera.position, v, 0.05)

    render()

    stats.update()
};

function render() {
    renderer.render(scene, camera)
}
animate();