-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebris.js
62 lines (49 loc) · 1.57 KB
/
debris.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
import { Vector } from "./vector.js"
export class RocketDebris {
gravityScale = 6000
destroyed = false
rotation = Math.random() * Math.PI
rotationalVelocity = Math.random() * 10
constructor(x, y, velocityX, velocityY) {
this.x = x
this.y = y
this.velocityX = velocityX
this.velocityY = velocityY
}
draw(ctx) {
// Transform the context.
ctx.translate(this.x, this.y)
ctx.rotate(this.rotation)
const hh = 2
const hw = 2
// Draw a triangle.
ctx.fillStyle = 'white'
ctx.beginPath();
ctx.moveTo(0, -hh)
ctx.lineTo(-hw, hh)
ctx.lineTo(hw, hh)
ctx.fill()
ctx.closePath()
// Restore transform.
ctx.rotate(-this.rotation)
ctx.translate(-this.x, -this.y)
}
update(deltaTime, planet) {
// Apply velocity.
this.x += this.velocityX * deltaTime
this.y += this.velocityY * deltaTime
this.rotation += this.rotationalVelocity * deltaTime
this.applyGravity(deltaTime, planet.x, planet.y)
// Destroy debris if colliding with planet.
if (planet.isOverlapping(this.x, this.y)) {
this.destroyed = true
}
}
applyGravity(deltaTime, planetX, planetY) {
const delta = new Vector(planetX - this.x, planetY - this.y)
const gravity = this.gravityScale / delta.len()
const acceleration = delta.norm().scale(gravity * deltaTime)
this.velocityX += acceleration.x
this.velocityY += acceleration.y
}
}