/
rocket.js
64 lines (54 loc) · 1.43 KB
/
rocket.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
class Rocket {
vel = new Vector(0, 0);
acc = new Vector(0, 0);
gravity = new Vector(0, 3.711 * scaleFac);
dir = 0;
angleRotateLimit = 15;
thurstPower = 3.72 * scaleFac / 4;
thurst = new Vector(0, 0);
constructor(x, y, r) {
this.pos = new Vector(x, y);
this.r = r;
}
drawRocket() {
fill(255);
stroke(0);
push();
translate(this.pos.x, this.pos.y);
rotate(this.dir);
ellipse(0, 0, this.r, this.r);
ellipse(0, -10, 3, 3);
pop();
}
moveRocket() {
this.acc = Vector.add(this.acc, this.gravity);
this.acc = Vector.add(this.acc, this.thurst);
this.vel = Vector.add(this.vel, this.acc);
this.pos = Vector.add(this.pos, this.vel);
}
applyForce(force, angle) {
let thurstToApply = force;
let angleToRotate = angle - this.dir;
if (force > 4) {
thurstToApply = 4;
} else if (force < 0) {
thurstToApply = 0;
}
if (angleToRotate > this.angleRotateLimit) {
angleToRotate = this.angleRotateLimit;
} else if (angleToRotate < -this.angleRotateLimit) {
angleToRotate = -this.angleRotateLimit;
}
this.dir += angleToRotate;
if (this.dir > 90) {
this.dir = 90;
} else if (this.dir < -90) {
this.dir = -90;
}
this.thurst.y = -scaleFac * cos(this.dir) * thurstToApply;
this.thurst.x = scaleFac * sin(this.dir) * thurstToApply;
}
deepCopy() {
return new Rocket(startingX, startingY, rocketR);
}
}