Hi murtlest,
I've been dealing with this same issue while trying to implement some basic GUI elements in Processing, too.
The main idea is to translate the mouse position, which is currently in "screen" coordinates, into "model" coordinates. This works as long as we can assume that the model is on a flat plane parallel to the screen, and it should be simple enough to do by taking the inverse of the model's transformation matrix, and using that to transform our point.
Check out the following code sample for a simple case of translating the screen mouse coordinates to the model coordinates using P3D. Note that this WILL NOT work using Java2D -- the Java2D PGraphics class currently doesn't fill the modelview and modelviewInv matrices with anything useful. I'll be submitting a bug report about this soon.
Quote:
float leftEdge = 20;
float topEdge = 20;
float w = 20;
float h = 20;
color fillColor = color(255,255,255);
void setup() {
size(300,300,P3D);
}
void hitDetectMyRect() {
// Retrieve the inverse modelview matrix.
PMatrix mat = g.modelviewInv;
// Translate the mouse coordinates by half screen size.
// (this is necessary because of how P3D uses transforms)
float mx = mouseX - width/2;
float my = mouseY - height/2;
// Transform the mouse coordinates using the inverse matrix.
float modelX = mx*mat.m00 + my*mat.m01 + mat.m02 + mat.m03;
float modelY = mx*mat.m10 + my*mat.m11 + mat.m12 + mat.m13;
// Do a simple rectangular hit detection.
if (modelX < leftEdge || modelX > leftEdge + w ||
modelY < topEdge || modelY > topEdge + h)
{
fillColor = color(255,255,255);
return;
}
fillColor = color(255,0,0);
}
void draw() {
background(255);
// Translate, rotate, and scale the stage.
translate(width/2,height/2);
rotate(frameCount * (PI / 300));
scale( cos(frameCount * (PI / 300)));
// Hit detect.
hitDetectMyRect();
// Draw the rectangle.
fill(fillColor);
rect(leftEdge,topEdge,w,h);
}
I hope this helps!
An aside: if you're doing your programming from outside of Processing (i.e. using another Java IDE and making your class extend PApplet), let me know -- I'm developing a simple static class that helps translate between screen and model coordinates. I haven't tested it extensively, but it may be of some use to you.
Cheers,
greg