#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# PVector - Explosion array trouble

edited May 2015

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; // Et array

void setup () {

size (600, 600);

smooth();

//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

}

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;

}

}

}

```
Tagged:

• What have I done wrong since my code is so ugly to look at...?

• Very bad in trigs. Most I can do is pass this online fireworks example: :-<

Also you should learn how to post formatted code in the forum: ~O)
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

• Thanks GoToLoop

• What do you mean by very bad in "trigs"?

• Trigonometry...

• your new particles are being initialized with

``````velocity = new PVector(random(-10, 10), random(-10, 10));
``````

but then you are

``````velocity.limit(maxspeed); // where maxspeed is 8
``````

so everything with a velocity > 8 (which is almost exactly half of your particles) gets clamped to velocity = 8. which means you get a lot of particles with the same speed but different angles. which is why you see a circle.

would be better to pick a random speed between, say, 4 and 8 and then pick a random direction.

``````speed = random(4.0, 8.0);
angle = random(0.0, TWO_PI);
velocity = new PVector(speed * cos(angle), speed * sin(angle));
``````
• edited May 2015

Thanks alot koogs! I'll have to google the TWO_PI as it is the first time i will have worked with it.

• edited May 2015 Answer ✓

TWO_PI is a Processing constant defined as `2.0f * PI`.
TAU is an alias to TWO_PI. >-)

• Again - thanks GoToLoop.

• Why is my animation/Explosion happening in the top left corner and not at the center of the screen when my PVector location = (mouseX, mouseY) ?

• Do you have an updated code to show? With proper formatting?

• Yes here you go

• Mmm, apparently, you started another topic at Spawning in top left corner? HELP. It can help to cross link your related posts.