I am using LibGDX trying to use raypicking to pick a point on a Mesh. I have two methods for this that don’t seem to work quite right:
/**
* @param ray
* @param intersection The vector to store the intersection result
* @return
*/
public boolean intersect(Ray ray, Vector3 intersection) {
final BoundingBox bb = new BoundingBox();
for (int i = 0; i < modelInstances.size; i++) {
ModelInstance instance = modelInstances.get(i);
instance.calculateBoundingBox(bb).mul(instance.transform);
if (Intersector.intersectRayBoundsFast(ray, bb)) {
return intersect(ray, models.get(i), instance.transform, intersection);
}
}
return false;
}
private boolean intersect(Ray ray, Model model, Matrix4 transform, Vector3 intersection) {
final Matrix4 reverse = new Matrix4(transform).inv();
for (Mesh mesh : model.meshes) {
mesh.transform(transform);
float[] vertices = new float[mesh.getNumVertices() * 6];
short[] indices = new short[mesh.getNumIndices()];
mesh.getVertices(vertices);
mesh.getIndices(indices);
try {
if (Intersector.intersectRayTriangles(ray, vertices, indices, 4, intersection)) {
intersection.mul(transform);
return true;
}
} finally {
mesh.transform(reverse);
}
}
return false;
}
The first method calculates if the ray intersects the bounding box of each mesh, if an intersection is found, the second method should calculate exactly where the intersection occurs.
The models I am checking right now are chunks of a terrain created with a height map. It seems that both methods have mistakes in them.
In the second method, I apply the transformation of the model instance to the mesh (since the mesh’s themselves do not have any transformations. After calculating the intersection I transform the mesh with the inverse (which works).
Is there something I am missing here? The method that calculates the bounding boxes seems to always have false positives, also the intersection results are always incorrect.
How do I correctly intersect with bounding boxes?
How to consider transformations when checking intersections with the meshes?
Are the intersection results the world coordinates for the intersection?
How do I check for the closest intersection when there are multiple intersections with the mesh?