Certainly! Below is a Processing sketch that demonstrates how to implement beautiful particle interactions across multiple windows. Each window runs its own particle simulation with different interaction methods. This setup allows for multiple windows and particle simulation methods.
Main Sketch (Main.pde)
void settings() {
size(400, 400);
}
void setup() {
// Create multiple windows
String[] windowArgs1 = {"ParticleSimulation1"};
ParticleSimulation1 ps1 = new ParticleSimulation1();
PApplet.runSketch(windowArgs1, ps1);
String[] windowArgs2 = {"ParticleSimulation2"};
ParticleSimulation2 ps2 = new ParticleSimulation2();
PApplet.runSketch(windowArgs2, ps2);
}
void draw() {
background(0);
fill(255);
textAlign(CENTER, CENTER);
textSize(24);
text("Main Window", width/2, height/2);
}
Particle Class (Particle.pde)
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float mass;
color col;
Particle(PVector position, float mass, color col) {
this.position = position.copy();
this.mass = mass;
this.col = col;
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0);
// Wrap around edges
if (position.x > width) position.x = 0;
if (position.x < 0) position.x = width;
if (position.y > height) position.y = 0;
if (position.y < 0) position.y = height;
}
void display() {
fill(col);
noStroke();
ellipse(position.x, position.y, mass*2, mass*2);
}
}
Particle Simulation 1 (ParticleSimulation1.pde)
public class ParticleSimulation1 extends PApplet {
ArrayList<Particle> particles;
public void settings() {
size(600, 600);
}
public void setup() {
particles = new ArrayList<Particle>();
for (int i = 0; i < 150; i++) {
float x = random(width);
float y = random(height);
float m = random(2, 4);
color c = color(random(50, 255), random(50, 255), random(50, 255));
particles.add(new Particle(new PVector(x, y), m, c));
}
}
public void draw() {
background(0);
for (Particle p1 : particles) {
// Apply gravitational attraction from other particles
for (Particle p2 : particles) {
if (p1 != p2) {
PVector force = gravitationalAttraction(p1, p2);
p1.applyForce(force);
}
}
}
for (Particle p : particles) {
p.update();
p.display();
}
}
PVector gravitationalAttraction(Particle p1, Particle p2) {
float G = 0.5f;
PVector force = PVector.sub(p2.position, p1.position);
float distance = force.mag();
distance = constrain(distance, 5, 25);
force.normalize();
float strength = (G * p1.mass * p2.mass) / (distance * distance);
force.mult(strength);
return force;
}
}
Particle Simulation 2 (ParticleSimulation2.pde)
public class ParticleSimulation2 extends PApplet {
ArrayList<Particle> particles;
float springLength = 50;
float stiffness = 0.05f;
public void settings() {
size(600, 600);
}
public void setup() {
particles = new ArrayList<Particle>();
for (int i = 0; i < 60; i++) {
float x = random(width);
float y = random(height);
float m = random(4, 8);
color c = color(random(50, 255), random(50, 255), random(50, 255));
particles.add(new Particle(new PVector(x, y), m, c));
}
}
public void draw() {
background(0);
// Apply spring forces
for (int i = 0; i < particles.size(); i++) {
Particle p1 = particles.get(i);
for (int j = i + 1; j < particles.size(); j++) {
Particle p2 = particles.get(j);
PVector force = springForce(p1, p2);
p1.applyForce(force);
p2.applyForce(force.mult(-1));
}
}
for (Particle p : particles) {
p.update();
p.display();
}
}
PVector springForce(Particle p1, Particle p2) {
PVector force = PVector.sub(p2.position, p1.position);
float distance = force.mag();
float stretch = distance - springLength;
force.normalize();
force.mult(stiffness * stretch);
return force;
}
}
Instructions
-
Create a new Processing sketch.
-
Add four tabs to your sketch, naming them: Main, Particle, ParticleSimulation1, and ParticleSimulation2. In the Processing IDE, click the small arrow in the top-right corner and select "New Tab" to add tabs.
-
Copy each code block into its corresponding tab:
Main.pde: Copy the code from Main Sketch.
Particle.pde: Copy the code from Particle Class.
ParticleSimulation1.pde: Copy the code from Particle Simulation 1.
ParticleSimulation2.pde: Copy the code from Particle Simulation 2.
-
Run the sketch. The main window will display along with two additional windows, each running a different particle simulation.
Explanation
-
Main Sketch (Main.pde):
- Sets up the main window.
- Creates instances of
ParticleSimulation1 and ParticleSimulation2 classes.
- Calls
PApplet.runSketch() to run each simulation in its own window.
-
Particle Class (Particle.pde):
- Represents individual particles with position, velocity, acceleration, mass, and color.
- Contains methods to apply forces (
applyForce), update position (update), and display the particle (display).
- Particles wrap around the edges of the window for continuous movement.
-
Particle Simulation 1 (ParticleSimulation1.pde):
- Simulates particles that attract each other using a gravitational force.
- Each particle calculates gravitational forces from all other particles.
- Provides a visually appealing effect as particles cluster and orbit each other.
-
Particle Simulation 2 (ParticleSimulation2.pde):
- Simulates particles connected by virtual springs.
- Particles oscillate and form dynamic structures due to spring forces.
- The spring force is calculated based on Hooke's law.
Customizations
Notes
Conclusion
This setup allows you to explore various particle interaction methods simultaneously in different windows. By customizing the forces and behaviors, you can create a wide range of beautiful and dynamic simulations.
Feel free to experiment with the code to create unique particle interactions and add your own creative touches!
Let me know if you need further assistance or have any questions about the code!