Long initial pause/freeze on mousepicking

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;

Is there something I’m doing wrong or a better way to optimise this so there is no initial freeze?

When you first click an object, the Meshes that don’t have collision data need to generate it:

You can either do this manually right after you load it or just run a collision on the loaded models. You will get a pause either way but you might be able to spread it out at a less obvious time.

It can be done on a background thread also but only before you’ve attached it to the scene graph. So that dance is a little more complicated.

1 Like

Perfect, thanks. Mesh.createCollisionData() is just what I was after.