Ray casting on custom objects

I have another question. The ray collision aglorithm does a great job at retrieving geometries.



However, I have wrapped my objects in items classes. Each item has its own node, and that node has a unique name. Some items are

geometries from basic shapes within jmonkey, others are imported from j3o files immediately. If i name each node in the item class, is there a way to access that name from the geometry? or from the collision in general?

spatial.getParent()

sorry the problem wasn’t that. I knew about spatial.getParent(). But it gave me the wrong geometry for the diamond. Here’s some code:





public static Node createModel(String location, String id)

{

Node itemNode = (Node) app.getAssetManager().loadModel(“Models/”.concat(location).concat(".j3o"));

itemNode.setName(id);

return itemNode;

}

///// The itemNode is returned with the string id “Selector”



public Selector(Game g,String id)

{

super(g);



Node n = ItemCreator.createModel(“Selector/Selector”,id);

n.scale(0.25f);



selection = true;

sinUpdate = 0;



setNode(n);



this.g = g;

}



/////// This node is then set as the node of this item



private static void createCursor()

{

S = new Selector(g,“Selector”);

attach(S);

}



//// When I create the Item: Selector, i give it the proper String id



private static void attach(Item i)

{

SceneNode.attachChild(i.getNode());

}



///// It’s attached to the scene



While everything else works properly (the boxes for example, the floor and the sky)

System.out.println(closest.getGeometry().getParent().getName()); <— this returns “plane” for the selector



But the word plane isn’t in any source file, so i don’t get it :frowning:

An easy way to flag your nodes as a parent node that you care about (such as your item node) is to set “user data” on it.



myNode.setUserData(“item”, “myItemName”);



Then in the collision put your spatial.getParent() in a loop:

[java]

spatial spat = collisionResult.getClosest

while() {

if (spat.getUserData(“item”) != null)

// found the item spatial!

else

spat = spat.getParent()

}

[/java]

What is is doing it climbing up looking for any nodes that are flagged as “items”. You can then get the name from that.

that’s really nice, I’ll try it out, thanks!

you were completely right. The selector had two nodes as parents. Thanks!

No problem, glad you got it working.