AI plugin now with NavMesh pathfinding

Hey,

as some didn’t notice yet, there is an AI plugin in the update center, it adds an AI library (jme3-libraries-ai) to the library list of your SDK that you can use in games. Its in very early stages, some things might change and for now its really just a loose collection of tools from some core members. There also no documentation apart from a few lines of javadoc as of yet.

Nonetheless, I now finally added some cleaned up (in comparison to the MonkeyZone version) NavMesh code that can be used in games to make use of the NavMeshes you can create with the SDK (what, you didn’t know that? Right-click a node or geometry and select “NavMesh” ;)).

The basic usage is (untested top-of-head code):

//initialization
Mesh mesh = myLevel.getGeometry("NavMesh").getMesh();
NavMesh navMesh = new NavMesh(mesh);
NavMeshPathfinder navi = new NavMeshPathfinder(navMesh);
navi.setPosition(myPlayer.getWorldTranslation());

//compute new path (probably should do that on another thread, note the class is not "thread safe" per se)
navi.computePath(targetLocation);

//usage in update loop (e.g. in a Control)
public void update(tpf){
    //getNextWayPoint will return always the same waypoint until we manually advance to the next
    WayPoint wayPoint = navi.getNextWayPoint();
    if(wayPoint == null) return;
    Vector3f vector = wayPoint.getLocation().subtract(myPlayer.getWorldTranslation());
    if(!vector.length()<1)){
        //move the spatial to location while its not there
        myPlayer.move(vector.normalize().multLocal(tpf));
        return;
    } else{
        //if we are at the waypoint already, go to the next one
        navi.goToNextWaypoint();
    }
}

Additionally theres some code from @Sploreg that takes a different, more immediate approach on avoiding obstacles. Maybe he can say some things about that himself as I didn’t use that code yet :slight_smile:

Cheers,
Normen

10 Likes

I’m in the process of finishing more steering behaviors with some refactoring and adding some functionalists (probably in a couple of months) so it will get even better. I almost solved the problem with the wrong heuristic, but, as I said, almost. I hope I can fix it by then.

1 Like

Any advice on how to generate a NavMesh?

I tried it on various nodes, but it seems to do nothing. No error, no message after the parameter window, and of course, no mesh generated.
I have read some posts about it, but it seems to be quite easy (just right-click on any node…). Except it does not work for me.

For the A* heuristic, you can have some trivial ones that work: 0 or Euclidean distance. Manhattan distance works also if you don’t allow diagonal moves (using a rectangular grid). All of them are below the real cost, so they are admissible.

Still no java code in the AI plugin. Not that easy to look into bytecode!

You have to create the navmesh generator on geometry, e.g. a terrain. Else it cannot find anything…

This is great news Normen!
I will supply some of my fixes to it when I am back from holidays.

In regards to the nav mesh generator timing out, that is fairly common if the cell size is tiny. It will spin and spin and spin. It doesn’t do much logging either so it is hard to tell what is going on. Maybe we could hook into the newer native critterAI libraries in the future, since they are up to date and used by many projects.

Also, you can select a node and run the nav mesh generator on that (terrainQuad is a node after all). It doesn’t have to be a geometry. If you don’t see the mesh in the scene but see it in the Scene Explorer window, then just click on it and it will render in the Scene Composer.

3 Likes

Yeah but the node needs geometry somewhere in the tree so the pathfinder can find ground to walk on :wink:

@shirkit said:I'm in the process of finishing more steering behaviors with some refactoring and adding some functionalists (probably in a couple of months) so it will get even better. I almost solved the problem with the wrong heuristic, but, as I said, almost. I hope I can fix it by then.

@shirkit: In fact i’m using your code from the past and improve it to intergrate more things to create a complete AI library in style of an AI Engine as describe in the famous book “ARTIFICIAL INTELLIGENCE FOR GAMES” - IAN MILLINGTON.

I’ve done:
Steering
NavMesh, base in your codes and MonkeyZone.

A* + various technique like (jump point, hex). - Base on various codes
Generic FSM - Tungsteng FSM
Scripting - Groovy
RTS tatic (my own code for my game)

Are there any hints what you are creating so far, because I think the two codes are very similar.

I’m just creating more behaviors for the Steering library, making it more generic and some other stuff;
Some adjustments on the Navmesh library to fix some bugs (A* heuristic and one rounding error).

Is there any documentation on how to use things in the ai plugin? Specifically the scripting.

@8Keep123 said: Is there any documentation on how to use things in the ai plugin? Specifically the scripting.
UTSL
@8Keep123 said: Is there any documentation on how to use things in the ai plugin? Specifically the scripting.

First, you should hear to @normen suggestion and use the source. Now, after reading it, do you still have questions? If you do, make a proper question by reading http://www.mikeash.com/getting_answers.html

And I don’t recall any scripting in this plugin.

Well. It would be very useful if there were any source attached. Since the sources are not on the JME svn either… We are let just with guessing from the functions profile. There is not even javadoc available !

@yang71 said: Well. It would be very useful if there were any source attached. Since the sources are not on the JME svn either.... We are let just with guessing from the functions profile. There is not even javadoc available !
No there isn't. I could have waited with uploading it until I'd have found the time to add all that, you'd have liked that better? ;) The source is available here, like for all contrib projects: https://code.google.com/p/jmonkeyplatform-contributions/source/browse/#svn%2Ftrunk%2Fjme3-artificial-intelligence%2FAI

Thanks for the link @Normen. Sorry I missed this one. I feel so stupid…

We’re always on the forefront here, with commercial packages you’d see everything a year later, when its done :wink:

So I added the sources and existing javadoc to the build, I guess the docs are not yet trustworthy though, I didn’t check or clean them yet in the navmesh parts at least.

1 Like

Unfortunately I’m having issues with the plugin, Instead of a path being created there are 3 waypoints generated:
1st one is x: 0, y: 0, z: 0
2nd one is x: 0, y: 0, z: 0 aswell
3rd waypoint is the destination waypoint with xyz coordinates of the navi.computePath vector, ignoring the navmesh completely

Regards

Make sure you are using right parameters. You are probably passing the wrong parameters.

It does indeed generates a double starting position, but that can be easily fixed by you, Just check distance between first 2 waypoints after generating it and check if it’s really small.

Hi,

I trying, use NavMesh from AI-plugin creating NavMesh via editor and just initializing via code, but i not found way to used like @normen did. I not found this method getGeometry, Did i missing something?

[java]
//initialization
Mesh mesh = myLevel.getGeometry(“NavMesh”).getMesh();
NavMesh navMesh = new NavMesh(mesh);
NavMeshPathfinder navi = new NavMeshPathfinder(navMesh);
navi.setPosition(myPlayer.getWorldTranslation());

//compute new path (probably should do that on another thread, note the class is not “thread safe” per se)
navi.computePath(targetLocation);
[/java]

thanks.

@drr00t said: Hi,

I trying, use NavMesh from AI-plugin creating NavMesh via editor and just initializing via code, but i not found way to used like @normen did. I not found this method getGeometry, Did i missing something?

[java]
//initialization
Mesh mesh = myLevel.getGeometry(“NavMesh”).getMesh();
NavMesh navMesh = new NavMesh(mesh);
NavMeshPathfinder navi = new NavMeshPathfinder(navMesh);
navi.setPosition(myPlayer.getWorldTranslation());

//compute new path (probably should do that on another thread, note the class is not “thread safe” per se)
navi.computePath(targetLocation);
[/java]

thanks.

Hi,

I don’t know if is right, but i did this way.

[java]
Spatial navSpatial = worldRootNode.getChild(“NavMesh”);
List geometrys = new LinkedList();
GeometryBatchFactory.gatherGeoms(navSpatial, geometrys); //will be only one geometry in this list
navMesh = new NavMesh(geometrys.get(0).getMesh()); // so i can get.
navPathFinder = new NavMeshPathfinder(navMesh);
[/java]