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){
SceneGraphVisitorAdapter visitor = new SceneGraphVisitorAdapter() {
public Node visit(Node node, Spatial troop) {
// search criterion can be control class:
NavCalculator nav = new NavCalculator;
if (node != null && nav.getDistance(troop, node) <= 2000){
}
}
};
// … or scan it breadth first.
nodetype.breadthFirstTraversal(visitor);
}[/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*
https://www.google.com/search?q=A+Star#q=a+star+algorithm
You already have a graph, so any graph based search algo should be easy to implement.
I think the way I’d do this is have a sphere ghost physics object and then the next frame you’ll be able to get all the objects within that radius. Or you could have that sphere there the whole time and every frame you would have all of those objects in the radius, but it would waste more resources if you don’t need that information every frame.
Edit, I just realized you need the closest one, not just within a radius. I think the easiest way to do that is to just check each object you have for the distance and return the smallest number. But if you have a lot of objects that won’t work too well, especially every frame.