Hello. I’d like some advice on how to optimise my mousepicking behaviour.
When the game first loads and the player first picks something with the cursor, there’s about a 2 second pause - the game freezes - when retrieving the closest node. However, subsequent clicks are instant and work as expected.
[java]CollisionResults results = new CollisionResults();
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
Ray ray = new Ray(click3d, dir);
rootNode.collideWith(ray, results);
if (results.size() > 0) {
Node clickedNode = results.getClosestCollision().getGeometry().getParent(); // Long initial pause occurs here
return clickedNode;
}[/java]
Is there something I’m doing wrong or a better way to optimise this so there is no initial freeze?