aboutsummaryrefslogtreecommitdiff
path: root/sketch.js
blob: 126edee678febfbc4ac8859eda260f72ae3dbceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var ps = [];
var gravity;
var air_resistance = 0.4;
var wid = 800;
var hei = 600;

function setup() {
  createCanvas(wid, hei);
  colorMode(HSL);
  gravity = createVector(0, 5);
}

function draw() {
  background(0);

  stroke(255);
  strokeWeight(4);
  line(0, 0.75*hei, 0.25*wid, hei);
  line(0.75*wid, hei, wid, 0.75*hei);

  for(var i = ps.length - 1; i >= 0; i--) {
    ps[i].applyForce(gravity);
    ps[i].applyDrag(air_resistance);
    ps[i].update();

    stroke(ps[i].colour);
    strokeWeight(ps[i].mass);
    ps[i].show();

    if (ps[i].online(0, 0.75*hei, 0.25*wid, hei)) {
      ps[i].reflect(createVector(0.25*wid, 0.25*hei), 5);
    } else if (ps[i].online(0.75*wid, hei, wid, 0.75*hei)) {
      ps[i].reflect(createVector(0.25*wid, -0.25*hei), 5);
    }

    if (ps[i].pos.y >= hei)
      ps.splice(i, 1);
  }

  if (mouseIsPressed) {
    var force = p5.Vector.fromAngle(random(PI, 2*PI));
    force.mult(random(250));
    var p = new Particle(
          mouseX, mouseY, random(2, 10),
          color(random(360), random(90, 100), random(50, 100))
        );
    p.applyForce(force);
    ps.push(p);
  }
}