# How to find the closest node of a given type?

Hello, I need to for my game be able to locate the closest node to a spatial of a give type (parent node troop, cover… etc.), and the only thing I have found so far is a scene visitor, which led me to this mess after a lot of experimenting:

[java] public Node findClosestNode(Node nodetype, Spatial troop){

public Node visit(Node node, Spatial troop) {
// search criterion can be control class:
NavCalculator nav = new NavCalculator;

``````if (node != null &amp;&amp; nav.getDistance(troop, node) &lt;= 2000){
}
``````

}

``````};
``````

// … or scan it breadth first.

}[/java]

Frankly, I’ve give it my all and can’t figure out how to simply locate the closest node that is within a range defined by an interface variable, can someone help?

Thank you all very much.

I’m terribly sorry to bump this, but it is the one thing holding me back.

I am going to try to knock it out myself today, though.

Man, be nice if I had a partner for this. All this phsyics stuff is really kicking my arse xD

Hi there friend!

Simple!

You would simply have a list or node of whatever node you’re looking to be closest to!

Then get your players current location and then use a for statement to iterate over your list of what objects you are checking the distance of.

Which ever one has the smallest distance is obviously the closest one!

@BigBob said: Hi there friend!

Simple!

You would simply have a list or node of whatever node you’re looking to be closest to!

Then get your players current location and then use a for statement to iterate over your list of what objects you are checking the distance of.

Which ever one has the smallest distance is obviously the closest one!

That is alot… Simpler than I imagined, thank you.

Only one thing though, how do I create a list, getChildren with some sort of index? I can probably figure out this one by myself.

Thank you!

Or do it the correct way and implement A*