Spawning in top left corner? HELP

I'm trying to make my "explosion happen at mouse coordinates, but it is happening in top left corner instead of at mouseX,mouseY ? Why is this ? Please help :)

Still working on the whole formatting-thing in here , so mind the code structure on this page.

CODE :



PImage ruin;

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

float q = random(-45, 45); //Værdier til place of point()

float h = random(-10, 10); // Random angles

int opa = 255; //Opacity - skal gå ned til 0 lidt efter lidt

Explosion [] bang = new Explosion[100]; // An 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) { 
    opa = opa-1;
    for (int v = 0; v < bang.length; v++) {

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

class Explosion {

  PVector gravity;

  PVector location;

  PVector velocity;

  PVector acceleration;

  float speed;

  float angle;
  
  float mass;

  Explosion() {

    mass = 1;
    
    angle = random(0.0, TWO_PI);

    speed = random(2.0, 8.0);

    location = new PVector(mouseX,mouseY);

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

    gravity = new PVector (0.15, 0.02);


    acceleration = new PVector(0, 0.1);
  }


  void update() {     //Bevægelses algoritme



      velocity.add (gravity);
    
    velocity.add (acceleration); //
    
    location.add(velocity);
  
}







  void explode() { 
    stroke(255, opa); // Farve på point() og opa
    smooth(); 


    if (mousePressed) {

      strokeWeight(r); // Size of point() 
      point (location.x, location.y); //Particles
      gravity.x = 0;
    }
  }


  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

  • help me plz

  • Please format the code, details are in the sticky threads at top of forum. This way there'll be line numbers we can quote.

  • In the meantime, look at how you are initialising location.

  • Answer ✓

    Now that it is formatted, I can read it! Looks like you are creating your Explosion objects on line 28 in setup(). In setup(), mouseX and mouseY are ALWAYS 0, because the sketch hasn't started yet and so it doesn't know where the mouse is yet.

    Don't think that the position is going to change to the new values of mouseX and mouseY every frame! When you create the PVector, it does not "magically link" to those other variables - instead it takes their current value and does not change it!

  • Yeah that seems to do the trick, but the whole animation just stays within 10 or so points of the mouseX,mouseY. But thanks

  • if you've put the explosion initialization code where i think you have, inside the mousepressed condition on line 35, then every time you press the mouse you reinitialize the location to mouseX, mouseY

    you need a flag to tell you whether this is the first time you've pressed the mouse and only do the initialization then

  • Got any suggentions :) ?

  • Am I looking for a boolean function or?

  • counter maybe..

  • Answer ✓

    bang[0] is null if this is the first time

Sign In or Register to comment.