Shooting objects: Problem with geometry assignment

Hi there,

i`m loading spatials via:
[java]
Spatial flashlight = assetManager.loadModel(“Models/flashlight/flashlight3.j3o”);
[/java]

Now i attach them to the rootNode and try to shoot them. I use this code for shooting inside the onAction-Method of the ActionListener-Class:
[java]
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
rootNode.collideWith(ray, results);

        for (int i = 0; i < results.size(); i++) {
            CollisionResult colRes = results.getCollision(i);
            String hit = colRes.getGeometry().getName();
            float distance = colRes.getDistance();
            System.out.println(hit);
            break;
        }

[/java]

Works fine so far! The Problem is that the spatial`s name (variable: hit) is “flashlight”. When i try to set the name of the spatial during import with “flashlight.setName(“SHOOTABLE_Flashlight”);” it has no effect. And when i cast it to Node and check all names of the Node-children, there is no child with the name “flashlight”.

Now the question: How can i set a Geometry name during asset import so i can see this name when i shoot it?

I need some kind of mapping here because some loaded spatials are wrapped by another class. This classes contain the spatial itself and some other information about this spatial. I want the name to map it to the class these geometry belongs to.

Thanks!

flashlight.setName(name);

no that does not work. assetManager.loadModel… returns a Spatial that is an instance of Node, not of Geometry. And in the collision result i get something of type geometry. So these two objects can not be the same ones.

If only there were some way to get the parent node… or to search the forum for a similar question that is posted two times a week… :wink:

2 Likes
@dano said: no that does not work. assetManager.loadModel.. returns a Spatial that is an instance of Node, not of Geometry. And in the collision result i get something of type geometry. So these two objects can not be the same ones.

You can set the name of any Spatial, whether it’s a Geometry or a Node. If the collision result is a descendant of the named node, you should be able to search rootward in the scene graph to find the named node, like so:
[java]
Spatial spatial = colRes.getGeometry();
while (spatial != null) {
if (spatial.getName().matches(pattern)) {
return spatial;
}
spatial = spatial.getParent();
}
return null;
[/java]

Most recently:
http://hub.jmonkeyengine.org/forum/topic/scene-graph-insanity/

Okay. you are right. works like a charm! it`s really late and my brain is almost empty =)

1 Like