Hi all,
I´m currently playing around with some basic AI. At the moment I´m just trying to make a box follow the player/cam, as well as navigate by using a NavMesh.
My problem is the movement of the box is not consistent. It´s trying to follow the player around, but most of the time it is just “shaking” in the same location.
If I move the cam the box sometimes continues to follow the cam for a short period of time. I´m probably doing the translations wrong somewhere, but I´m not sure.
The relevant code looks like this:
[java]
private CharacterControl follower;
…
Box box = new Box(1,1,1);
geometry = new Geometry (“My target”, box);
Material unshadedMat = new Material (assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
geometry.setMaterial(unshadedMat);
BoxCollisionShape boxShape = (BoxCollisionShape) CollisionShapeFactory.createBoxShape(geometry);
follower = new CharacterControl(boxShape, 0.05f);
follower.setJumpSpeed(20);
follower.setFallSpeed(50);
follower.setGravity(50);
follower.setPhysicsLocation(new Vector3f(0, 20, 0));
geometry.addControl(follower);
bulletAppState.getPhysicsSpace().add(follower);
//AI Navigation
Mesh mesh = ((Geometry) gameLevel.getChild(“NavMesh”)).getMesh();
NavMesh navMesh = new NavMesh(mesh);
navi = new NavMeshPathfinder(navMesh);
navi.setPosition(geometry.getLocalTranslation());
//compute new path (probably should do that on another thread, note the class is not “thread safe” per se)
navi.computePath(player.getPhysicsLocation());
[/java]
Then in the simpleUpdate method:
[java]
//AI stuff
//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.getPosition().subtract(follower.getPhysicsLocation());
if (!(vector.length() < 1)) {
//move the spatial to location while its not there
follower.setWalkDirection(vector.normalize().multLocal(tpf * 20));
geometry.setLocalTranslation(follower.getPhysicsLocation());
//geometry.move(vector.normalize().multLocal(tpf * 20));
return;
}
else {
//if we are at the waypoint already, go to the next one
navi.goToNextWaypoint();
}
[/java]
Any clues on what´s wrong here?