-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlander.js
92 lines (84 loc) · 2.19 KB
/
lander.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
/**
* A simple lander affected by gravity.
*/
class Lander {
/**
* @constructor
* @param {float} x - The starting horizontal position.
* @param {float} y - The starting vertical position.
*/
constructor(x, y) {
this.pos = [x, y]
this.vel = [0, 0]
this.gravity = [0, -10]
this.thrusterState = {
"up": false,
"left": false,
"right": false
}
}
/**
* Update the lander state.
*
* @param {number} deltaTime - Time step of update in seconds.
*/
update(deltaTime) {
const thrust = math.zeros(2)
if (this.thrusterState["up"]) {
thrust[1] += 30
}
if (this.thrusterState["left"]) {
thrust[0] -= 30
}
if (this.thrusterState["right"]) {
thrust[0] += 30
}
const acc = math.add(this.gravity, thrust)
this.vel[0] += acc[0] * deltaTime
this.vel[1] += acc[1] * deltaTime
this.pos[0] += this.vel[0] * deltaTime
this.pos[1] += this.vel[1] * deltaTime
}
/**
* @param {boolean} on - Whether this thruster is on
*/
setUpThruster(on) {
this.thrusterState["up"] = on
}
/**
* @param {boolean} on - Whether this thruster is on
*/
setRightThruster(on) {
this.thrusterState["right"] = on
}
/**
* @param {boolean} on - Whether this thruster is on
*/
setLeftThruster(on) {
this.thrusterState["left"] = on
}
}
/**
* A vertical radar sensor.
*/
class Radar {
/**
* @constructor
* @param {number} angle - The mounting angle of the radar in radians.
*/
constructor(angle) {
this.angle = angle
}
/**
* Measure the distance from the given point to the ground in the world.
*
* Note that this sensor is currently "perfect" without measurement noise.
*
* @param {number} x - Horizontal position of sensor.
* @param {number} y - Verticle position of sensor.
* @param {World} world - The world to use as reference.
*/
measure(x, y, world) {
return distanceToWorld([x, y], this.angle, world) || -1
}
}