Well, one of the sample sketches for the picking library is a pretty good start IMHO.
I just turned the cubes into spheres, and removed the stroke color. Bam:
- /**
- * @author Nicolas Clavaud <antiplastik@gmail.com>
- * With slight modification by TfGuy44 to use spheres instead.
- */
- import picking.*;
- Picker picker;
- Cube[] cubes;
- float rotation = 0;
- void setup() {
-
- size(200, 200, P3D);
-
- picker = new Picker(this);
-
- cubes = new Cube[10];
- for (int i = 0; i < cubes.length; i++) {
- cubes[i] = new Cube(
- -15 + (int)random(30),
- -15 + (int)random(30),
- -15 + (int)random(30),
- 5 + (int)random(15)
- );
- }
-
- }
- void draw() {
- rotation += 0.002;
- if (rotation > TWO_PI) rotation -= TWO_PI;
-
- camera(-20, -20, 50, 0, 0, 0, 0, 1, 0);
-
- rotateY(rotation);
-
- background(0);
- lights();
- for (int i = 0; i < cubes.length; i++) {
- picker.start(i);
- cubes[i].display();
- }
-
- }
- void mouseClicked() {
-
- int id = picker.get(mouseX, mouseY);
- if (id > -1) {
- cubes[id].changeColor();
- }
-
- }
- class Cube {
-
- int x, y, z, w;
- color c;
-
- Cube(int x, int y, int z, int w) {
- this.x = x; this.y = y; this.z = z; this.w = w;
- this.changeColor();
- }
-
- void changeColor() {
- c = color(
- int(random(0, 255)),
- int(random(0, 255)),
- int(random(0, 255))
- );
- }
-
- void display() {
- fill(c);
- noStroke(); // Added
- pushMatrix();
- translate(x, y, z);
- //box(w); // Removed
- sphere(w); // Added
- popMatrix();
- }
-
- }
Of course, you'll probably want your menu to actually do something other than change the color of the clicked sphere...