From 2fe64c1beae121fee681912420667a38facc2830 Mon Sep 17 00:00:00 2001 From: shiffman Date: Sat, 6 Apr 2024 21:41:07 +0000 Subject: [PATCH] Notion - Update docs --- content/04_particles.html | 7 ++++--- content/05_steering.html | 4 ++-- content/07_ca.html | 12 +++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/content/04_particles.html b/content/04_particles.html index 03da975..3f7e274 100644 --- a/content/04_particles.html +++ b/content/04_particles.html @@ -910,12 +910,14 @@ for (let animal of kingdom) { emitter.run(); }

Of course, if I call an applyForce() method on the Emitter object in draw(), I then have to define that method in the Emitter class. In English, that method needs to be able to receive a force as a p5.Vector and apply that force to all the particles. Here’s the translation into code:

-
  applyForce(force) {
+
+
  applyForce(force) {
     for (let particle of this.particles) {
       particle.applyForce(force);
     }
   }
-

It almost seems silly to write this method. The code is essentially saying, “Apply a force to a particle system so that the system can apply that force to all the individual particles.” Although this may sound a bit roundabout, this approach is quite reasonable. After all, the emitter is in charge of managing the particles, so if you want to talk to the particles, you’ve got to talk to them through their manager. (Also, here’s a chance to use an enhanced for...of loop, since no particles are being deleted!)

+
+

It almost seems silly to write this method. The code is essentially saying, “Apply a force to a particle system so that the system can apply that force to all the individual particles.” Although this may sound a bit roundabout, this approach is quite reasonable. After all, the emitter is in charge of managing the particles, so if you want to talk to the particles, you’ve got to talk to them through their manager. (Also, here’s a chance to use a for...of loop, since no particles are being deleted!)

Here’s the full example, including this change. (The code assumes the existence of the Particle class written earlier; there’s no need to show it again, since nothing has changed.)

Example 4.6: A Particle System with Forces

@@ -940,7 +942,6 @@ function draw() { emitter.run(); } - class Emitter { constructor(x, y) { this.origin = createVector(x, y); diff --git a/content/05_steering.html b/content/05_steering.html index 5f5ff9c..026022f 100644 --- a/content/05_steering.html +++ b/content/05_steering.html @@ -1197,7 +1197,7 @@ function draw() {

Modify Example 5.10 so that the behavior weights change over time. For example, what if the weights were calculated according to a sine wave or Perlin noise? Or what if some vehicles are more concerned with seeking and others are more concerned with separating? Can you introduce other steering behaviors as well?

Flocking

-

Flocking is a group animal behavior found in many living creatures, such as birds, fish, and insects. In 1986, Reynolds created a computer simulation of flocking behavior and documented the algorithm in his paper “Flocks, Herds, and Schools: A Distributed Behavioral Model.” Re-creating this simulation in p5.js will bring together all the concepts in this chapter:

+

Flocking is a group animal behavior found in many living creatures, such as birds, fish, and insects.
In 1986, Reynolds created a computer simulation of flocking behavior and documented the algorithm in his paper “Flocks, Herds, and Schools: A Distributed Behavioral Model.” Re-creating this simulation in p5.js will bring together all the concepts in this chapter:

  1. I will use the steering force formula (steer = desired – velocity) to implement the rules of flocking.
  2. These steering forces will be group behaviors and will require each vehicle to perceive all the other vehicles.
  3. @@ -1232,7 +1232,7 @@ function draw() { this.applyForce(alignment); this.applyForce(cohesion); }
-

Now, it’s just a matter of implementing the three rules. I applied separation already; it’s identical to the previous example. Instead, I’ll focus on alignment, or steering in the same direction as the neighboring boids. As with all other steering behaviors, I have to express this concept as a desire: the boid’s desired velocity is the average velocity of its neighbors. The algorithm is therefore to calculate the average velocity of all the other boids and set that to the desired velocity:

+

Now, it’s just a matter of implementing the three rules. I applied separation already; it’s identical
to the previous example. Instead, I’ll focus on alignment, or steering in the same direction as the neighboring boids. As with all other steering behaviors, I have to express this concept as a desire: the boid’s desired velocity is the average velocity of its neighbors. The algorithm is therefore to calculate the average velocity of all the other boids and set that to the desired velocity:

  align(boids) {
     // Add up all the velocities and divide by the total
     // to calculate the average velocity.
diff --git a/content/07_ca.html b/content/07_ca.html
index f937ebc..f377a69 100644
--- a/content/07_ca.html
+++ b/content/07_ca.html
@@ -743,11 +743,13 @@ board = next;

Exercise 7.11

Create a CA in which each pixel is a cell and the pixel’s color is its state.

-

Historical

-

In the object-oriented Game of Life example, I used two variables to keep track of a cell’s current and previous states. What if you use an array to keep track of a cell’s state history over a longer period? This relates to the idea of a complex adaptive system, one that has the ability to change its rules over time by learning from its history. (Stay tuned for more on this concept in Chapters 9 and 10.)

-
-

Exercise 7.12

-

Visualize the Game of Life by coloring each cell according to the amount of time it has been alive or dead. Can you also use the cell’s history to inform the rules?

+
+

Historical

+

In the object-oriented Game of Life example, I used two variables to keep track of a cell’s current and previous states. What if you use an array to keep track of a cell’s state history over a longer period? This relates to the idea of a complex adaptive system, one that has the ability to change its rules over time by learning from its history. (Stay tuned for more on this concept in Chapters 9 and 10.)

+
+

Exercise 7.12

+

Visualize the Game of Life by coloring each cell according to the amount of time it has been alive or dead. Can you also use the cell’s history to inform the rules?

+

Moving Cells

In these basic examples, cells have a fixed position on a grid, but you could build a CA with cells that have no fixed position and instead move about the canvas.