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

Forces

broots ITP, Nature of Code

This week in The Nature of Code I learned about techniques for simulating forces in P5.js using vectors and principles from physics.

In brainstorming for this assignment the indie game LIMBO was a strong inspiration for the look and feel.

Expanding on the in class demo of Gravitational Attraction I set out to write a sketch reminiscent of planets orbiting a star. This sketch uses the WEBGL renderer to generate a pulsing “star” from point lights on a plane in the middle of the screen. Clicking on the canvas generates planet objects that are attracted to the star at the center. To emulate the fall-off characteristics of light on the planets their brightness values are calculated based on their distance from the center.

An additional function called collisionCheck() loops through the planets array and combines planet objects when a collision is detected.

function collisionCheck(i){
  // Collision
  // confirm there are more than one planet object
  if(planets.length > 1){
    for(let a = i+1; a < planets.length; a++){
      let distance = p5.Vector.dist(planets[a].pos, planets[i].pos);
      // check that planets objects are touching
      if(distance < planets[a].r + planets[i].r){
        // combine two objects in array
        planets[a].vel = p5.Vector.add(planets[a].vel, planets[i].vel);
        planets[a].vel.div(2);
        planets[a].acc = p5.Vector.add(planets[a].acc, planets[i].acc);
        planets[a].acc.div(2);
        // combine mass of two objects
        planets[a].mass += planets[i].mass;
        // approximate math for calculating new radius
        planets[a].r = (planets[i].r + planets[a].r)/1.7;
        // play tone
        planets[a].env.play();
        // delete second object from array
        planets.splice(i, 1);
      }
    }
  }
}

In this sketch planet objects generate a tone. When a planet object is first created it is randomly assigned a frequency from the preset scale array based on the Lydian Mode G Scale. Passing close enough to the star attractor at the center of screen or colliding with another planet object also triggers tone playback.

I have hidden some other functions into the sketch below! Touch is enabled as well so it will run on a smartphone or tablet.

Kinetic and Measurement Project Ideas Oscillation

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