import processing.video.*;
MovieMaker mm; // Declare MovieMaker object
int numParticles = 25000;
ArrayList particles = new ArrayList();
Orbital_Att oa;
float theta, atheta = 0.0f;
float xoff = 0.0f;
int w = 640;
int h = 480;
PVector origin = new PVector(w/2, h/2, 0);
PVector ml;
float explRad = 1.0f;
float attRad = 30.0f;
void setup()
{
size(w, h, P3D);
colorMode(HSB, 360, 100, 100, 100);
oa = new Orbital_Att(new PVector(), 2.0f, 50.0f);
for(int i = 0; i < numParticles; i++)
{
PVector a = new PVector();
PVector l = new PVector(width/2, height/2, 0);
PVector v = new PVector();
particles.add(new Particle(a,v,l, 1.0f, random(0.0f, 100.0f)));
}
//mm = new MovieMaker(this, width, height, "drawing.mov", 30, MovieMaker.VIDEO, MovieMaker.LOSSLESS);
}
void draw()
{
background(1, 0, 0, 100);
ml = new PVector(w/2, h/2, 0);
float ax = attRad * cos(atheta);
float ay = attRad * sin(atheta);
ax += ml.x;
ay += ml.y;
atheta += 10.0f;
float np = map(mouseX, 0, width, 0.000001f, 0.1f);
float der = map(mouseY, 0, height, 0.5f, 3.0f);
for (int i = 0; i < numParticles; i++) {
Particle prt = (Particle) particles.get(i);
prt.run();
if(mousePressed == true){
oa.setLocation(new PVector(ax, ay, 0));
PVector force = oa.warp(prt);
prt.add_force(force);
}
if (prt.dead()) {
particles.remove(i);
float x = der * cos(theta);
float y = der * sin(theta);
x *= noise(xoff);
y *= noise(xoff);
x += origin.x;
y += origin.y;
xoff += np;
theta += 1.0f;
PVector a = new PVector();
PVector l = new PVector(x, y, 0);
PVector v = PVector.sub(l,origin);
particles.add(new Particle(a,v,l, 1.0f));
}
}
//mm.addFrame();
}
void keyPressed() {
if (key == ' ') {
mm.finish();
}
}
class Particle {
PVector loc;
PVector vel;
PVector acc;
float ms;
float timer;
float distance;
Particle(PVector a, PVector v, PVector l, float ms_) {
acc = a;
vel = v;
loc = l;
ms = ms_;
timer = 100.0;
}
Particle(PVector a, PVector v, PVector l, float ms_, float tmr) {
acc = a;
vel = v;
loc = l;
ms = ms_;
timer = tmr;
}
void run() {
update();
render();
}
void update() {
vel.add(acc);
loc.add(vel);
acc = new PVector();
timer -= 0.5f;
}
void render() {
float col = map(timer, 0, 100, 30, 70);
stroke(col, 100, 100, 20);
point(loc.x,loc.y);
}
void add_force(PVector force) {
force.div(ms);
vel.add(force);
}
float getMass() {
return ms;
}
void setMass(float mp) {
ms = mp;
}
PVector getLocation() {
return loc;
}
boolean dead() {
if (timer <= 0) {
return true;
} else {
return false;
}
}
}
class Orbital_Att
{
PVector location;
float mass;
float G;
Orbital_Att(PVector location, float mass, float G)
{
this.location = location;
this.mass = mass;
this.G = G;
}
PVector warp(Particle p) {
PVector dir = PVector.sub(location,p.getLocation());
float d = dir.mag();
dir.normalize();
float force = (G * mass * p.getMass()) / (d * d);
dir.mult(force);
return dir;
}
PVector getLocation() {
return location;
}
void setLocation(PVector l){
location = l;
}
}