🌌 Cosmic Particle Dance Simulation 🌠
Here's a creative particle simulation concept that visualizes particles as celestial bodies interacting in a cosmic dance. This simulation uses JavaScript with p5.js (a Processing library for the web).
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<style>
body { margin: 0; overflow: hidden; background: #000; }
canvas { display: block; }
</style>
</head>
<body>
<script>
let particles = [];
const colors = [
{r: 255, g: 100, b: 100}, // Red
{r: 100, g: 255, b: 100}, // Green
{r: 100, g: 100, b: 255}, // Blue
{r: 255, g: 255, b: 100}, // Yellow
{r: 255, g: 100, b: 255} // Purple
];
let gravityWell;
let mode = 0;
const maxModes = 3;
function setup() {
createCanvas(windowWidth, windowHeight);
// Create initial particles
for (let i = 0; i < 200; i++) {
particles.push(new Particle());
}
// Create central gravity well
gravityWell = createVector(width/2, height/2);
// Change mode every 8 seconds
setInterval(() => {
mode = (mode + 1) % maxModes;
// Add some new particles when mode changes
for (let i = 0; i < 20; i++) {
particles.push(new Particle());
}
}, 8000);
}
function draw() {
background(0, 0, 0, 25);
// Update gravity well position to follow mouse
gravityWell.lerp(createVector(mouseX || width/2, mouseY || height/2), 0.05);
// Draw gravity well
noFill();
stroke(255, 50);
strokeWeight(2);
ellipse(gravityWell.x, gravityWell.y, 100, 100);
// Update and display all particles
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].update();
particles[i].display();
// Remove particles that are too old
if (particles[i].lifespan <= 0) {
particles.splice(i, 1);
}
}
// Occasionally add new particles
if (frameCount % 5 === 0 && particles.length < 500) {
particles.push(new Particle());
}
}
class Particle {
constructor() {
this.reset();
this.size = random(2, 8);
this.color = colors[floor(random(colors.length))];
this.lifespan = random(1000, 2000);
this.angle = random(TWO_PI);
this.rotationSpeed = random(-0.02, 0.02);
this.trail = [];
this.maxTrail = 20;
}
reset() {
if (random() > 0.5) {
this.pos = createVector(
random(width),
random() > 0.5 ? 0 : height
);
} else {
this.pos = createVector(
random() > 0.5 ? 0 : width,
random(height)
);
}
this.vel = p5.Vector.random2D().mult(random(0.5, 2));
this.acc = createVector(0, 0);
}
applyBehaviors() {
// Different behaviors based on mode
switch(mode) {
case 0: // Swirling galaxy
this.applyAttraction(gravityWell, 0.01);
this.applyPerpendicularForce(0.05);
break;
case 1: // Repelling explosion
this.applyRepulsion(gravityWell, 0.02);
break;
case 2: // Flocking behavior
this.applyFlocking();
this.applyAttraction(gravityWell, 0.005);
break;
}
}
applyAttraction(target, strength) {
let force = p5.Vector.sub(target, this.pos);
let distance = force.mag();
distance = constrain(distance, 5, 100);
force.normalize();
force.mult(strength * 1000 / (distance * distance));
this.acc.add(force);
}
applyRepulsion(target, strength) {
let force = p5.Vector.sub(this.pos, target);
let distance = force.mag();
distance = constrain(distance, 5, 200);
force.normalize();
force.mult(strength * 1000 / (distance * distance));
this.acc.add(force);
}
applyPerpendicularForce(strength) {
let toCenter = p5.Vector.sub(gravityWell, this.pos);
let perpendicular = createVector(-toCenter.y, toCenter.x);
perpendicular.normalize();
perpendicular.mult(strength);
this.acc.add(perpendicular);
}
applyFlocking() {
let alignment = createVector(0, 0);
let cohesion = createVector(0, 0);
let separation = createVector(0, 0);
let total = 0;
const perceptionRadius = 100;
for (let other of particles) {
let d = dist(this.pos.x, this.pos.y, other.pos.x, other.pos.y);
if (other !== this && d < perceptionRadius) {
// Alignment
alignment.add(other.vel);
// Cohesion
cohesion.add(other.pos);
// Separation
let diff = p5.Vector.sub(this.pos, other.pos);
diff.div(d * d);
separation.add(diff);
total++;
}
}
if (total > 0) {
alignment.div(total);
alignment.setMag(0.1);
cohesion.div(total);
cohesion.sub(this.pos);
cohesion.setMag(0.05);
separation.div(total);
separation.setMag(0.2);
}
this.acc.add(alignment);
this.acc.add(cohesion);
this.acc.add(separation);
}
update() {
this.applyBehaviors();
// Update physics
this.vel.add(this.acc);
this.vel.limit(5);
this.pos.add(this.vel);
this.acc.mult(0);
this.angle += this.rotationSpeed;
// Add current position to trail
this.trail.push(createVector(this.pos.x, this.pos.y));
if (this.trail.length > this.maxTrail) {
this.trail.shift();
}
// Bounce off edges
if (this.pos.x < 0 || this.pos.x > width) this.vel.x *= -0.8;
if (this.pos.y < 0 || this.pos.y > height) this.vel.y *= -0.8;
// Constrain to canvas
this.pos.x = constrain(this.pos.x, 0, width);
this.pos.y = constrain(this.pos.y, 0, height);
// Decrease lifespan
this.lifespan -= 1;
}
display() {
// Draw trail
noFill();
beginShape();
for (let i = 0; i < this.trail.length; i++) {
let alpha = map(i, 0, this.trail.length, 50, 200);
let col = this.color;
stroke(col.r, col.g, col.b, alpha);
strokeWeight(map(i, 0, this.trail.length, 1, this.size));
vertex(this.trail[i].x, this.trail[i].y);
}
endShape();
// Draw particle
push();
translate(this.pos.x, this.pos.y);
rotate(this.angle);
// Fade out as lifespan decreases
let alpha = map(this.lifespan, 0, 2000, 0, 255);
fill(this.color.r, this.color.g, this.color.b, alpha);
noStroke();
// Different shapes based on mode
switch(mode) {
case 0: // Stars
this.drawStar(0, 0, this.size/2, this.size, 5);
break;
case 1: // Circles with rings
ellipse(0, 0, this.size);
noFill();
stroke(this.color.r, this.color.g, this.color.b, alpha/2);
strokeWeight(1);
ellipse(0, 0, this.size * 2);
break;
case 2: // Triangles
triangle(
-this.size, this.size/2,
this.size, this.size/2,
0, -this.size
);
break;
}
pop();
}
drawStar(x, y, radius1, radius2, npoints) {
let angle = TWO