Brandon Roots
  • INTERACTIVE
  • FILM
  • ABOUT
  • CONTACT
February 12, 2021

Vectors

broots ITP, Nature of Code

This week we learned about vectors in P5.js which are useful for calculating position and movement values in 2D and 3D space. As practice I rewrote code from my previous Random Walk assignment to use vectors to store coordinates. I also restructured the code below to make use of a Tangle class to generate objects and move them around the screen.

Additionally I am interested in adding a third dimension to this project by enabling WEBGL and adding Z-axis coordinate to the vectors however I am unsure of the best way to approach rendering these shapes in P5.js. The examples I have found to generate 3D shapes using WEBGL make us of primitives such as spheres, cubes, or planes. Ideally I think I would store these shapes as splines, perhaps using the beginShape(), endShape(), and curveVertex() functions. Hopefully I can incorporate something like this as practice into the next assignment about forces.

let position1;
let position2;


function setup() {
  createCanvas(windowWidth, windowHeight);
  background(25);
  position1 = new Tangle(0, 1000, 0, 0.002, 0.01, 25, 1);
  position2 = new Tangle(333, 33333, 33, 0.001, 0.005, 3, 0);
}

function draw() {
  blendMode(LIGHTEST);
  position1.move();
  position1.display();
  
  position2.move();
  position2.display();
  
}

// Tangle object
class Tangle {
  constructor(xoff, yoff, boff, posChange, bChange, scale, mirror)   {
    // position offset
    this.poff = createVector(xoff, yoff);
    // brightness offset
    this.boff = boff;
    this.posChange = createVector(posChange, posChange);
    this.bChange = bChange;
    this.scale = scale;
    this.mirror = mirror;
  }
  
  move(){
    this.poff.x += this.posChange.x;
    this.poff.y += this.posChange.y;
    this.boff += this.bChange;
  }
  
  display(){
    let x = map(noise(this.poff.x), 0, 1, 0, windowWidth);
    let y = map(noise(this.poff.y), 0, 1, 0, windowHeight);
    let b = map(noise(this.boff), 0, 1, 0, 255);
    let d1 = int(dist(x, y, windowWidth-x, windowHeight-y));
    d1 = map(d1, 1, max(windowWidth, windowHeight), this.scale, 1);
    noStroke();
    fill(b);
    ellipse(x, y, d1, d1);
    // Mirror function
    if(this.mirror){
      ellipse(windowWidth-x, windowHeight-y, d1, d1);
    }
  }
}
Random Walk Kinetic and Measurement Project Ideas

Related Posts

Fractal Plant – Foiled by  Registers

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Foiled by Registers

Since receiving the PCBs and successfully soldering the board together I have been trying to rewrite code for the I2C port expander. This has been immensely difficult! The Inkplate Arduino Library makes considerable use of an “Mcp” class, which is written to work with the MCP23017 GPIO expander IC. These chips are quite difficult to […]

“Handling” Playtest Week

Handling, ITP

“Handling” Playtest Week

Last week we attended “Playtest Thursday” on the second floor of 370 Jay St with our games. I came away from the experience with some very specific feedback. Seeing a number of people play the game showed me things I didn’t anticipate. Some folks approached the cabinet and immediately treated it as a touch screen. […]

Fractal Plant – Beta Build

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Beta Build

The boards arrived! Amazingly within an hour of one another. Based on the experience I think that JLCPCB is a better value. With shipping OSHPark was $55.50 for 3 boards. JLCPCB was $26.36 for 10 boards. Aside from a higher cost OSHPark also left sharp bits of tabs around the edges of the boards which […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • “Handling” Playtest Week“Handling” Playtest Week
    May 5, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
Brandon Roots