Sketch – Week 8

Concept

For this week’s assignment, I wanted to investigate the code for the wandering behaviour that we covered in class. I found it interesting how the direction of wander isn’t completely random, rather guided in one direction for a bit and then another. When I thought of what in nature wanders, I thought of tadpoles. I wanted to create a realistic tadpole, with its realism enhanced by the wandering behaviour. Working of the code from class, I tweaked some of the numbers and added edges, so that the tadpole can’t escape the bounds of the canvas.

Code Snippet

edges() {
  let pushed = false;

  // Check each boundary, reverse direction, and add a force toward the center if needed
  if (this.pos.x > width - this.r) {
    this.pos.x = width - this.r;
    this.vel.x *= -1;
    pushed = true;
  } else if (this.pos.x < this.r) {
    this.pos.x = this.r;
    this.vel.x *= -1;
    pushed = true;
  }

  if (this.pos.y > height - this.r) {
    this.pos.y = height - this.r;
    this.vel.y *= -1;
    pushed = true;
  } else if (this.pos.y < this.r) {
    this.pos.y = this.r;
    this.vel.y *= -1;
    pushed = true;
  }

  // If a boundary was hit, apply a slight push toward the canvas center
  if (pushed) {
    let center = createVector(width / 2, height / 2);
    let directionToCenter = p5.Vector.sub(center, this.pos);
    directionToCenter.setMag(0.5); // Set the magnitude of the push
    this.applyForce(directionToCenter);
  }
}

Embedded Sketch

Reflections

I like the visual effect of my code and I enjoyed learning more about how to modify the wandering behaviour. I struggled the most with creating the boundary for the canvas, and making sure the tadpole didn’t get stuck and keep bouncing on the boundary. With more time, I would incorporate more into the code, such as mousePressed(), to make the project more interactive and experiment more with wander.

Leave a Reply

Your email address will not be published. Required fields are marked *