How to get Node object where a Geometry or Spatial belongs to?

I know may be this is really a silly question. And it’s more if Geometry class has a method getParent() that will return a Node object…but



My problem is,

I have a model, it’s really a simple model, this model(Spatial) is attached as a child to a Node, model has several meshes.



When I try to select the corresponding node depending on which geometry is returned by:



Vector3f mousePos=new Vector3f();

ExtHashMap<Node,Vector3f> Coll=new ExtHashMap<Node,Vector3f>();

Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);

Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);



direction.subtractLocal(origin).normalizeLocal();



CollisionResults results = new CollisionResults();

rootNode.collideWith(new Ray(origin, direction), results);



if (results.size() > 0) {



mousePos=results.getClosestCollision().getContactPoint();

Coll.put(results.getClosestCollision().getGeometry().getParent(),mousePos);

System.out.println(“Debug Object:”+results.getClosestCollision().getGeometry().getName());



}



Now results.getClosestCollision().getGeometry() will get which geometry ray is colliding to, that’s ok, but I want to know to what node this geometry belongs to… Each time I run this and ask for a parent node that I’m supposing that will return node where this geom is attached to, I just get geometry not a node…:



I’m not getting a node… I’m getting a Spatial/Geometry



Debug Object:Symbol_And-geom-1

Debug: Current Selected node to Drag:Symbol_And-ogremesh

Debug: Current Selected node to Drag:Symbol_And-entity

Debug Object:Base_And-geom-1

Debug: Current Selected node to Drag:Base_And-ogremesh

Debug: Current Selected node to Drag:Base_And-entity

X=3.33 Y=4.95

Debug Object:Frame_And-geom-1

Debug: Current Selected node to Drag:Frame_And-ogremesh

Debug: Current Selected node to Drag:Frame_And-entity

X=3.31 Y=5.0099998





I need to get correct node, where a Geometry or Spatial belong to… how do I do that???

You sometimes have to crawl up the node tree getParent().getParent().getParent(), testing each time if it is the node you are looking for.

You can use a specific naming convention on the node (such as “Character_blahblahblah”) or set user data on the node and identify it with that.

Thank you for your fast answer… I was thinking about that… so I have some wrong understanding of Nodes, I was thinking that getParent() will only return a node…(Yeah, now realized Spatial will be include as “node” as well :slight_smile: ) but then accordantly to your explanation, it will return the last “branch” of a tree I need to look more to docs… lol



Actually, I have Named each node like “NODE:XXX” but, I guess I will also use user Data to specify who direct parent is …



Thanks mate, I’m really enjoying this…

Check out this, it explains how the scene graph is structured:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

@xquake said:so I have some wrong understanding of Nodes, I was thinking that getParent() will only return a node...


getParent() _will_ only return a Node. Always.
@normen said:
Check out this, it explains how the scene graph is structured:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies


Great, thanks... I will now continue with my program...