HelloPicking Tutorial (detach model problem) [Solved]

I am no programmer and i learn from your tutorials. I am on 8 Hello Picking in end and doing Exercise 3: Pick up into Inventory and trying make detach model (oto - robot). But it is not working. Boxes working perfectly and are disappear if i click on box.

here is my code for detach
[java]Spatial oto = assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);
oto.setLocalScale(0.5f, 0.5f, 0.5f);
oto.move(0, 0, -5);
/** **/
[java]if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
Geometry geom = (Geometry) rootNode.getChild(closest.getGeometry().getName()); // i have this code from forum because i dont know how to use closest.getGeometry().getName() from tutorial

it is working onli if i have this code:
but oto disappear always if i click on any box or oto in scene.

Is there any solution? i doing something wrong or for detach model is something special code?

It’s because you need to remove the top of the oto hierarchy of spatials, collision results return geometries, so u need to check the parent nodes and traverse up, until you find the right one, or do it some other way


thanks for trying help, but it dont help. I read scene graph from zarch link and more (spatials, nodes) but this i think, that all i know how nodes are working. What i dont know is the programing and maybe logic of programing. What is in my code, i think it is all logical and i dont see any reason why this code dont work for xml.models. Oto is addet to scene by shootables node by attachChild. Shootables node is added into scene by rootNode.attachChild. In “click to object” code is detect geometry and his name and detachChild by this info. And this working only for geometry created by jME engine (box, sphere). Anyway, this code detect oto and write it to program output “You shot Oto-geom-1 at (-0.56539834, -1.824694, -4.8908176), 14.004654wu away.” then i dont understand why not this code dont detach this model object.

just traverse up, something like:

Spatial s = geom;
while (s != null) {
if (s.getName().equals (“oto”)) {
shootables.detachChild (s);
s = s.getParent (); // check parent
} [/java]

Thanks wezrule. This is working. But i think this is not good for more objects, because you must write for each model object (which you want detach) another "if (…equals (“name)” code. I go brood about this, how to do it for more object easy.

i load more models (ninja and teapot) and add it to shootables, nothing more. If i run application, only teapot disappear without wezrule code. And what i discover is, if i click on teapot, output give me information teapot-geom-0. Oto and Ninja have geom-1 and cant detach, for that reason i think, there is the problem for detach models. What represent the number? How i change this number or detect the number?

If you need to do it for a lot of different models, then u could add the same UserData to each model, then check for that instead of a name.

public abstract class Clickable implements Savable {

public abstract void clicked();

public abstract void clickHeld();

public abstract void clickDragged(float x, float y);

public abstract void doubleClick();

public void write(JmeExporter ex) throws IOException {
    throw new UnsupportedOperationException();

public void read(JmeImporter im) throws IOException {
    throw new UnsupportedOperationException();


I’ve created an abstract class clickable - I then have a Click app state that listens to the click, traverses looking for the user data, calls the appropriate click/etc method in it. This means that if you attach a Clickable user data to any object on the scene then if that object or any of its children is clicked the app state will call the clickable. I tend to then use anonymous classes for the actual clickable implementations.

Zarch this is interesting. But now for me only for future. I am not able doing own codes. All what i creating is now from existing codes, understanding them, and make own codes from examples.

I am solve my problem for 90% :).
Here is code what i found in google and add little edit for my need.
[java]Spatial cl = shootables.getChild(results.getClosestCollision().getGeometry().getName());
if (cl != null) {
Spatial cl2 = pickables.getChild(results.getClosestCollision().getGeometry().getName());
if (cl2 != null) {
Now if i click on model or geometry in shootables is disappear(detach from scene). But not excepted as i want. From models detach only meshes, for example, if i click on ninja model, ninja model disappear, but his sword not. Of course, if i click on sword, sword disappear.