// Dartmouth CS 2, Winter 2009, Chris Bailey-Kellogg // Notes 15 | Sketch 3 // Like Sketch 2, but using Traer Physics package import traer.physics.*; Particle[] ps = new Particle[10]; int dragging = -1; // which particle is being dragged (-1 if none) int r=10; // uniform radius ParticleSystem physics; int G = 1000; // strength of repulsion void setup() { size(400,300); smooth(); noStroke(); // Create the physics module, with no inherent gravity and a small amount of drag physics = new ParticleSystem(0,0.1); // Create the particles, with uniform mass of 1, starting at random positions for (int i=0; i width) p.velocity().set(-0.9*p.velocity().x(), p.velocity().y(), 0); if (p.position().y() < 0 || p.position().y() > height) p.velocity().set(p.velocity().x(), -0.9*p.velocity().y(), 0); p.position().set(constrain(p.position().x(), 0, width), constrain(p.position().y(), 0, height), 0); } void mousePressed() { // See if beginning to drag dragging = -1; for (int i=0; i= 0) ps[dragging].makeFixed(); } void mouseDragged() { // If dragging, update position if (dragging >= 0) ps[dragging].position().set(mouseX,mouseY,0); } void mouseReleased() { // Stop dragging and put it at rest (0 velocity) if (dragging >= 0) { ps[dragging].makeFree(); ps[dragging].velocity().set(0,0,0); dragging = -1; } } void keyPressed() { if (key=='G') { // more repulsion G+=100; println("G:"+G); } else if (key=='g') { // less repulsion if (G>100) { G-=100; println("G:"+G); } } else if (key=='s') { // stop for (int i=0; i