Skip to content

Instantly share code, notes, and snippets.

@XiaohanYa
Created March 5, 2017 10:35
Show Gist options
  • Save XiaohanYa/338cecb782cd19dab302e2ba385f3756 to your computer and use it in GitHub Desktop.
Save XiaohanYa/338cecb782cd19dab302e2ba385f3756 to your computer and use it in GitHub Desktop.
var planets = [];
var particles = [];
function setup() {
createCanvas(1200, 800);
planets.push(new Planet(-300, random(-200, 200), random(80, 120))); // x,y,radius
planets.push(new Planet(300, random(-200, 200), random(150, 200))); // x,y,radius
for (var i = 0; i < 30; i++) {
particles.push(new Particle(-width / 2, -height / 2));
}
}
function draw() {
background(0);
push();
translate(width / 2, height / 2);
// create zoom-in & out
var s = map(mouseY, 0, height, 0.5, 1.5);
scale(s);
for (var j = 0; j < planets.length; j++) {
var tPlanet = planets[j];
for (var i = 0; i < particles.length; i++) {
var p = particles[i];
// how to create a gravity
var gravity = p5.Vector.sub(tPlanet.pos, p.pos);
gravity.normalize();
gravity.mult(tPlanet.cGravity * p.mass);
p.applyForce(gravity);
var distance = p.pos.dist(tPlanet.pos);
// core area
if (distance < tPlanet.coreRad) {
p.fillColor(255, 255, 255, 255);
p.vel.mult(tPlanet.cCoreRestitution);
}
// water area
else if (distance < tPlanet.waterRad) {
// water resistance
p.fillColor(255, 255, 0, 0);
var resistance = p5.Vector.mult(p.vel, -1);
resistance.normalize();
var speed = p.vel.mag();
resistance.mult(speed * speed * tPlanet.cWaterResistance);
resistance.limit(speed); // ***
p.applyForce(resistance);
}
// air area
else if (distance < tPlanet.rad) {
// air resistance
var resistance = p5.Vector.mult(p.vel, -1);
resistance.normalize();
var speed = p.vel.mag();
resistance.mult(speed * speed * tPlanet.cAirResistance);
resistance.limit(speed); // ***
p.applyForce(resistance);
// wind
var wind = p.vel.copy();
wind.normalize();
wind.rotate(radians(60)); // ***
wind.mult(tPlanet.cWindMagnitude);
p.applyForce(wind);
p.fillColor(255, 0, 0, 0);
}
p.update();
p.display();
}
tPlanet.update();
tPlanet.display();
}
pop();
}
"use strict";
class Planet {
constructor(x, y, radius) {
this.pos = createVector(x, y);
this.rad = radius;
this.waterRad = this.rad * random(0.4, 0.9);
this.coreRad = this.rad * random(0.2, 0.3);
this.cGravity = radius*0.0001;
this.cWindMagnitude = 2;
this.cAirResistance = 0.01;
this.cWaterResistance = 0.9;
this.cCoreRestitution = 1.5;
}
update() {
//this.pos.x = cos(frameCount*0.01) * 50;
//this.pos.y = sin(frameCount*0.01) * 50;
}
display() {
push();
// Air
fill(255, 50);
stroke(255,100);
ellipse(this.pos.x, this.pos.y, this.rad * 2, this.rad * 2);
// Water
fill(0, 0, 255, 120);
stroke(0, 0, 255);
ellipse(this.pos.x, this.pos.y, this.waterRad * 2, this.waterRad * 2);
// core
fill(255, 0, 0, 80);
stroke(255, 0, 0);
ellipse(this.pos.x, this.pos.y, this.coreRad * 2, this.coreRad * 2);
pop();
}
}
"use strict";
class Particle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(random(1, 3), random(-1, 3)); // ***
this.acc = createVector();
this.dia = 2;
this.mass = random(1, 10);
this.c = color('rgba(255,255,255,0)')
}
fillColor(r, g, b, a) {
this.c = color('rgba(r, g, b,a)');
}
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(this.c);
ellipse(0, 0, this.dia * this.mass, this.dia * this.mass);
pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment