I try to have a function who can detect intersection of a vector in a PShape of type Box. I create a function GetIntersection for that. It work for all box's faces execept for Front and Back faces.
public PVector getIntersection(PVector rayOrigin, PVector rayDirection, PShape shape) {
if (shape.getKind() == PShape.BOX) {
// Get the dimensions of the box
float x = shape.getWidth();
float y = shape.getHeight();
float z = shape.getDepth();
PVector frontIntersection = intersect(rayOrigin, rayDirection, new PVector(-x/2, -y/2, -z/2), new PVector(x/2, -y/2, -z/2));
PVector backIntersection = intersect(rayOrigin, rayDirection, new PVector(-x/2, -y/2, z/2), new PVector(x/2, -y/2, z/2));
// Calculate left intersection
PVector leftIntersection = intersect(rayOrigin, rayDirection, new PVector(-x/2, -y/2, -z/2), new PVector(-x/2, y/2, -z/2));
// Calculate right intersection
PVector rightIntersection = intersect(rayOrigin, rayDirection, new PVector(x/2, -y/2, -z/2), new PVector(x/2, y/2, -z/2));
// Calculate top intersection
PVector topIntersection = intersect(rayOrigin, rayDirection, new PVector(-x/2, y/2, -z/2), new PVector(x/2, y/2, -z/2));
// Calculate bottom intersection
PVector bottomIntersection = intersect(rayOrigin, rayDirection, new PVector(-x/2, -y/2, -z/2), new PVector(x/2, -y/2, z/2));
if ( backIntersection != null && pointTouchesBox(backIntersection, shape) ) {
test = backIntersection ;
}
// Check for intersections and return the closest one
if (frontIntersection != null && pointTouchesBox(frontIntersection, shape) ) {
println("return front\n");
return frontIntersection;
} else if (backIntersection != null && pointTouchesBox(backIntersection, shape) ) {
println("return back\n");
return backIntersection;
} else if (leftIntersection != null && pointTouchesBox(leftIntersection, shape) ) {
return leftIntersection;
} else if (rightIntersection != null && pointTouchesBox(rightIntersection, shape) ) {
return rightIntersection;
} else if (topIntersection != null && pointTouchesBox(topIntersection, shape) ) {
return topIntersection;
} else if (bottomIntersection != null && pointTouchesBox(bottomIntersection, shape) ) {
return bottomIntersection;
} else {
// If no intersection was found, return null
return null;
}
} else {
// For other shape types, use the original getIntersection function
for (int i = 0; i < shape.getChildCount(); i++) {
PVector intersection = getIntersection(rayOrigin, rayDirection, shape.getChild(i));
if (intersection != null) {
return intersection;
}
}
return null;
}
}
public PVector intersect(PVector rayOrigin, PVector rayDirection, PVector p1, PVector p2) {
PVector v1 = PVector.sub(rayOrigin, p1);
PVector v2 = PVector.sub(p2, p1);
PVector v3 = new PVector(-rayDirection.y, rayDirection.x, 0);
float dot = PVector.dot(v2, v3);
if (abs(dot) < 0.0001) {
return null;
}
float t1 = cross(v2, v1).z / dot;
float t2 = PVector.dot(v1, v3) / dot;
if (t1 >= 0 && (t2 >= 0 && t2 <= 1)) {
return PVector.add(rayOrigin, PVector.mult(rayDirection, t1));
} else {
return null;
}
}
public static PVector cross(PVector v1, PVector v2) {
float x = v1.y * v2.z - v1.z * v2.y;
float y = v1.z * v2.x - v1.x * v2.z;
float z = v1.x * v2.y - v1.y * v2.x;
return new PVector(x, y, z);
}
I know that the error is in the calculation of the variable frontIntersection and backIntersection but I didn't sucess.