Skip to content

Examples : Helicopter Physics

Description

<>

This example demonstrates,

Code

./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
300
import * as THREE from 'three'
import Stats from 'three/examples/jsm/libs/stats.module'
import { GUI } from 'dat.gui'
import * as CANNON from 'cannon-es'
import CannonDebugRenderer from './utils/cannonDebugRenderer'

const scene = new THREE.Scene()

const 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 = 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 = 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 = new THREE.MeshPhongMaterial()

const world = new CANNON.World()
world.gravity.set(0, -9.82, 0)

const groundMaterial = new CANNON.Material('groundMaterial')
groundMaterial.friction = 0.25
groundMaterial.restitution = 0.25

const wheelMaterial = new CANNON.Material('wheelMaterial')
wheelMaterial.friction = 0.25
wheelMaterial.restitution = 0.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 (let i = 0; i < 100; i++) {
    const height = Math.random() * 50 + 0.5
    const jump = new THREE.Mesh(
        new THREE.CylinderGeometry(0, 1, height, 5),
        phongMaterial
    )
    jump.position.x = Math.random() * 100 - 50
    jump.position.y = height / 2
    jump.position.z = Math.random() * 100 - 50
    scene.add(jump)

    const cylinderShape = new CANNON.Cylinder(0.01, 1, height, 5)
    const cylinderBody = new CANNON.Body({ mass: 0 })
    cylinderBody.addShape(cylinderShape, new CANNON.Vec3())
    cylinderBody.position.x = jump.position.x
    cylinderBody.position.y = jump.position.y
    cylinderBody.position.z = jump.position.z
    world.addBody(cylinderBody)
}

const heliBodyGeometry = new THREE.SphereGeometry(0.66)
const heliBodyMesh = new THREE.Mesh(heliBodyGeometry, phongMaterial)
heliBodyMesh.position.y = 1
heliBodyMesh.castShadow = true
scene.add(heliBodyMesh)
heliBodyMesh.add(chaseCam)
const heliTailGeometry = new THREE.BoxGeometry(0.1, 0.1, 2)
const heliTailMesh = new THREE.Mesh(heliTailGeometry, phongMaterial)
heliTailMesh.position.z = 1
heliTailMesh.castShadow = true
heliBodyMesh.add(heliTailMesh)
const skidGeometry = new THREE.BoxGeometry(0.1, 0.05, 1.5)
const skidLeftMesh = new THREE.Mesh(skidGeometry, phongMaterial)
const skidRightMesh = new THREE.Mesh(skidGeometry, phongMaterial)
skidLeftMesh.position.set(-0.5, -0.45, 0)
skidRightMesh.position.set(0.5, -0.45, 0)
skidLeftMesh.castShadow = true
skidRightMesh.castShadow = true
heliBodyMesh.add(skidLeftMesh)
heliBodyMesh.add(skidRightMesh)

const heliBodyShape = new CANNON.Box(new CANNON.Vec3(0.6, 0.5, 0.6))
const heliBody = new CANNON.Body({ mass: 0.5 })
heliBody.addShape(heliBodyShape)
heliBody.position.x = heliBodyMesh.position.x
heliBody.position.y = heliBodyMesh.position.y
heliBody.position.z = heliBodyMesh.position.z
heliBody.angularDamping = 0.9 //so it doesn't pendulum so much
world.addBody(heliBody)

//rotor
const rotorGeometry = new THREE.BoxGeometry(0.1, 0.01, 5)
const rotorMesh: THREE.Mesh = new THREE.Mesh(rotorGeometry, phongMaterial)
rotorMesh.position.x = 0
rotorMesh.position.y = 3
rotorMesh.position.z = 0
scene.add(rotorMesh)

const rotorShape = new CANNON.Sphere(0.1)
const rotorBody = new CANNON.Body({ mass: 1 })
rotorBody.addShape(rotorShape)
rotorBody.position.x = rotorMesh.position.x
rotorBody.position.y = rotorMesh.position.y
rotorBody.position.z = rotorMesh.position.z
rotorBody.linearDamping = 0.5 //simulates auto altitude
world.addBody(rotorBody)

const rotorConstraint = new CANNON.PointToPointConstraint(
    heliBody,
    new CANNON.Vec3(0, 1, 0),
    rotorBody,
    new CANNON.Vec3()
)

rotorConstraint.collideConnected = false
world.addConstraint(rotorConstraint)

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

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 = new 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 = new THREE.Clock()
let delta

const cannonDebugRenderer = new CannonDebugRenderer(scene, world)

const v = new THREE.Vector3()
let banking = false
let climbing = false
let pitching = false
let yawing = false
const stableLift = 14.7
const thrust = new CANNON.Vec3(0, 5, 0)

function animate() {
    requestAnimationFrame(animate)

    helper.update()

    delta = Math.min(clock.getDelta(), 0.1)
    world.step(delta)

    cannonDebugRenderer.update()

    // Copy coordinates from Cannon to Three.js
    rotorMesh.position.set(
        rotorBody.position.x,
        rotorBody.position.y,
        rotorBody.position.z
    )
    rotorMesh.rotateY(thrust.y * delta * 2)

    heliBodyMesh.position.set(
        heliBody.position.x,
        heliBody.position.y,
        heliBody.position.z
    )
    heliBodyMesh.quaternion.set(
        heliBody.quaternion.x,
        heliBody.quaternion.y,
        heliBody.quaternion.z,
        heliBody.quaternion.w
    )

    climbing = false
    if (keyMap['KeyW']) {
        if (thrust.y < 40) {
            thrust.y += 5 * delta
            climbing = true
        }
    }
    if (keyMap['KeyS']) {
        if (thrust.y > 0) {
            thrust.y -= 5 * delta
            climbing = true
        }
    }
    yawing = false
    if (keyMap['KeyA']) {
        if (rotorBody.angularVelocity.y < 2.0)
            rotorBody.angularVelocity.y += 5 * delta
        yawing = true
    }
    if (keyMap['KeyD']) {
        if (rotorBody.angularVelocity.y > -2.0)
            rotorBody.angularVelocity.y -= 5 * delta
        yawing = true
    }

    pitching = false
    if (keyMap['ArrowUp'] || keyMap['Numpad8']) {
        if (thrust.z >= -10.0) thrust.z -= 5 * delta
        pitching = true
    }
    if (keyMap['ArrowDown'] || keyMap['Numpad5']) {
        if (thrust.z <= 10.0) thrust.z += 5 * delta
        pitching = true
    }
    banking = false
    if (keyMap['ArrowLeft'] || keyMap['Numpad4']) {
        if (thrust.x >= -10.0) thrust.x -= 5 * delta
        banking = true
    }
    if (keyMap['ArrowRight'] || keyMap['Numpad6']) {
        if (thrust.x <= 10.0) thrust.x += 5 * delta
        banking = true
    }

    //auto stabilise
    if (!yawing) {
        if (rotorBody.angularVelocity.y < 0)
            rotorBody.angularVelocity.y += 1 * delta
        if (rotorBody.angularVelocity.y > 0)
            rotorBody.angularVelocity.y -= 1 * delta
    }
    heliBody.angularVelocity.y = rotorBody.angularVelocity.y

    if (!pitching) {
        if (thrust.z < 0) thrust.z += 2.5 * delta
        if (thrust.z > 0) thrust.z -= 2.5 * delta
    }
    if (!banking) {
        if (thrust.x < 0) thrust.x += 2.5 * delta
        if (thrust.x > 0) thrust.x -= 2.5 * delta
    }
    if (!climbing && heliBodyMesh.position.y > 2) {
        thrust.y = stableLift
    }

    rotorBody.applyLocalForce(thrust, new CANNON.Vec3())

    camera.lookAt(heliBodyMesh.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()

Comments