So i have a geometry geom.
i assign an userData to it :
[java]geom.setUserData(“ID”,coord[4]);[/java]
Then i need to switch it to a model so i just switch it :
[java]model.setUserData(“ID”,coord[4]);[/java]
But then the peice of code that didnt change in the mouseEvent catcher is :
[java]if(results.size() > 0) {
Geometry geom = results.getClosestCollision().getGeometry();
float temp = -1;
temp = geom.getUserData(“ID”);
mainTarget = object.getObject((int)temp);
} [/java]
But then it gives me this error :
[java]SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at Rooms.MainLand$MouseEvent.onAnalog(MainLand.java:249)
at com.jme3.input.InputManager.invokeAnalogs(InputManager.java:244)
at com.jme3.input.InputManager.invokeUpdateActions(InputManager.java:214)
at com.jme3.input.InputManager.update(InputManager.java:852)[/java]
I’ve tested it, and it is only because the object is now an spatial, because with the geometry it worked just fine. Any Sugestions?
Thanks.
It sounds like you are now setting the user data on a different object than you are looking at. There is no magic here. If you set a user data on object A then object B does not magically also get that user data. (And thank goodness, too)
You can either continue to set it on the geometries, too… or you can look for the parent of the Geometry until you find the spatial that actually has the ID user data.
No, the geometry i got is from CollisionResults
I know that it is the spatial i find as result but i cant get a spatial out of it so i get the geometry but i can’t get the userData i put in my spatial.
So it is the same object.
@mathieu-roux222 said:
No, the geometry i got is from CollisionResults
I know that it is the spatial i find as result but i cant get a spatial out of it so i get the geometry but i can't get the userData i put in my spatial.
So it is the same object.
I'm not even sure I understand what you are saying at this point. If you set user data on an object then it will be there later. It will not be on a different object.
[java]
Geometry someGeom = new Geometry("foo", mesh);
Spatial someSpatial = new Node("bar");
someSpatial.attachChild(someGeom);
someSpatial.setUserData("ID", 1234);
someGeom.getUserData("ID") == null because it was never set
[/java]
The only way getUserData("ID") would return null is if you never set any user data on THAT object. (or if you cleared it somewhere but then I expect you would have mentioned that.)
Most of the time when you load a model it is not the same as Geometry. It will have some number of Geometry objects underneath it. When you do collision detection, you get the Geometry and NOT the model. You have to do getParent() to find the model (and potentially getParent().getParent() or getParent().getParent().getParent()... or make a loop).
No, I have an Spatial :
[java]Spatial player = assetManager.loadModel(path);[/java]
Then i attach it a user data :
[java]player.setUserData("ID", coord[4]);[/java]
But with this peice of code :
[java]if(results.size() > 0) {
Geometry geom = results.getClosestCollision().getGeometry();
float temp = -1;
temp = geom.getUserData("ID");
mainTarget = object.getObject((int)temp);
} [/java]
It gives me this error :
[java]SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at Rooms.MainLand$MouseEvent.onAnalog(MainLand.java:249)
at com.jme3.input.InputManager.invokeAnalogs(InputManager.java:244)
at com.jme3.input.InputManager.invokeUpdateActions(InputManager.java:214)
at com.jme3.input.InputManager.update(InputManager.java:852)[/java]
To sum up : I have a spatial player with a user data of x;
Then i have a function to get watever object was click, so is user CollisionResults.
The CollisionResults gives me a geometry, but when i want to get the userData out of it, it gives e an error.
Hope you understand
Mathieu, its you who doesn’t understand. Its not the same spatial.
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies
Edit: Open the model in the Scene Explorer and look at it, then it should be clearer. If not, System.out.println() the name of the model you loaded and the geometry you get as collision result. Then look at the model again in the Scene Explorer
Ok, so i assumed that the model has a name, and the geometry has another name, but is there anyway to get the model user data out of a geometry? If not then how can i do it because CollisionResults do not support Spatials? If no then how can i make this?
@mathieu-roux222 said:
Ok, so i assumed that the model has a name, and the geometry has another name, but is there anyway to get the model user data out of a geometry? If not then how can i do it because CollisionResults do not support Spatials? If no then how can i make this?
Ugh... I told you twice already. Get the Geometry's parent.
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/Spatial.html#getParent()
If that's not the right one then get that one's parent and so on. It's best to construct a simple loop to getParent() until you find the one with your ID on it. This is a trivial loop to write... but maybe I should include it anyway.
[java]
for( Spatial s = geom; s != null; s = s.getParent() {
if( s.getUserData("ID") != null ) {
....do stuff...
break;
}
}
[/java]
Thanks so much, i still not comletly understand, but one day i will, i have an idea on how that works but i’ll learn more in further development anyway:
Thanks to all who have helped me you made it works. Thanks! and a big thanks to you Pspeed!