I just take this example of a particle system:
[https://processing.org/examples/simpleparticlesystem.html]
and made some changes.
My goal is to change the colors with keyPressed. With my code the variables of r1,b1,g1 are changing when the program runs, but there is no effect on the color of the ellipses.
I don’t really have an idea where to place the code.
Does anybody have a suggestion?
Thank you
ParticleSystem ps;
int r1 = 255;
int g1 = 0;
int b1 = 155;
int r2 = 0;
int g2 = 155;
int b2 = 255;
color color1 = color(r1, g1, b1);
color color2 = color(r2, g2, b2);
void setup() {
size(800, 400);
ps = new ParticleSystem(new PVector(width/2, height/2));
}
void draw() {
background(0);
ps.addParticle();
ps.run();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class---------------------------------------------
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(random(-0.05, 0.05), random(-0.05, 0.05));
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
// Change the color -------------------------------------------
if (keyPressed) {
if (key == 'b' || key == 'B') {
r1= 0;
g1= 0;
b1= 0;
}
}
update();
display();
println(r1, g1, b1);
}
// Method to update position-------------------------------------------
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display----------------------------------------------------
void display() {
stroke(255, lifespan);
fill(lerpColor(color1, color2, (((millis()/5000)%2==0)?millis()%5000:5000-millis()%5000)/5000.0), lifespan);
ellipse(position.x, position.y, 200-lifespan, 200-lifespan);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
Well, the problem is that right after the r1 g1 b1 etc... values are made, there are 2 other values - color1 and color2.
Another thing to note is that the if (keyPressed)... part of your code is run inside of every single particle, essentially doing the same thing as many times as there are particles. Considering all particles use the same color1 and color2 variables, it makes sense to change them only once.
I suggest making a function definition keyPressed() and putting code to change your colors in there. So, outside of any classes add something like this:
This changes color1 to a random color when you press B, or color2 when you press N.
This function is run only once when you press a key, so if you want to use it further for more advanced keyboard controls, you can also write void keyReleased(){... to define what happens when a key is released.