Problem with picking spatial

Hi guys,
Ive started recently learning this awesome JMonkeyEngine by creating some little game, ive had some problems but i solved it, but now im strugling for some time with probably simple mistake. Lets get to the point. My purpose is to detach spatial from scene using ray.
I was taking example from hello collision tutorial, and ive found in this forum working code but this guy was using geometry, but my target is a spatial.
Here is some code:

//loading spatial
shroom = assetManager.loadModel(“Models/boletus/boletus.j3o”);
shroom.setLocalTranslation(0, -17, -10);
shootables.attachChild(shroom);
//harvesting
Geometry geo = clostest.getGeometry();
shootables.detachChild(geo);

Now, when i place a geometry cube it works fine, i can detach it from node, but i cant make it right with spatial.

Im sure i would find working solution here: http://jmonkeyengine.org/wiki/doku.php/jm3:solutions
but this link is not working.

Sincerly,
Begginer

Hm for me the link works, but anyway:

The reason is most likely that you have a deeper tree, eg you dont have a geometry directly ttached in the shootables,
but like
shootables → somenode → geometry

Thus you need to loop via getParent upwards and remove the correct spatial from shootables.

I get the point, but i dont know how to make it works. For example, if i do
[java]
Geometry hitedObj = closest.getGeometry();
Node hitedObjNode = hitedObj.getParent();
String name = hitedObjNode.getParent().getName();
shootables.detachChildNamed(name);
[java]

it works for shroom, but not for simple cube (because its attached directly to shootbales node).
And this link is redirecting to page http://wiki.jmonkeyengine.org/doku.php where i cannot find solutions to tasks.

You can loop over the parents of a node and if it is a direct child of shootables, you detach it.

So roughly

    while(node != null) {
    if(shootables.getChild(node) != null) {
    shootables.detachChild(node);
    return;
    }
    node = node.getParent();
    }

There is no such getChild() method as you are calling.

Really, the easy way is to just see if the spatial’s parent is shootables ( == shootables) and if not then go one parent up.

The more holistic way is to take the models you load with some user data and then check for that.

Yeah I meant getChild(node.getName()) or something like that, it should just convey the general idea. But you are right of course.

The problem is that i cant get any node directly from CollisionResult (there is only getGeometry method). So this could work but only in some cases. If i create simple geometry i only need to do closest.getGeometry() but if there are more nodes i need to do this loop cuple times;
I was trying something like that:
[java]
Node node = closest.getGeometry().getParent();
while(node != null)
{
if(node.getParent().getName().equals(“shootables”))
shootables.detachChildNamed(node.getName());
else
node = node.getParent();
}
[/java]

But i keep getting NullPointerException ;c

General rule: don’t bother telling anyone about exceptions without a stack trace.

You all are making this way too hard… makes me wonder if you are perhaps reaching beyond your grasp a bit but here goes…

for( Spatial s = closest.getGeometry(); s != null; s = s.getParent() ) {
    if( s.getParent() == shootables ) {
        // Do the stuff.
        break;
    }
}
2 Likes

Thats exactly what i was looking for. Works great!
Thanks!

Have you tried this?

target.removeFromParent();