forked from nature-of-code/noc-examples-p5.js-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle.js
147 lines (124 loc) · 4.1 KB
/
vehicle.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// The "Vehicle" class
class Vehicle {
constructor(x, y, ms, mf) {
this.position = createVector(x, y);
this.acceleration = createVector(0, 0);
this.velocity = createVector(2, 0);
this.r = 4;
this.maxspeed = ms || 4;
this.maxforce = mf || 0.1;
}
run() {
this.update();
this.display();
}
// This function implements Craig Reynolds' path following algorithm
// http://www.red3d.com/cwr/steer/PathFollow.html
follow(p) {
// Predict position 50 (arbitrary choice) frames ahead
let predict = this.velocity.copy();
predict.normalize();
predict.mult(50);
let predictLoc = p5.Vector.add(this.position, predict);
// Look at the line segment
let a = p.start;
let b = p.end;
// Get the normal point to that line
let normalPoint = getNormalPoint(predictLoc, a, b);
// Find target point a little further ahead of normal
let dir = p5.Vector.sub(b, a);
dir.normalize();
dir.mult(10); // This could be based on velocity instead of just an arbitrary 10 pixels
let target = p5.Vector.add(normalPoint, dir);
// How far away are we from the path?
let distance = p5.Vector.dist(predictLoc, normalPoint);
// Only if the distance is greater than the path's radius do we bother to steer
if (distance > p.radius) {
this.seek(target);
}
// Draw the debugging stuff
if (debug) {
fill(200);
stroke(200);
line(this.position.x, this.position.y, predictLoc.x, predictLoc.y);
ellipse(predictLoc.x, predictLoc.y, 4, 4);
// Draw normal location
fill(200);
stroke(200);
line(predictLoc.x, predictLoc.y, normalPoint.x, normalPoint.y);
ellipse(normalPoint.x, normalPoint.y, 4, 4);
stroke(200);
if (distance > p.radius) fill(255, 0, 0);
noStroke();
ellipse(target.x + dir.x, target.y + dir.y, 8, 8);
}
}
applyForce(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
seek(target) {
let desired = p5.Vector.sub(target, this.position); // A vector pointing from the position to the target
// If the magnitude of desired equals 0, skip out of here
// (We could optimize this to check if x and y are 0 to avoid mag() square root
if (desired.mag() === 0) return;
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(this.maxspeed);
// Steering = Desired minus Velocity
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce); // Limit to maximum steering force
this.applyForce(steer);
}
// Method to update position
update() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset accelerationelertion to 0 each cycle
this.acceleration.mult(0);
}
// Wraparound
borders(p) {
if (this.position.x > p.end.x + this.r) {
this.position.x = p.start.x - this.r;
this.position.y = p.start.y + (this.position.y - p.end.y);
}
}
display() {
// Draw a triangle rotated in the direction of velocity
let theta = this.velocity.heading() + PI / 2;
fill(127);
stroke(200);
strokeWeight(1);
push();
translate(this.position.x, this.position.y);
rotate(theta);
beginShape();
vertex(0, -this.r * 2);
vertex(-this.r, this.r * 2);
vertex(this.r, this.r * 2);
endShape(CLOSE);
pop();
}
}
// A function to get the normal point from a point (p) to a line segment (a-b)
// This function could be optimized to make fewer new Vector objects
function getNormalPoint(p, a, b) {
// Vector from a to p
let ap = p5.Vector.sub(p, a);
// Vector from a to b
let ab = p5.Vector.sub(b, a);
ab.normalize(); // Normalize the line
// Project vector "diff" onto line by using the dot product
ab.mult(ap.dot(ab));
let normalPoint = p5.Vector.add(a, ab);
return normalPoint;
}