import processing.opengl.*; import traer.physics.*; /** * Click on a square to give it a tap "into" the screen. */ ParticleSpringGrid psg; color[] seed = new color[] { ColorGrid.RED, ColorGrid.GREEN, ColorGrid.BLUE }; void setup() { size(256, 256, P3D); rectMode(CENTER); ColorGrid cg = new ColorGrid(8, 8); cg.populate(seed); psg = new ParticleSpringGrid(8, 8, 30, new PSGRColorRects(cg, 25, 25)); } void draw() { psg.update(); background(0); pushMatrix(); translate(20, 20); psg.draw(); popMatrix(); } void mousePressed() { // have to account for the translate in draw Particle closest = findClosest(mouseX - 20, mouseY - 20); closest.addVelocity(0, 0, -10); } Particle findClosest(float x, float y) { Particle closest = psg.get(0, 0); for(int r = 0; r < psg.rows(); r++) { for(int c = 0; c < psg.cols(); c++) { float dc = dist(closest.position().x(), closest.position().y(), x, y); Particle p = psg.get(r, c); float dp = dist(p.position().x(), p.position().y(), x, y); if ( dp < dc ) { closest = p; } } } return closest; }