I'm attempting a simple exercise using particle systems and arraylists. I am attempting to simulate fireworks. I want to shoot a ball object, which explodes in to a particle system when it enters a specific zone. I was able to make this work with a single ball object, however now that I'm using an ArrayList, the origin of the ParticleSystem reverts back to what is instantiated in setup. The problem, is that I can't set the origin to the ball location. How do I set origin of the particlesystem, to the location of the ball?
Any help would be much appreciated. Code owes a heavy heavy debt to Shiffman's Nature of Code.
Code is below.
ArrayList balls;
ParticleSystem ps;
boolean button = false;
PVector origin;
PVector grav = new PVector(0.0, 0.1);
int x = 100;
int y = 380;
int w = 100;
int h = 100;
int x1 = 320;
int y1 = 150;
int w1 = 100;
int h1 = 100;
void setup() {
size (640,480);
smooth();
PVector a = new PVector(random(3),random(-5)); //acceleration
PVector v = new PVector(2,-5); //velocity
PVector loca = new PVector(50,380); //where the ball starts
balls = new ArrayList();
balls.add(new Ball(a,v,loca));
ps = new ParticleSystem(1, new PVector(height/2, width/2));
}
void draw() {
background(255);
//balls.applyForce(grav);
if (button) {
for (int i = balls.size() - 1; i >=0; i--) {
Ball ball = (Ball) balls.get(i);
if (ball.pop()) {
// ball.impact = true;
// if(ball.impact) {
for (int p = 0;p < 10; p++) {
ps.run();
ps.addParticle();
}
}
else {
// ball.appyForce(grav);
ball.move();
ball.render();
}
}
}
fill(175);
rect(x,y,w,h);
noFill();
}
void mousePressed() {
if (mouseX > x && mouseX < x+w && mouseY <y+h && mousePressed) {
button = !button;
}
}
//void explode() {
// for (int i = 0; i < 20; i++) {
// ps.applyForce(grav);
// ps.run();
// ps.addParticle();
// }
//}
class Particle {
PVector loc;
PVector vel;
PVector acc;
float r;
float timer;
Particle(PVector a, PVector v, PVector l, float r_) {
acc = a.get();
vel = v.get();
loc = l.get();
r = r_;
timer = 100.0;
}
Particle(PVector l) {
acc = new PVector(0,0.05,0);
vel = new PVector(random(-1,1),random(-2,0),0);
loc = l.get();
r = 10.0;
timer = 150.0;
}
void run() {
update();
render();
}
void update() {
vel.add(acc);
loc.add(vel);
acc = new PVector();
timer -= 2.5;
}
//I'm bi-winning. Winning here, winning there!
void render() {
ellipseMode(CENTER);
stroke(0,timer);
fill(0,timer);
ellipse(loc.x,loc.y,r,r);
// point(loc.x,loc.y);
}
void applyForce(PVector force) {
acc.add(force);
}
boolean dead() {
if (timer <= 0.0) {
return true;
} else {
return false;
}
}
}
//code/comments taken and altered (sorta) from Shiffman's SimpleParticalSystem <http://www.shiffman.net>
class ParticleSystem {
ArrayList particles; //an ArrayList for all the particle
PVector origin;
ParticleSystem(int num, PVector l){
particles = new ArrayList();
origin = l.get();
println(origin);
for (int i = 0; i < num; i++) {
// PVector n = new PVector (origin); //store the origin point, the mouseX/mouseY was added by Liesje to demonstrate how to change the origin
// origin = l.get();
particles.add(new Particle(origin)); //add "num" particles to the array list
}
}
void run(){
//going backwards through ArrayList because we are deleting
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i); //why is it "(Particles)"
p.run();
if (p.dead()){
particles.remove(i); //remove all particles (i)
}
}
}
void setOrigin(){
for (int i = balls.size() - 1; i >=0; i--) {
Ball ball = (Ball) balls.get(i);
origin = ball.loc;
}
}
void addParticle(){
particles.add(new Particle(origin));
}
void setOrigin(PVector v) {
origin = v.get();
}
void addParticle(Particle p) {
particles.add(p);
}
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
}
}
class Ball {
ArrayList balls;
PVector loc;
PVector vel;
PVector acc;
float mass = 1.0f;
boolean impact = false;
Ball(PVector a, PVector v, PVector l) {
acc = a.get();
vel = v.get();
loc = l.get();
}
void update() {
vel.add(acc); //adding velocity to acceleration
loc.add(vel); //adding location to vel, to create movement
acc.mult(0); //resetting the acceleration back to 0
}
boolean pop() { //from tank sketch
//if loc.x > 320 and loc x is smaller than 320+100 and loc.y is smaller than 250
// if loc.x is between 320/420 and loc.y is smaller than 250