From cd02ec7282c2a177d8502bdc9c682d1e983cefc9 Mon Sep 17 00:00:00 2001 From: shiffman Date: Sat, 24 Feb 2024 19:09:34 +0000 Subject: [PATCH] Notion - Update docs --- content/05_steering.html | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) 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)
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 earlier seek() 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 earlier seek() 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.

Figure 5.27: The target is 25 pixels (an arbitrary choice) ahead of the normal point along the path. @@ -1519,7 +1523,7 @@ for (let i = 0; i < grid.length; i++) {

Each of these tips is detailed next.

Use the Magnitude Squared

-

What is magnitude squared, and when should you use it? Think back to how the magnitude of a vector is calculated:

+

What is magnitude squared, and when should you use it? Think back to how the magnitude of a vector is calculated.

function mag() {
   return sqrt(x * x + y * y);