Howdy, I'm new here. This is my first time working in JAVA, thanks to my university's school course. I decided to start a project to try and learn JAVA better, but all I could think of required 3D work.
As such, here I am.
Ok, now to the question… I've created my own gamestate, and an absoluteMouse as well. Following the wiki's MousePick example, I've been doing mouse pick tests. But… I want to be able to retrieve some sort of identification of the objects the mouse hits, but I'm not finding how to. For example, I'm creating the default "My Box", and attaching it to the rootNode. I perform a findPick on the rootNode with a ray from my mouse cursor. But the pickResults only return a geom, not the node (I think ). I'd like to some how retrieve the "My Box" node name, or something like that. I know the picking works, because the randomColors() function works just fine.
Any thoughts? What simple area am I totally missing?
Thanks in advance!
Try traversing the scenegraph towards the root, in other words, ask the geometry you get from the pick result to return it's parent.
Geometry geom = pickResult;
SceneElement element = geom.getParent();
while (!(element instanceof Node)) { // you might check other things, eg !(element instanceof GeomBatch) or !(element instanceof Spatial)
element = element.getParent();
}
System.out.println(element.getName());
Well, I'm not sure why, but your code didn't work, but with some tweaking, you lead me the right direction.
// Create a ray starting from the camera, and going in the direction
// of the mouse's location
Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
// Does the mouse's ray intersect the box's world bounds?
pr.clear();
rootNode.findPick(mouseRay, pr);
for (int i = 0; i < pr.getNumber(); i++) {
pr.getPickData(i).getTargetMesh().setRandomColors();
Geometry geom = pr.getPickData(i).getTargetMesh().getParentGeom();
System.out.println(geom.getName());
SceneElement element = geom.getParent();
System.out.println(element.getName());
//SceneElement element =
/*while (!(element instanceof Node)) { // you might check other things, eg !(element instanceof GeomBatch) or !(element instanceof Spatial)
element = element.getParent();
//element.
System.out.println(element.getName());
}*/
}
Outputs:
My Box
rootNode
Strange, but it works. I think it's because I didn't create a node for the box, now that I think about it. I'll give that a go and see. (By the way, I had some trouble with "element.getParent()", reporting "no symbol found" for "getParent()", hence the commenting out.)
EDIT: Sure enough, I didn't create a node for the box, but after doing so, my results are now:
My Box
My Box Node
Thanks again!
You don't have to create a Node, it's just if you did and were looking for that node you would check against "instaceof Node". Probably it's also a good idea to check if your element has a null parent (no parent) if you are trying to loop it.