Get parent matches for geometry?

I’m curious is there an equivalent of descendant matches for getting parent matches. I have a temporary storage node I use for collision. Once I do the ray cast it returns the closest collision as a geometry. I want to travel up the geometries nodes to find the parent node that holds a control.

The only solution I could think of involves a recursive function and as a rule of thumb I avoid those types of calls.

Is the geometry attached to a node which contains the control you are looking for? Or is there a hole hierarchy of nodes to climb up to get the desired control?

Why exactly do you not use recursive calls?

My question is more why do you need a recursive call? I mean just crawl up the parents in a loop and look which one do have your desired control and stop if there is no parent any more? Or what exactly do I miss?.

I just used the spatial I loaded into my scene as a reference for the possible max amount nodes.

public Spatial getParentControl(Spatial spatial){
    
    for (int loops = 0;loops < scene.node.getQuantity();loops ++){
      if (spatial.getControl(ObjectControl.class) != null){
         return spatial;
     }  else {
          spatial = spatial.getParent();
          if (spatial == null){
              // You have reached the root
              return null;
          }
      }
    }
    return null;
}

You could easily convert this to get any control you wanted. I think it would be handy to a feature like this into JME.

This loop is not doing anything but wasting time.

Finding a parent:

for( Spatial s = spatial.getParent(); s != null; s = s.getParent() ) {
    // if it's the spatial we are looking for then done
}

@pspeed Thanks , that works and is much shorter. I changed it up a bit to work directly with geometry since I’m using it to get a collision result. For anyone interested.

public Spatial getParentControl(Geometry geometry){
    
    for(Spatial spatial = geometry.getParent(); spatial != null; spatial = spatial.getParent() ) {
       if (spatial.getControl(ObjectControl.class) != null){
         return spatial;
        }    
     }
    return null;

}