Collision and picking¶
A collision query answers a geometric question, such as which object a ray intersects. It does not, by itself, simulate physics or move objects realistically; for rigid bodies and physics collision events, see Physics.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
CollisionResults hits = new CollisionResults();
rootNode.collideWith(ray, hits);
if (hits.size() > 0) {
CollisionResult closest = hits.getClosestCollision();
Geometry geometry = closest.getGeometry();
Vector3f point = closest.getContactPoint();
}
Cast against a node containing the objects you want to inspect, rather than every spatial in the application. To pick from a mouse cursor, first convert the cursor position through the camera into a world-space ray. The mouse-picking guide walks through that process.