-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanualController.js
More file actions
79 lines (67 loc) · 2.11 KB
/
manualController.js
File metadata and controls
79 lines (67 loc) · 2.11 KB
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
class ManualController {
constructor(newcar) {
this.car = newcar;
this.instantAngle = 0;
this.stearingAngle = 0;
this.steeringFilterFrequency = 10;
this.force = 0;
this.upPressed = false;
this.downPressed = false;
this.leftPressed = false;
this.rightPressed = false;
keyListeners.push(this);
}
update(dt) {
if (this.downPressed) {
this.force = -3080;
} else if (this.upPressed) {
this.force = 3080;
} else {
this.force = 0;
}
// Steering code
if (this.leftPressed) {
this.instantAngle += 5 * dt;
if (this.instantAngle > this.car.maxStearingAngle) {
this.instantAngle = this.car.maxStearingAngle;
}
} else if (this.rightPressed) {
this.instantAngle -= 5 * dt;
if (this.instantAngle < -this.car.maxStearingAngle) {
this.instantAngle = -this.car.maxStearingAngle;
}
} else {
this.instantAngle = this.instantAngle - this.instantAngle * 2 * dt;
}
let a = dt * 2 * PI * this.steeringFilterFrequency;
this.stearingAngle = (1 - a) * this.stearingAngle + a * this.instantAngle;
// mouse control:
// this.stearingAngle = - mouseX / width * 2 * this.car.maxStearingAngle + this.car.maxStearingAngle;
this.car.setSetpoints(this.stearingAngle, this.force);
}
draw() {}
keyPressed() {
if (keyIsPressed) {
if (keyCode === UP_ARROW) {
this.upPressed = true;
} else if (keyCode === DOWN_ARROW) {
this.downPressed = true;
} else if (keyCode === LEFT_ARROW) {
this.leftPressed = true;
} else if (keyCode === RIGHT_ARROW) {
this.rightPressed = true;
}
}
}
keyReleased() {
if (keyCode === UP_ARROW) {
this.upPressed = false;
} else if (keyCode === DOWN_ARROW) {
this.downPressed = false;
} else if (keyCode === LEFT_ARROW) {
this.leftPressed = false;
} else if (keyCode === RIGHT_ARROW) {
this.rightPressed = false;
}
}
}