This function returns true if the given ray intersects a box of size s with a placement determined by the current geometry transformations applied to the scene. The ray can be automatically computed from the screen coordinates as in the following example:
void setup() {
fullScreen(P3D);
}
void draw() {
background(200, 0, 150);
translate(width/2, height/2);
if (mousePressed && intersectsSphere(70, 0, 0)) fill(0, 0, 255);
else fill(255, 0, 0);
box(70);
}
A ray with arbitrary origin and direction can also be provided:
PVector origin = new PVector();
PVector direction = new PVector();
void setup() {
fullScreen(P3D);
}
void draw() {
origin.set(width/2, height/2, 0);
direction.set(-width/2, -height/2).normalize();
background(200, 0, 150);
translate(width/2, height/2);
if (mousePressed && intersectsSphere(70, origin, direction)) fill(0, 0, 255);
else fill(255, 0, 0);
box(70);
}
The function accepts different width/height/depth values for the box:
void draw() {
// ...
translate(width/2, height/2);
if (mousePressed && intersectsBox(100, 120, 80, origin, direction)) fill(0, 0, 255);
else fill(255, 0, 0);
box(100, 120, 80);
}