From 993442813dbee7805ef18146113872cae5892e99 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 27 Jun 2017 14:43:16 +0100 Subject: Initial Commit --- .gitignore | 1 + Readme | 3 +++ index.html | 12 ++++++++++++ particle.js | 30 ++++++++++++++++++++++++++++++ sketch.js | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 Readme create mode 100755 index.html create mode 100644 particle.js create mode 100644 sketch.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/Readme b/Readme new file mode 100644 index 0000000..bf47f6c --- /dev/null +++ b/Readme @@ -0,0 +1,3 @@ +Just a simple physics simulator. + +Mostly ripped off from Coding Rainbow. diff --git a/index.html b/index.html new file mode 100755 index 0000000..c28cb6e --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + diff --git a/particle.js b/particle.js new file mode 100644 index 0000000..66f4f6e --- /dev/null +++ b/particle.js @@ -0,0 +1,30 @@ +function Particle(x, y, m, c) { + this.pos = createVector(x, y); + this.vel = createVector(0, 0); + this.acc = createVector(0, 0); + this.colour = c; + this.mass = m; + + this.applyForce = function(force) { + this.acc.add(p5.Vector.div(force, this.mass)); + } + + this.update = function() { + this.vel.add(this.acc); + this.pos.add(this.vel); + this.acc.mult(0); + + if (this.pos.x <= 0 || this.pos.x >= wid) + this.vel.x = -this.vel.x; + if (this.pos.y <= 0) + this.vel.y = -this.vel.y; + } + + this.show = function() { + point(this.pos.x, this.pos.y); + } + + this.gone = function() { + return this.pos.y >= hei; + } +} diff --git a/sketch.js b/sketch.js new file mode 100644 index 0000000..3cce527 --- /dev/null +++ b/sketch.js @@ -0,0 +1,36 @@ +var ps = []; +var gravity; +var wid = 800; +var hei = 600; + +function setup() { + createCanvas(wid, hei); + colorMode(HSL); + gravity = createVector(0, 5); +} + +function draw() { + background(0); + + for(var i = ps.length - 1; i >= 0; i--) { + ps[i].applyForce(gravity); + ps[i].update(); + + stroke(ps[i].colour); + strokeWeight(ps[i].mass); + ps[i].show(); + + if (ps[i].gone()) { + ps.splice(i, 1); + } + } + + if (mouseIsPressed) { + var p = new Particle( + mouseX, mouseY, random(2, 8), + color(random(0, 360), random(90, 100), random(50, 100)) + ); + p.applyForce(createVector(random(-40, 40), random(-150, 0))); + ps.push(p); + } +} -- cgit v1.2.1