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);
}
}