NPC doesn't follow player

Hello sir, I want NPC(Ninja) to follow my player(sindbad). But when i executes my app NPC doesn’t follow player but instead it shakes his own place only. A very similar issue I found here http://hub.jmonkeyengine.org/forum/topic/trouble-making-ai-follow-player/ but no help. I believe there is a problem in my update method code mentioned below.

[java]Waypoint wayPoint = navi.getNextWaypoint();
if(wayPoint == null) {
return;
}
System.out.println("wayPoint = " + wayPoint);
Vector3f vector = wayPoint.getPosition().subtract(npc_phy.getPhysicsLocation());
path.addWayPoint(vector);
System.out.println("vector = " + vector);
if (!(vector.length() < 1)) {
npc_phy.setWalkDirection(vector.normalize().multLocal(tpf * 20));
npc.addControl(npc_phy);
}
else {
navi.goToNextWaypoint();
}[/java]

Please help me how to fix this. Many thanks

[java]Vector3f vector = wayPoint.getPosition().subtract(npc_phy.getPhysicsLocation());
path.addWayPoint(vector);[/java]

does it not expect a position, rather than a direction?

Explain what you think your code does (line by line).

Ya normen, Everytime it is getting waypoint in a path computed by “navi.computePath(model.model_phy.getPhysicsLocation(), debugInfo);”. After that my NPC trying to reach player using those waypoints. “path.addWayPoint(vector);” doesn’t have any meaning here forgot to remove, I added for different purpose. Below are more explanation.

  1. First it is storing initial wayapoint. since my NPC location is same as first waypoint so vector will get 0 and it will go to else part to get the next way point.

  2. second time it will take second way point and after subtracting from NPC position it will give new vector position where my NPC should be. so I am moving my NPC to new position using “npc_phy.setWalkDirection(vector.normalize().multLocal(tpf * 20));”.

  3. Now Repeat step 2 untill it reach my player(sindbad)

This is my understanding, I know some problem in this logic only that’s why I asked. Thanks

i think youre a little confused by the api… if you havent already you should see normen’s initial post about the pathfinder: http://hub.jmonkeyengine.org/forum/topic/ai-plugin-now-with-navmesh-pathfinding/

youre trying to do way more work than you need to. all you need to do is navMeshPathFinder.setPosition(npcLocation) then do navMeshPathFinder.computePath(playerLocation) this internally creates a list of waypoints in the navmeshpathfinder that the npc would need to reach (in that order) to get to the player without going through walls or obstacles.

you would never do path.addWayPoint() thats a method used by navMeshPathFinder to populate the path for you. all you want to do is read the values in the path so you know which direction sinbad needs to go.

on a sidenote, your usage of CharacterControl is a little… wrong. you might want to just make this work without Bullet (just move shapes around using spatial.move() and spatial.setLocalTranslation() then wants you see how the nav mesh pathfinder works. then go the next step and work with physics.

Hi daniel you are correct. The thing u have mentioned i already defined in my code that is setPosition for NPC and computePath from NPC to my player. I didnt paste whole code here. I pasted only part of it. For “path.addWaypoint()” as I already told, I mentioned this line for different purpose forgot to remove in my post. I was testing the motion path code for that I mentioned path.addWaypoint().

Finally it got fix. Many thanks to all of you. when I am moving NPC without physcis its working fine. I don’t know why my physcis is not moving using “npc_phy.getPhysicsLocation()”. Finally I removed physics from my NPC and did “npc.getLocalTranslation()”, move my NPC and then i applied physcis to it. Anyone who has the same issue do like that.