Method to get ancestor of a given class

This is just a small tweak that I thought might be useful to others.



I often extend Node for more complex objects that I have in my scene, and when I do a collision check I often want to trace it back from the Geometry and get the instance of the Node subclass. For example, if I have a class Building which extends Node, a window pane geometry might trace back as Geometry → Node → Node → Building or something like that.



So I wrote two small methods that either get or check for an ancestor of the class given. I’d love to see these integrated into Spatial, but the provided methods are static methods that can be placed anywhere.



EDIT: Removed the code, see Normen’s reply below.

1 Like

Extending node should really not be the way to add functionality to Spatials, rather use Controls. They have all these functions (get one by class etc) and can be added to any spatial without that spatial having to be a special class type.

Ah nice, sorry about that. I thought Controls were just for physics and movement. Looks like I have some rewriting to do.

You can use them for anything and since extending is evil (e.g. you have a MovableNode and an OpenableNode → You cannot have an Openable, Movable Node ;)) they are a viable alternative. Via the spatial they can even communicate (e.g. when attached check if another needed control exists on the spatial like if(spatial.getControl(MyControl.class)!=null){doStuffs();}) and exchange data with each other (or use the spatial’s UserData).

1 Like

Nice, that actually makes a lot more sense than the way I was doing it. Thanks for the tips :slight_smile: