• WORK
  • ABOUT
  • LABS
  • BLOG
Menu

WAWA LABS

  • WORK
  • ABOUT
  • LABS
  • BLOG
Screen Shot 2020-09-17 at 12.37.35 PM.png

Gravity Force & Wind Force & Array

February 25, 2020

p5 editor

// var attractors
var particles = []
let value = 0
var r
var g
var b
function setup() {
  createCanvas(640, 360);
}

function mousePressed() {
  var p = new Particle(mouseX, mouseY, random(3, 7))
  particles.push(p);
  r = random(255);
  g = random(255);
  b = random(255);
}


//draw particles
function draw() {
  background(0);


  for (i = 0; i < particles.length; i++) {
    var gravity = createVector(0, 0.1 * particles[i].mass);
    var wind = createVector(0.05 * particles[i].mass,0);
 

    particles[i].applyForce(gravity);
    particles[i].applyForce(wind);
    

    particles[i].display();
    particles[i].update();
    particles[i].edges();
  }
}



// create Walker class
function Particle(x, y, m) {


  //set up pos,vel,acc,mass
  this.pos = createVector(x, y);
  this.vel = createVector(0, 0);
  this.acc = createVector(0, 0);
  this.mass = m

  //display
  this.display = function() {
    fill(r,g,b);
    ellipse(this.pos.x, this.pos.y, this.mass * 3, this.mass * 3);
  }


  //FORCE
  this.applyForce = function(force) {
    var f = force.copy();
    f.div(this.mass)
    this.acc.add(f)
  }


  //physics engine
  this.update = function() {

    var mouse = createVector(mouseX, mouseY);


    this.vel.add(this.acc);
    this.pos.add(this.vel);
    this.acc.set(0, 0);
  }





  //create border
  this.edges = function() {
    if (this.pos.y > height - this.mass * 3 / 2) {
      this.vel.y *= -1
      this.pos.y = height - this.mass * 3 / 2;
    }

    if (this.pos.x > width - this.mass * 3 / 2) {
      this.vel.x *= -1
      this.pos.x = width - this.mass * 3 / 2;
    }

    if (this.pos.y - this.mass * 3 < 0) {
      this.vel.y *= -1
      this.pos.y = this.mass * 3;
    }

    if (this.pos.x < this.mass * 3 / 2) {
      this.vel.x *= -1
      this.pos.x = this.mass * 3 / 2;
    }
  }


}



← Simulation projectAcceleration & Physics →

© 2021 All Rights Reserved