/** * spxlChaser2D3D by subpixel
* http://subpixels.com
* 2009-12-27
*
* A mouse follower with a 3rd dimension...
* Controls:
* [space]: Pause
* [Z]: toggle Z dimension
* [R] / mouse click: randomise all (also on mouse click) */ import processing.opengl.*; final int numChasers = 5; Chaser[] chasers = new Chaser[numChasers]; PVector target; float targetSize = 10; // Display paused? boolean paused = false; // Use Z dimension? boolean useZ = true; // Clock time for end temporary pause long waitUntil = 0; void setup() { size(400, 400, OPENGL); frameRate(10); // slowed down to see motion detail chasers = new Chaser[numChasers]; for (int i = 0; i < chasers.length; i++) { Chaser chaser = new Chaser(); chasers[i] = chaser; } target = new PVector(mouseX, mouseY); waitMillis(1000); display(); } void draw() { update(); rotateX(PI / 4f); // Adjust the global view display(); } void update() { // Target the mouse target.set(mouseX, mouseY, 0); if (useZ) target.z = -250 + 250 * sin(millis() * 0.001); // Update the chasers for (int i = 0; i < chasers.length; i++) { chasers[i].update(target, targetSize); } } void display() { // Update display background(0); // Display the target pushMatrix(); // Show vertical pole upon which the target oscillates up and down if (useZ) { stroke(255); line(target.x, target.y, 0, target.x, target.y, -500); } translate(target.x, target.y, target.z); noStroke(); fill(255, 0, 0, 255); sphere(targetSize); popMatrix(); // Display the chasers for (int i = 0; i < chasers.length; i++) { chasers[i].display(); } } // Set a temporary pause state void waitMillis(long millisecondsToWait) { waitUntil = millis() + millisecondsToWait; } void keyPressed() { switch(key) { case ' ': paused = !paused; break; case 'r': case 'R': randomiseAll(); break; case 'z': case 'Z': useZ = !useZ; break; } } void mousePressed() { randomiseAll(); } public void randomiseAll() { for (int i = 0; i < chasers.length; i++) { chasers[i].randomise(); } waitMillis(1000); }