We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › particle system not working
Page Index Toggle Pages: 1
particle system not working (Read 1120 times)
particle system not working
Feb 1st, 2010, 4:31pm
 
can anyone tell me why this wont work? i click and nothing happens, but i can't figure out why. its supposed to look like a shower of particles bursts from the  mouseX and mouseY position when the mouse is clicked but like i said nothing happens Sad

Code:
ArrayList particles;
void setup(){
 size(960,600);
 smooth();
 particles = new ArrayList();

}

void draw(){
 background(0);
 
 
}

void mouseClicked(){
 for(int i = 0; i < 25; i++){
   //particles.add(new Particle(tempblock.xpos,tempblock.ypos));
   particles.add(new Particle(mouseX,mouseY));
 }
 for (int i = 0; i >= particles.size(); i++ ) {
   Particle p = (Particle) particles.get(i);
   p.display();
   p.burst();
   p.gravity();
   

   if (p.finished()) {
     particles.remove(i);
   }
 }
}

class Particle{
 float px, py, xspeed, yspeed, life;
 
 Particle(float x_, float y_){
   px = x_;
   py = y_;
   xspeed = random(-2,2);
   yspeed = random(-4,0);
   life = 255;
 }
 
 void burst(){
   px += xspeed;
   py += yspeed;
 }
 
 void gravity(){
   yspeed += .05;
 }
 
 boolean finished(){
   life -= 2.0;
   if (life < 0) return true;
   else return false;
 }

 void stop() {
   xspeed = 0;
   yspeed = 0;
 }
 
 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   ellipseMode(CENTER);
   fill(0,80,250,75);
   ellipse(px,py,10,10);
 }

}

void explode(){
 
 for(int i = 0; i < 25; i++){
   //particles.add(new Particle(tempblock.xpos,tempblock.ypos));
   particles.add(new Particle(mouseX,mouseY));
 }
 for (int i = 0; i >= particles.size(); i++ ) {
   Particle p = (Particle) particles.get(i);
   p.display();
   p.burst();
   p.gravity();
   

   if (p.finished()) {
     particles.remove(i);
   }
 }
}
Re: particle system not working
Reply #1 - Feb 1st, 2010, 11:30pm
 
You're only drawing when the mouse is clicked - so you need to move some stuff into draw():

void draw(){
 background(0);
 for (int i = 0; i < particles.size(); i++ )
 {
   Particle p = (Particle) particles.get(i);
   p.display();
   p.burst();
   p.gravity();
   

   if (p.finished())
   {
     particles.remove(i);
   }
 }
 
 
}

void mouseClicked(){
 for(int i = 0; i < 25; i++){
   //particles.add(new Particle(tempblock.xpos,tempblock.ypos));
   particles.add(new Particle(mouseX,mouseY));
}
 
}

I tested this and it works....
Re: particle system not working
Reply #2 - Feb 2nd, 2010, 4:00am
 
awesome it works perfectly thanks so much giles
Re: particle system not working
Reply #3 - Feb 2nd, 2010, 4:49am
 
Okay, nice answer. I was just about to post the same but I'm too late. Sorry Wink
Re: particle system not working
Reply #4 - Feb 2nd, 2010, 8:44am
 
i'm trying to implement the previous code into another project and is doesn't seem to be working i'll highlight where i'm getting problems. any help would be amazing

Code:

ArrayList blocks;
Paddle paddle = new Paddle();
PFont font;
String level = "";
int levelnum = 0;
int runonce = 0;
int runonce2 = 0;
int runonce3 = 0;
int rungame = 0;
int complete = 0;
ArrayList particles;

void setup(){
 size(960,600);
 smooth();
 font = loadFont("Helvetica-48.vlw");
 particles = new ArrayList();

}

void draw(){
 background(0);

 if(runonce == 0){
   levelselect();
 }
 if(rungame != 0){
   levelexecute(levelnum);
 }
 paddle.display(mouseX,constrain(mouseY, 500, height-paddle.paddle_height/2-25));

 if(checkcollision(mouseX, mouseY) == true){
   println("Success!");


 }


 if(checkcomplete() == true){

   delay(1000);
   runonce2 = 0;
   if(levelnum < 10){
     levelnum ++;
   }
   else{
     complete++;
   }
 }
 if(complete != 0){
   levelnum = -2;
   blocks.clear();
   stroke(0,80,250);
   fill(0,80,250);
   textFont(font);
   textSize(40);
   textMode(SCREEN);
   text("Congratulations! You Win!", 250, height/2);
 }
 
 if(particles.isEmpty() == false){   //if i comment this out i get an out of bounds exception but not in the stand alone program
   for (int i = 0; i >= particles.size(); i++ ) {
     Particle p = (Particle) particles.get(i);
     p.burst();
     p.gravity();
     p.display();

     if (p.finished()) {
       particles.remove(i);
     }
   }
 }

}

void mouseClicked(){
 Block tempblock = (Block) blocks.get(0);
 for(int i = 0; i < 25; i++){
   particles.add(new Particle(mouseX,mouseY));
 }
}


void keyPressed(){
 if((runonce == 0) && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' ||
   key == '7' || key == '8' || key == '9' || key == '0')){
   level = level+key;
   levelnum = int(level);
 }
 if(key == ENTER){
   runonce++;
   rungame++;

 }
 if(key == TAB){
   runonce = 0;
   runonce2 = 0;
   rungame = 0;
   level = "";
 }
 if(rungame != 0 && key == BACKSPACE){

   blocks.remove(0);
   explode();

   //blocks.clear();
 }
}
class Ball{
 float ball_x;
 float ball_y;
 float ball_r;
 
 Ball(float x, float y, float r) {
   this.ball_x = x;
   this.ball_y = y;
   this.ball_r = r;
 }
}
class Block{
 float xpos;
 float ypos;

 Block(float x, float y){
   xpos = x;
   ypos = y;
 }

 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   rectMode(CENTER);
   fill(0,80,250,75);

   rect(xpos,ypos, 60,25);
 }
}
boolean checkcollision(float px, float py){
 boolean check = false;
 ArrayList blockgrid = new ArrayList();
 if(runonce != 0){
   for(int i = 0; i < blocks.size(); i++){
     Block tempblock = (Block) blocks.get(i);
     float temp = dist(px,py,tempblock.xpos,tempblock.ypos);
       if( temp< 10){
         check = true;
       }
       else{
         check = false;
       }
   }
 }
 return check;
}
boolean checkcomplete(){
 if(runonce == 0){
   return false;
 }
 else if(blocks.size() <= 0){
   return true;
 }
 else{
   return false;
 }
}
void explode(){
 Block tempblock = (Block) blocks.get(0);
 for(int i = 0; i < 25; i++){
   particles.add(new Particle(tempblock.xpos,tempblock.ypos));
 }
 if(particles.isEmpty() == false){
   for (int i = 0; i >= particles.size(); i++ ) {
     Particle p = (Particle) particles.get(i);
     p.burst();
     p.gravity();
     p.display();

     if (p.finished()) {
       particles.remove(i);
     }
   }
 }
}
void levelexecute(int levelnum){
 if(runonce2 == 0){
   blocks = new ArrayList();
   for(int x = 1;x < 15;x++){
     for(int t = 1; t <= levelnum+2; t++){
       blocks.add(new Block(x*60+30, t*25+30));
     }
   }
   runonce2++;
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   tempblock.display();
 }
 
}
void levelexecute(int levelnum){
 if(runonce2 == 0){
   blocks = new ArrayList();
   for(int x = 1;x < 15;x++){
     for(int t = 1; t <= levelnum+2; t++){
       blocks.add(new Block(x*60+30, t*25+30));
     }
   }
   runonce2++;
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   tempblock.display();
 }
 
}
void levelexecute(int levelnum){
 if(runonce2 == 0){
   blocks = new ArrayList();
   for(int x = 1;x < 15;x++){
     for(int t = 1; t <= levelnum+2; t++){
       blocks.add(new Block(x*60+30, t*25+30));
     }
   }
   runonce2++;
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   tempblock.display();
 }
 
}
class Paddle{
 float paddle_xpos;
 float paddle_ypos;
 int paddle_height;
 int paddle_width;
 
 Paddle(){
   paddle_height = 15;
   paddle_width = 96;
 }
 void display(int xpos_,int ypos_){
   int xpos = xpos_;
   int ypos = ypos_;
   xpos = constrain(mouseX, paddle_width/2, width-paddle_width/2);
   rectMode(CENTER);
   fill(255);
   rect(xpos, ypos,paddle_width, paddle_height);
 }
}
class Particle{
 float px, py, xspeed, yspeed, life;
 
 Particle(float x_, float y_){
   px = x_;
   py = y_;
   xspeed = random(-2,2);
   yspeed = random(-4,0);
   life = 255;
 }
 
 void burst(){
   px += xspeed;
   py += yspeed;
 }
 
 void gravity(){
   yspeed += .05;
 }
 
 boolean finished(){
   life -= 2.0;
   if (life < 0) return true;
   else return false;
 }

 void stop() {
   xspeed = 0;
   yspeed = 0;
 }
 
 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   ellipseMode(CENTER);
   fill(0,80,250,75);
   ellipse(px,py,10,10);
 }

}
Re: particle system not working
Reply #5 - Feb 2nd, 2010, 8:45am
 
this is where it is working by itself
Code:
ArrayList particles;

void setup(){
 size(960,600);
 smooth();
 particles = new ArrayList();

}

void draw(){
background(0);
for (int i = 0; i < particles.size(); i++ )
{
  Particle p = (Particle) particles.get(i);
  p.display();
  p.burst();
  p.gravity();
 

  if (p.finished())
  {
    particles.remove(i);
  }
}


}

void mouseClicked(){
for(int i = 0; i < 25; i++){
  //particles.add(new Particle(tempblock.xpos,tempblock.ypos));
  particles.add(new Particle(mouseX,mouseY));
}

}
class Particle{
 float px, py, xspeed, yspeed, life;
 
 Particle(float x_, float y_){
   px = x_;
   py = y_;
   xspeed = random(-2,2);
   yspeed = random(-4,0);
   life = 255;
 }
 
 void burst(){
   px += xspeed;
   py += yspeed;
 }
 
 void gravity(){
   yspeed += .05;
 }
 
 boolean finished(){
   life -= 2.0;
   if (life < 0) return true;
   else return false;
 }

 void stop() {
   xspeed = 0;
   yspeed = 0;
 }
 
 void display(){
   strokeWeight(1);
   stroke(0,80,250);
   ellipseMode(CENTER);
   fill(0,80,250,75);
   ellipse(px,py,10,10);
 }

}
Re: particle system not working
Reply #6 - Feb 2nd, 2010, 9:00am
 
so i copy and pasted the code from the project i was trying to implement it into, and put it in the one that was working and it works now... weird but if anyone knows why let me know so i can learn for future reference. otherwise thanks anyways Smiley
Page Index Toggle Pages: 1