Problem with instancing?

Hey guys I posted a few days ago about using a mouse click to cast a ray and intersect with a geometry, then using that geometry work backwards to find a stored class within it (an Actor class I made). I seem to have hit another issue and I have tried to fix it in several different ways, but I’m afraid I am simply missing something. My issue is when I load my players (I have multiple using the same model) I insert my ref to the Actor class and all is well at this point ( I made some debug checks and it is all stored correctly). The issue comes up with the raytrace. After my models are all added to the rootNode they have numbers tacked on to them (IE: SinbadMesh-7) and they no longer have anything in the userData portion. I think this has to do with instancing, but I am not quite sure how to deal with it. I have tried adding my user data at different times (After it is added to the root node) and other attempts, but no dice. Any advice would be much appreciated.

If you call setUserData() on a node then that user data will be there later.



If you call getUserData() on a spatial and do not see that user data then it is not the same spatial.



There is no magic… it’s essentially just a HashMap inside Spatial.



We would need more information to debug your problem.

1 Like

It would appear I found and solved my own problem. After looking more at the API I remembered that a model is simply like a spatial and can have several layers, sure enough I just wasn’t grabbing the right parent. At the base I am colliding with the actual mesh, whose parent is my model, whose parent is the is the node it is attached to. My data is correctly stored in the first parent, and works perfectly. Sorry for wasting anyones time, it was a silly mistake, thanks for the help either way, cheers.

For collision detection type stuff, I frequently end up with loops that look something like:

[java]

Object myObject = null;

for( Spatial s = hit; s != null; s = s.getParent() {

myObject = s.getUserData( “myData” );

if( myObject != null ) break;

}

[/java]



Then it doesn’t matter how deep my hierarchy is and I can change things later and they don’t break. I pack all of my model-specific user data into its root node and I’m done.

1 Like

Thanks very much for sharing some of your tricks, I had something similar in place, but your method is much more elegant, thanks again :3!

@pspeed said:
[java]
Object myObject = null;
for( Spatial s = hit; s != null; s = s.getParent() {
myObject = s.getUserData( "myData" );
if( myObject != null ) break;
}
[/java]


Yeah, very elegant method :).