xxxxxxxxxx
//Inspired by this: https://www.openprocessing.org/sketch/147268
final int MAX_PARTICLES = 100;
Particle[] particles = new Particle[MAX_PARTICLES];
ColorGenerator colorGen = new ColorGenerator();
void setup()
{
size(600, 500, P2D);
for (int x = 0; x < MAX_PARTICLES; x++) { // Initiate our particles
particles[x] = new Particle();
}
}
void draw()
{
noStroke();
colorGen.update();
fill(120, 1);
//background(255);
for (int x = 0; x < MAX_PARTICLES; x++) { // Move our particles
particles[x].move();
particles[x].draw();
}
}
class Particle
{
final static float SIZE = 4;
final static float BOUNCE = -1;
final static float MAX_SPEED = 2.0;
final static float MAX_DIST = 50;
PVector speed = new PVector(random(-MAX_SPEED, MAX_SPEED), random(-MAX_SPEED, MAX_SPEED)); // Create speed in a random direction/vector (using our speed constraints)
PVector pos; // Position of particle
// Constructor
Particle()
{
pos = new PVector(random(width), random(height));
}
public void move()
{
pos.add(speed); // Add our speed to our position to get a position change
// React to hitting walls
if (pos.x < 0)
{
pos.x = 0;
speed.x *= BOUNCE;
}
else if (pos.x > width)
{
pos.x = width;
speed.x *= BOUNCE;
}
if (pos.y < 0)
{
pos.y = 0;
speed.y *= BOUNCE;
}
else if (pos.y > height)
{
pos.y = height;
speed.y *= BOUNCE;
}
}
public void draw()
{
noFill();
fill(colorGen.R, colorGen.G, colorGen.B);//255, 14);
ellipse(pos.x, pos.y, SIZE, SIZE);
}
}
class ColorGenerator
{
final static float MIN_SPEED = 0.7;// Min speed of color change
final static float MAX_SPEED = 1.5; // Max speed of color change
float R, G, B; // Starting color combination
float Rspeed , Gspeed, Bspeed; // Speed of color transition
ColorGenerator()
{
init();
}
public void init()
{
// Generate base start colours
R = random(255);
G = random(255);
B = random(255);
// transition to new related colour
Rspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
Gspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
Bspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
}
public void update()
{
// Change color but keep the colour ranges withing the RGB 255 bounds
Rspeed = ((R += Rspeed) > 255 || (R < 0)) ? -Rspeed : Rspeed;
Gspeed = ((G += Gspeed) > 255 || (G < 0)) ? -Gspeed : Gspeed;
Bspeed = ((B += Bspeed) > 255 || (B < 0)) ? -Bspeed : Bspeed;
}
}
void keyPressed() {
if (key == ENTER) {
colorGen.init();
}
}