diff --git a/content/05_steering.html b/content/05_steering.html index 9bd26cc..3ec5319 100644 --- a/content/05_steering.html +++ b/content/05_steering.html @@ -627,12 +627,14 @@ let n = a.dot(b);
let a = createVector(10, 2); let b = createVector(4, -3); let angle = acos(a.dot(b) / (a.mag() * b.mag()));-
Turns out, if you again dig into the guts of the p5.js source code, you’ll find a method that implements this exact algorithm:
-angleBetween(v) { +Turns out, if you again dig into the guts of the p5.js source code, you’ll find a method called
+angleBetween
that implements this exact algorithm.+angleBetween(v) { let dot = this.dot(v); let angle = Math.acos(dot / (this.mag() * v.mag())); return angle; }+Sure, I could have told you about this
angleBetween()
method to begin with, but understanding the dot product in detail will better prepare you for the upcoming path-following examples and help you see how the dot product fits into a concept called scalar projection.Exercise 5.9
@@ -762,13 +764,15 @@ let normalPoint = p5.Vector.add(path.start, b);Figure 5.26: A vehicle with a future position on the path (top) and one that’s outside the path (bottom) -I can encode that logic with a simple
-if
statement and use my earlierseek()
method to steer the vehicle when necessary:let distance = p5.Vector.dist(future, normalPoint); +I can encode that logic with a simple
+if
statement and use my earlierseek()
method to steer the vehicle when necessary.+let distance = p5.Vector.dist(future, normalPoint); // If the vehicle is outside the path, seek the target. if (distance > path.radius) { //{!1} The desired velocity and steering force can use the+seek()
method created in Example 5.1. this.seek(target); }But what’s the target that the path follower is seeking? Reynolds’s algorithm involves picking a point ahead of the normal on the path. Since I know the vector that defines the path (\vec{B}), I can implement this point ahead by adding a vector that points in \vec{B}’s direction to the vector representing the normal point, as in Figure 5.27.