DMSC Devlog: Week 6 & 7 - Climate Change


I have been hung up on the work from these two weeks for, well, more weeks. What started as a desire to make a plant grow and decay in day/night cycles turned into hours of finagling with vector geometry and collision detection and relearning eighth grade science. I’ve been stuck for a while, but my professor insisted I document both my successes and failures, so I’ll do what any other green and well-intentioned computer artist would do and tell the internet what I did wrong.

Our assignment: create a digital ecosystem made of multiple interacting entities, a la Conway’s “Game of Life” or physics sandboxes like The Powder Toy. It needs at least one class, several forces (including an “attraction force”), and random components that create a different simulation on every run.

My interpretation of these guidelines (from which I quickly lost the plot) was a Garden of Dreams, where each seed would grow when “fed” phrases or photos and wither into their own dreams when overgrown. I would need gravity to affect the seeds and “plant food” and, if each grown plant was comprised of individual seeds before it decays, a force that holds the plant together while it sprouts. And I guess I can take the wind force from the reference simulations to make it more polished. We’ll get there when we get there, I tell myself.

My immediate focus went into fixing the way gravity functioned in our in-class demos. As a reference, we were given sketches adapted from Daniel Shiffman’s “The Nature of Code,” in which forces were applied to an object’s acceleration and objects with more mass were less affected by the forces applied to it.

applyForce(force) {
    var f = p5.Vector.div(force, this.mass);
    this.acceleration.add(f);
  }

This worked for wind and water simulations, but made things fall inconsistently (as objects with more mass usually fall faster). Through trial and error, I took a solution that gives each “mover” its own gravity based on its mass -

let gravity = createVector(0, 0.1);
let gravityA = p5.Vector.mult(gravity, moverA.mass);
  moverA.applyForce(gravityA);
let gravityB = p5.Vector.mult(gravity, moverB.mass);
  moverB.applyForce(gravityB);
  • into an array of more realistic gravities.
    mover[i].applyForce(gravity);

So now my bouncing demo balls bounce properly. Great. Since I was already thinking of making the flowers decay into balls of dirt, I shifted my attention to making a ball pit of brown dirts to use as a garden bed. Using my collision trials with Alfie from the week prior, I tried (and failed repeatedly) to make the dirts satisfyingly form a body that could sit still for two seconds. It wasn’t until I sifted through the p5.js Reference manually that I found the lerp() and slerp() functions, which can find an average vector between two others (like determining a change in velocity from the angle of two objects colliding). Two practice sketches later, and my brown ball pit clumps satifyingly!

for (let i=0;i<d.length;i++) {
      if (i != d.indexOf(this)) {
        if (this.position.dist(d[i].position) < this.radius+d[i].radius) {
          let vectorBetween = p5.Vector.sub(this.position,d[i].position);
          let vM = this.velocity.mag();
          vectorBetween.setMag(vM);
          this.velocity.setMag(0);
          this.velocity.lerp(vectorBetween,0.5);
          let vA = this.velocity.mag()
          vectorBetween.setMag(vA);
          this.acceleration.setMag(0);
          this.acceleration.lerp(vectorBetween,0.5);
	}
      }
}

Finally, I made the dirts change color based on their mass -

this.color = color(200-(this.mass*2),130-(this.mass*2),150-(this.mass*4))
  • and added a condition for the dirts to transform into seeds: if a dirt is touching enough other dirts (determined using an extra array in the collision() function) and has enough mass, its coordinates are put into the Flower object array so it can grow.
if (this.collisionCount > 4 && this.mass > 30 && this.transformed == false) {
      this.timeToTransform -= 1;
    } else {
      this.timeToTransform = 300;
    }
    if (this.timeToTransform == 0) {
      this.transformed = true;
      // print(i + " transforms!");
      flowers.push(new Flower(d[i].position.x,d[i].position.y));
      this.color = color("green");
    }

And now I’ve totally lost the plot. At this point I knew I was doing something with dirt and daylight cycles and plant growth, but I got so carried away with programming physics that the actual assignment faded from memory. In retrospect, though, I have the bones of a project here! With a little leniency from my professor, I think I can turn this into a complete ecosystem (and something of a midterm grade) over the next few weeks.

Wish me luck!

-Rafi

Leave a comment

Log in with itch.io to leave a comment.