in processing, implement a beautiful particle interaction that works over multiple windows. allow mutiple windows and particle simulation methods. this entails particles from window 1 being able to interact with particles from window 2 when dragged around and responding dynamically. allow for a 3d simulation mode as well.
Assistant
Sure! Below is a Processing sketch that implements a beautiful particle system with interactions across multiple windows, including a 3D simulation mode. This code creates multiple windows, allows particles to be dragged around, and particles from one window interact dynamically with particles from other windows.
java
// MainSketch.pde
import java.util.ArrayList;
// Global particle manager
ParticleManager particleManager;
int numParticles = 100;
void settings() {
size(400, 400); // Main window (can be hidden or used as a control panel)
}
void setup() {
// Initialize particle manager
particleManager = new ParticleManager(numParticles);
// Create multiple windows
String[] args1 = { "Window1" };
ParticleWindow window1 = new ParticleWindow(particleManager, "Window 1", false); // 2D window
PApplet.runSketch(args1, window1);
String[] args2 = { "Window2" };
ParticleWindow window2 = new ParticleWindow(particleManager, "Window 2", true); // 3D window
PApplet.runSketch(args2, window2);
}
void draw() {
background(50);
fill(255);
textAlign(CENTER, CENTER);
textSize(16);
text("Particle Simulation\nWindows are running separately.", width / 2, height / 2);
}
java
// ParticleWindow.pde
public class ParticleWindow extends PApplet {
ParticleManager particleManager;
String windowTitle;
boolean is3D;
// Variables for dragging particles
Particle selectedParticle = null;
PVector dragOffset = new PVector();
ParticleWindow(ParticleManager pm, String title, boolean _3D) {
particleManager = pm;
windowTitle = title;
is3D = _3D;
}
public void settings() {
if (is3D) {
size(600, 600, P3D);
} else {
size(600, 600);
}
}
public void setup() {
surface.setTitle(windowTitle);
smooth();
}
public void draw() {
if (is3D) {
background(10);
lights();
// Simple camera rotation based on mouse
float camX = map(mouseX, 0, width, -200, 200);
float camY = map(mouseY, 0, height, -200, 200);
camera(camX, camY, 600, 0, 0, 0, 0, 1, 0);
} else {
background(10);
}
// Update and display particles
particleManager.update();
particleManager.display(this, is3D);
}
public void mousePressed() {
selectedParticle = particleManager.getParticleUnderMouse(this);
if (selectedParticle != null) {
dragOffset.set(PVector.sub(selectedParticle.position, new PVector(mouseX, mouseY, is3D ? mouseZ : 0)));
}
}
public void mouseDragged() {
if (selectedParticle != null) {
selectedParticle.position.set(mouseX + dragOffset.x, mouseY + dragOffset.y, is3D ? mouseZ + dragOffset.z : 0);
}
}
public void mouseReleased() {
selectedParticle = null;
}
}
java
// ParticleManager.pde
class ParticleManager {
ArrayList<Particle> particles;
ParticleManager(int numParticles) {
particles = new ArrayList<Particle>();
for (int i = 0; i < numParticles; i++) {
particles.add(new Particle());
}
}
void update() {
// Update particles
for (Particle p : particles) {
p.update();
}
// Check interactions
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);
p1.interactWith(p2);
}
}
}
void display(PApplet app, boolean is3D) {
for (Particle p : particles) {
p.display(app, is3D);
}
}
Particle getParticleUnderMouse(PApplet app) {
for (Particle p : particles) {
float distance;
if (app.g instanceof PGraphics3D) {
PVector screenPos = new PVector();
((PGraphics3D) app.g).camera(); // Update camera matrix for accurate projection
PGraphics3D g3d = (PGraphics3D) app.g;
PVector modelPos = p.position.copy();
g3d.modelview.mult(modelPos, screenPos); // Project model coordinates to screen
g3d.projection.mult(screenPos, screenPos);
screenPos.x = app.screenX(screenPos.x, screenPos.y, screenPos.z);
screenPos.y = app.screenY(screenPos.x, screenPos.y, screenPos.z);
distance = PVector.dist(new PVector(app.mouseX, app.mouseY), new PVector(screenPos.x, screenPos.y));
} else {
distance = PVector.dist(new PVector(app.mouseX, app.mouseY), p.position);
}
if (distance < p.size / 2) {
return p;
}
}
return null;
}
}
Save the Files: Copy each of the code blocks into separate .pde files within the same Processing sketch folder. The filenames should match the class names:
MainSketch.pde
ParticleWindow.pde
ParticleManager.pde
Particle.pde
Run the Sketch: Open MainSketch.pde in Processing and run it. This will launch the main control window and two additional windows titled "Window 1" and "Window 2".
Interact with the Simulation:
Window 1: This is a 2D view of the particle simulation. You can click and drag particles around. The particles interact with each other, and their interactions are reflected in all windows.
Window 2: This is a 3D view of the same particle simulation. You can also click and drag particles in 3D space. Use the mouse to rotate the camera view if you wish to enhance the visualization (additional controls can be added for camera movement).
Features of the Simulation:
Multi-Window Interaction: Particles exist in a shared space and are displayed in multiple windows. Actions in one window (like dragging a particle) will affect the state of the simulation and be reflected in other windows.
Dynamic Particle Interactions: The particles influence each other with simple physics. In the provided example, particles have a basic attraction or repulsion when they are close to each other.
3D Simulation Mode: The second window demonstrates the particle system in 3D, adding depth to the interaction.
Customization:
Number of Particles: Adjust the numParticles variable in MainSketch.pde to increase or decrease the number of particles in the simulation.
Particle Behavior: Modify the interactWith method in Particle.pde to change how particles interact (e.g., gravitational attraction, collision response).
Visual Appearance: Change the colors, sizes, and visual styles in the display method to enhance the visual appeal.
Notes:
Ensure that all files are saved in the same sketch folder, and their names match the class names for Processing to recognize them properly.
This example uses basic physics and interactions for demonstration purposes. For more complex simulations, consider implementing more advanced physics engines or algorithms.
Enjoy exploring and modifying the particle simulation to suit your creative ideas!