Note

Turning the random walker from last week into a vector.

Screen Recording 2024-02-07 at 7.38.23 PM.mov


let r;
let g;
let b;
let a;

let position;

function setup() {
  createCanvas(400, 400);
  position = createVector(random(width / 2), random(height / 2));
  background(20);

  r = random(255);
  g = random(255);
  b = random(255);
  a = random(255);
}

function draw() {
  constrain(r, 0, 255);
  constrain(g, 100, 255);
  constrain(b, 200, 255);
  constrain(a, 50, 255);

  stroke(r, g, b, a);
  point(position.x, position.y);
  position.x = position.x + random(-1, 1);
  position.y = position.y + random(-1, 1);
}

//reference:
// Daniel Shiffman
//Happy Coding
// Kevin Workman <https://www.youtube.com/watch?v=yBVeCXnxQYs>

Turn a bouncing ball into Vector

https://editor.p5js.org/jc11932/sketches/NOKUeA-L_

I added acceleration to its movement.

I added acceleration to its movement.

let mover;

function setup() {
  createCanvas(400, 400);
  mover = new Mover(200, 200);
}

function draw() {
  background(220);
  mover.update();
  mover.show();
}

class Mover {
  constructor(x, y) {
    this.pos = createVector(x, y);
    this.v = p5.Vector.random2D();
    this.v.mult(random(3));
  }

  update() {
    this.acc = p5.Vector.random2D();
    this.acc.setMag(3);
    this.v.add(this.acc);
    if (this.pos.x > width || this.pos.x < 0) {
      this.v.x = this.v.x * -1;
    }
    if (this.pos.y > height || this.pos.y < 0) {
      this.v.y = this.v.y * -1;
    }
    this.v.limit(150);

    this.pos.add(this.v);
  }
  show() {
    stroke(255);
    strokeWeight(2);
    fill(255, 100);
    ellipse(this.pos.x, this.pos.y, 20);
  }
}

// reference from the Coding Train & The Nature Of Code, Daniel Shiffman

Question: How to make it into a 3D ball bouncing inside a 3D Box?

Then I changed the color and made an array of balls.

https://editor.p5js.org/jc11932/sketches/OEnrQJow7

Screen Recording 2024-02-08 at 3.16.52 AM.mov