aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-06-27 14:43:16 +0100
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-06-27 14:43:16 +0100
commit993442813dbee7805ef18146113872cae5892e99 (patch)
treea5994824818eabc55765ac995a8262d0ef2be62f
Initial Commit
-rw-r--r--.gitignore1
-rw-r--r--Readme3
-rwxr-xr-xindex.html12
-rw-r--r--particle.js30
-rw-r--r--sketch.js36
5 files changed, 82 insertions, 0 deletions
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 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script>
+ <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js"></script>
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.sound.min.js"></script>-->
+ <script src="particle.js"></script>
+ <script src="sketch.js"></script>
+ </head>
+ <body>
+ </body>
+</html>
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);
+ }
+}