PVector - Explosion array trouble

I am very new to processing and following D. Shiffmans tutorial on the usage of PVectors. I am trying to create a program which startes an "explosion" with 2500 particles[point()] using mousePressed() and each particle needs a random direction and all have to decelerate. Right now I have alot of code, but I just can't piece it together. I could really use some input everybody. It's making a big circle thing and not an explosion - why? And it gets bigger and bigger. Regardless of if i use mousePressed().

Thanks.

My Code:


PImage ruin;

float r = random(1, 10); //Værdier til size of point()

int opa = 255; //Opacity - gennemsigtighed

Explosion [] bang = new Explosion[2000]; // Et array



void setup () {

  size (600, 600);

  smooth();

  ruin = loadImage ("ruins1.jpg");


  //initialize array

  for (int v = 0; v < bang.length; v++) { 

    bang[v] = new Explosion();
  
  }

}

void draw() {
  
  background(ruin);
  
  noStroke();
  
  if (mousePressed) { //Aftagende opacity
   
    opa = opa-1;
    
    for (int v = 0; v < bang.length; v++) {

      bang[v].update();
     
      bang[v].checkEdges();
    
      bang[v].explode();
    
  }
  
 }

}

class Explosion {

  PVector gravity; //tyngdekraft

  PVector location; //bestemmelsested

  PVector velocity; //hastighed

  PVector acceleration; //det samme 

  float speed; // fart

  float angle; //tilgænglige vinkler

    Explosion() {

    angle = random(0.0, TWO_PI);

    speed = random(2.0, 16.0);

    location = new PVector(mouseX, mouseY);

    velocity = new PVector(speed * cos(angle), speed * sin(angle));

    gravity = new PVector (0, 1.8);

    acceleration = new PVector(0, 0.9);
    
  }


  void update() {     //Bevægelses algoritmen

    velocity.add (gravity);

    velocity.add (acceleration);

    location.add(velocity);
    
  }







  void explode() { 
    
    stroke(opa,opa*0.5,opa-150, opa); // Farve på point() og opa
    
    smooth(); 


    if (mousePressed) {

      strokeWeight(r); // Size of point() 
      
      point (location.x, location.y); //Particles

         
  }
 
}


  void checkEdges() {

    // Bounce off walls
    if (location.x > width || location.x < 0) {

      velocity.x = velocity.x * -1;
    } 


    if (location.y > height || location.y < 0) {

      velocity.y = velocity.y * -1;
   
  }
 
 }

}

Answers

Sign In or Register to comment.