From f095ed2095ecebbf340566cd7872e91428405f1c Mon Sep 17 00:00:00 2001 From: Aakash Varier Date: Wed, 26 May 2021 20:59:37 +0530 Subject: [PATCH] Three body simulation --- three body/attractor.js | 25 ++++++++++++++++++++++ three body/particle.js | 46 ++++++++++++++++++++++++++++++++++++++++ three body/sketch.js | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 three body/attractor.js create mode 100644 three body/particle.js create mode 100644 three body/sketch.js diff --git a/three body/attractor.js b/three body/attractor.js new file mode 100644 index 0000000..e5c3e56 --- /dev/null +++ b/three body/attractor.js @@ -0,0 +1,25 @@ +class attractor { + constructor(m) { + this.m = m; + this.pos = createVector(0, 0); + this.r = sqrt(this.m) * 2; + this.G = 0.3; + } + attract(particle) { + let dir = p5.Vector.sub(this.pos, particle.pos); + + let mag = constrain(dir.magSq(), 100, 1000); + let force = (this.G * this.m * particle.m) / mag; + dir.setMag(force); + particle.applyforce(dir); + } + display() { + stroke(255); + ellipse(this.pos.x, this.pos.y, this.r * 2, this.r * 2); + } + applyforce(Force) { + // let f = Force.copy(); + // f.div(this.m); + this.acc.add(Force); + } +} diff --git a/three body/particle.js b/three body/particle.js new file mode 100644 index 0000000..0bbe3a9 --- /dev/null +++ b/three body/particle.js @@ -0,0 +1,46 @@ +class Particle { + constructor(x, y, m) { + this.acc = createVector(0, 0); + this.vel = p5.Vector.random2D(); + this.vel.mult(random(50, 100)); + // this.vel.limit(10); + // this.vel.rotate(90); + this.vel.limit(5); + this.pos = createVector(random(-ww / 2, ww / 2), random(-wh / 2, wh / 2)); + this.dx = x; + this.dy = y; + this.m = m; + this.G = 0.3; + } + update() { + this.vel.add(this.acc); + this.pos.add(this.vel); + this.acc.set(0, 0); + } + display() { + stroke(255); + strokeWeight(10); + ellipse(this.pos.x, this.pos.y, this.dx, this.dy); + } + applyforce(Force) { + // let f = Force.copy(); + // f.div(this.m); + this.acc.add(Force); + } + attract(particle) { + let dir = p5.Vector.sub(this.pos, particle.pos); + + let mag = constrain(dir.magSq(), 100, 1000); + let force = (this.G * this.m * particle.m) / mag; + dir.setMag(force); + particle.applyforce(dir); + } + bounce() { + if (this.pos.x > ww / 2 || this.pos.x < -ww / 2) { + this.vel.x = -1 * this.vel.x; + } + if (this.pos.y > wh / 2 || this.pos.y < -wh / 2) { + this.vel.y = -1 * this.vel.y; + } + } +} diff --git a/three body/sketch.js b/three body/sketch.js new file mode 100644 index 0000000..3ea0360 --- /dev/null +++ b/three body/sketch.js @@ -0,0 +1,47 @@ +let ww = window.innerWidth; +let wh = window.innerHeight; +// let m = 0; +// let n = 1; + +function setup() { + createCanvas(ww, wh); + p1 = new Particle(10, 10, 50); + p2 = new Particle(10, 10, 50); + q = new attractor(10); +} + +function draw() { + translate(ww / 2, wh / 2); + p1.display(); + p2.display(); + // let gravity = createVector(0, 0.5); + // let gravityForce = p5.Vector.mult(gravity, p.m); + // let wind = createVector( + // random(-noise(m), noise(m)) / 3, + // random(-noise(n), noise(n)) / 3 + // ); + background(0); + // p.applyforce(gravityForce); + // p.applyforce(wind); + p1.update(); + p2.update(); + // p1.bounce(); + // p2.bounce(); + p1.display(); + p2.display(); + p1.attract(p2); + // p1.attract(q); + p2.attract(p1); + // p2.attract(q); + q.attract(p1); + q.attract(p2); + // q.display(); + + // n += 0.001; + // m += 0.001; + if (mouseIsPressed) { + q.pos.x = mouseX - ww / 2; + q.pos.y = mouseY - wh / 2; + } + q.display(); +}