Quote:Am I going to have to scan through my array of objects to determine if there is a collision?
No you can get Processing to do it for you.
One of the neat features of Processing is being able to make objects of user defined classes e.g. your ellipse reposnsible for its own drawing and mouse handling.
The code below demonstrates this and should be reasonably easy to follow.
Code:
Ball[] balls = new Ball[10];
int tx, ty;
Ball selected;
void setup(){
size(600,600);
for(int i = 0; i < 10; i++){
balls[i] = new Ball();
registerDraw(balls[i]);
registerMouseEvent(balls[i]);
}
}
void draw(){
background(0);
}
public class Ball{
int x,y, radius, diameter;
int fillCol, strokeCol;
Ball(){
int r,g,b;
r = 128+(int)random(127);
g = 128+(int)random(127);
b = 128+(int)random(127);
fillCol = color(r,g,b);
strokeCol = color(r/2,g/2,b/2);
x = 50 + (int)random(400);
y = 50 + (int)random(400);
diameter = 50 + (int)random(100);
radius = diameter/2;
}
boolean isOver(int mx, int my){
return (mx-x)*(mx-x) + (my-y)*(my-y) < radius*radius;
}
// This method is called automatically every loop
// becasue we have registerDraw(this object)
void draw(){
ellipseMode(CENTER);
fill(fillCol);
stroke(strokeCol);
strokeWeight(2);
ellipse(x,y,diameter,diameter);
}
// This method is called automatically every loop
// becasue we have registerMouseEvent(this object)
void mouseEvent(MouseEvent event){
if((selected == null && isOver(mouseX, mouseY))
|| selected == this){
switch(event.getID()){
case MouseEvent.MOUSE_PRESSED:
selected = this;
break;
case MouseEvent.MOUSE_DRAGGED:
x = x + mouseX - pmouseX;
y = y + mouseY - pmouseY;
break;
case MouseEvent.MOUSE_RELEASED:
selected = null;
break;
}
}
}
}