-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicycle.js
69 lines (66 loc) · 1.54 KB
/
bicycle.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
var vehicle;
function setup() {
ca = createCanvas(windowWidth, windowHeight);
ca.position(0, 0)
ca.style("z-index: -1");
vehicle = new bicycle();
}
function draw() {
background(0);
fill(255)
textSize(16);
textAlign(RIGHT);
text("Use 'w' and 's' keys for Acceleration and Decceleration\n \nUse 'a' and 'd' keys for Steering", windowWidth - 40, 40)
vehicle.getinput();
vehicle.update();
vehicle.disp();
}
class bicycle {
constructor() {
this.pos = createVector(80, 40);
this.theta = 0;
this.vel = 0;
this.acc = 0;
this.delta = 0;
this.length = 250;
}
getinput() {
if (keyIsDown(87)) {
this.acc = 0.3;
} else if (keyIsDown(83)) {
this.acc = -0.3;
} else {
this.acc = 0;
}
if (keyIsDown(68)) {
if (this.delta < 0.8) {
this.delta += 0.02;
}
} else if (keyIsDown(65)) {
if (this.delta > -0.8) {
this.delta -= 0.02;
}
}
}
update() {
this.vel += this.acc
if (this.vel > 15 || this.vel < -15) {
this.vel -= this.acc
}
this.pos.x += this.vel * cos(this.theta);
this.pos.y += this.vel * sin(this.theta);
this.theta += (this.vel * tan(this.delta)) / this.length;
this.vel *= 0.95;
}
disp() {
translate(this.pos.x, this.pos.y);
rotate(this.theta);
fill(255)
rect(0, -10, this.length, 20)
fill(255, 0, 0)
rect(-50, -20, 100, 40)
translate(this.length, 0);
rotate(this.delta)
rect(-50, -20, 100, 40)
}
}