Trouble with mouse picking certain models[Solved]

I have been following the HelloPicking tutorial and trying to implement the concepts into my own project.

When I click the model while the application is running, the model should be removed from a node that is attached to the rootNode.
This works fine for test-data objects such as teapot but not for objects that I have imported into the game.

I am converting .blend files into .j3o format before initializing them. Any idea why my models aren’t working when the test-data models work fine?

The closest collision with the ray is registering as the correct spatial, and the System.out.println(s); //(where s is the closest spatial from the rays beginning)
is correctly printing the chest’s geometry.

Additionally, I’ve tried importing multiple models besides the chest

This is an example of initializing the teapot:

   Spatial teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
         Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
         teapot.setMaterial(mat_default);


This is an example of initializing a chest of my own:

  Spatial chest = assetManager.loadModel("Models/chest.j3o");
         Material mat_chest = new Material(assetManager,
         "Common/MatDefs/Light/Lighting.j3md");
        TangentBinormalGenerator.generate(chest);
          mat_chest.setTexture("DiffuseMap", assetManager.loadTexture("Models/wood_planks.jpg"));
       mat_chest.setTexture("NormalMap", assetManager.loadTexture("Models/wood-normal.jpg"));

          mat_chest.setColor("Diffuse",ColorRGBA.White);
          mat_chest.setColor("Specular",ColorRGBA.White);
          mat_chest.setFloat("Shininess", 64f);  // [0,128]
          chest.setMaterial(mat_chest);

Attaching them to the nodes:

 pickables.attachChild(chest);
 pickables.attachChild(teapot);
 rootNode.attachChild(pickables);

And here is the onAction method for detaching the child:

  if(name.equals("get") && !keyPressed){
        
         CollisionResults results = new CollisionResults();
          System.out.println("casting ray");
         
         Vector2f click2d = inputManager.getCursorPosition().clone();
         Vector3f click3d = cam.getWorldCoordinates( click2d, 0f).clone();
         Vector3f dir = cam.getWorldCoordinates( click2d, 1f).subtractLocal(click3d).normalizeLocal();
         Ray ray = new Ray(click3d, dir);
         
         pickables.collideWith(ray, results);
           
            if (results.size() > 0){
           
           CollisionResult closest = results.getClosestCollision();
           Spatial s = closest.getGeometry();     
          
           System.out.println(s);
          pickables.detachChild(s);
          
       }
     
       }

How you setup your hierarchy for the model depends on how you get the node you want to remove. If the geometry is nested under the parent node you could do something like this.

Node chest = closest.getGeometry().getParent();
pickables.detachChild(chest);

If the geometry is nested under other nodes then you need to navigate to the parent node.

1 Like

Thanks a ton for the pointer in the right direction! I ran into a similar problem when I was implementing the animations.

Here is a solution that works for multiple types of hierarchies

  CollisionResult closest = results.getClosestCollision();
         Spatial s = closest.getGeometry();     
         if(s.getParent().getName() == "pickables"){
             pickables.detachChild(s);
         }
         else{ 
          System.out.println(s);
         pickables.detachChild(s.getParent());
         }
         
      }
1 Like

gotcha thanks.

for this method I’ve moved all of the geometries into the main node of the .j3o file, I’m not sure if this will effect skeletal attachments as I haven’t implemented that yet, if it does you can return the .j3o file to its original form and edit the code to find the correct path.