I’m just wondering how do I get the class of the object from a collision result using a ray.
By class, I mean the thing I created in the game, I didn’t know if class was the right word to use…
I can do as much as getting the geometry from the object, but I want to get a custom variable from it.
I tried doing a brief search but never got much as I never really knew what I was looking for…
Edit: I wanted to have RTS style selections and stuff. Maybe there could be a better way that someone could point me to?
I’m not very experimented, so check my answer twice
[java]CollisionResults results = new CollisionResults();
Ray ray = new Ray( G.camera.getLocation(), G.camera.getDirection() );
node.collideWith( ray, results );
CollisionResult closest = results.getClosestCollision();
if ( closest != null )
{
System.out.println( closest.getGeometry().getName() );
}
[/java]
Maybe you can use a HashMap and attach your variable to the Geometry’s name ?
You could also extend Geometry to customize it with a variable.
Do not extend Geometry for this.
Every spatial already has user data and you can attach all kinds of things to it… no reason to subclass just for that.
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#setUserData(java.lang.String,%20java.lang.Object)
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getUserData(java.lang.String)
I see.
Do different objects using the same model have different values?
Also is it possible to link to the object itself through the user data?
Yes. They can have different values. Spatials can only have one parent so that are cloned and not shared in the case of a shared model. Only the meshes are shared for real.
It is possible put custom objects in user data but (unfortunately) they must implement the Saveable (sp?) interface. I usually find it better to stick and ID in there and use a map.
…or… depending on what kind of object it is and what it is used for, sometimes a Control is better. Or a Control that wraps your object. These can be looked up by class on the spatial.
http://hub.jmonkeyengine.org/groups/development-discussion-jme3/forum/topic/bare-bones-shooting-framework/#post-130906
I used a list of stuff, with each thing’s geometry’s name being its position in the list.