Created
March 7, 2017 14:46
-
-
Save wenqian-hu/59cdd2f17eb983ff9545324fbb8a9dac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is the Sketch | |
var planet; | |
var blackhole; | |
var particles = []; | |
function setup() { | |
createCanvas(1000, 950); | |
planet = new Planet(300, 300, 100); | |
blackhole = new Blackhole(-300, -300, 100); | |
for (var i = 0; i < 100; i++) { | |
particles.push(new Particle(random(width), random(height))); | |
} | |
} | |
function draw() { | |
background(255); | |
push(); | |
translate(width / 2, height / 2); | |
//for the blackhole | |
for (var i = 0; i < particles.length; i++) { | |
var p = particles[i]; | |
//gravity | |
var gravity = p5.Vector.sub(blackhole.pos, p.pos); | |
gravity.normalize(); | |
gravity.mult(blackhole.cGravity * p.mass); | |
p.applyForce(gravity); | |
var distance = p.pos.dist(blackhole.pos); | |
//blackhole | |
if (distance < blackhole.rad) { | |
var resistance = p5.Vector.mult(p.vel, -1); | |
resistance.normalize(); | |
var speed = p.vel.mag(); | |
resistance.mult(speed * speed * blackhole.cWaterResistance); | |
resistance.limit(speed); | |
// console.log(resistance); | |
p.applyForce(resistance); | |
} | |
} | |
for (var i = 0; i < particles.length; i++) { | |
var p = particles[i]; | |
// gravity | |
var gravity = p5.Vector.sub(planet.pos, p.pos); | |
gravity.normalize(); | |
gravity.mult(planet.cGravity * p.mass); | |
p.applyForce(gravity); | |
var distance = p.pos.dist(planet.pos); | |
// core | |
if (distance < planet.coreRad) { | |
p.vel.mult(planet.cCoreRestitution); | |
} | |
// water | |
else if (distance < planet.waterRad) { | |
var resistance = p5.Vector.mult(p.vel, -1); | |
resistance.normalize(); | |
var speed = p.vel.mag(); | |
resistance.mult(speed * speed * planet.cWaterResistance); | |
resistance.limit(speed); | |
p.applyForce(resistance); | |
} | |
// air | |
else if (distance < planet.rad) { | |
var resistance = p5.Vector.mult(p.vel, -1); | |
resistance.normalize(); | |
var speed = p.vel.mag(); | |
resistance.mult(speed * speed * planet.cAirResistance); | |
resistance.limit(speed); | |
p.applyForce(resistance); | |
// wind | |
var wind = p.vel.copy(); | |
wind.normalize(); | |
wind.rotate(radians(60)); | |
wind.mult(planet.cWindMagnitude); | |
p.applyForce(wind); | |
} | |
p.update(); | |
p.display(); | |
} | |
planet.update(); | |
planet.display(); | |
blackhole.update(); | |
blackhole.display(); | |
pop(); | |
} | |
function mousePressed(){ | |
// console.log('mouse'); | |
var count = 0; | |
for (var i = 0; i < particles.length; i++) { | |
var p = particles[i]; | |
var distance = p.pos.dist(blackhole.pos); | |
if(distance < blackhole.rad){ | |
count += 1; | |
p.pos = createVector(random(270, 330), random(270, 330)); | |
p.vel = createVector(random(-5,5), random(-5,5)); | |
} | |
} | |
console.log('Number in black hole: ' + count); | |
} | |
//This is Particles.js | |
"use strict"; | |
class Particle { | |
constructor(x, y) { | |
this.pos = createVector(x, y); | |
this.vel = createVector(random(-5,5), random(-5,5)); | |
this.acc = createVector(); | |
this.dia = 2; | |
this.mass = random(1, 10); | |
} | |
applyForce(f) { | |
f.div(this.mass); | |
this.acc.add(f); | |
} | |
update() { | |
this.vel.add(this.acc); | |
this.pos.add(this.vel); | |
this.acc.mult(0); | |
} | |
display() { | |
push(); | |
translate(this.pos.x, this.pos.y); | |
noStroke(); | |
fill(225,248,249); | |
ellipse(0, 0, this.dia * this.mass, this.dia * this.mass); | |
pop(); | |
} | |
} | |
//This is Blackhole.js | |
"use strict"; | |
class Blackhole{ | |
constructor(x,y,radius){ | |
this.pos = createVector(x,y); | |
this.rad = radius; | |
this.cGravity = radius*0.002; | |
this.cWaterResistance = 0.03; | |
} | |
update(){ | |
} | |
display(){ | |
push(); | |
fill(252,248,225,150); | |
noStroke(); | |
ellipse(this.pos.x,this.pos.y,this.rad * 2,this.rad * 2); | |
pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment