Hi guys, I’m making a game with some enemies (zombie). I’ve tried, unsuccessfully, write some code that allows the zombies to follow the player.
I use this code:
float vel = -2.2f;
for (int i = 0; i < zombie.length; i++) {
//Here I rotate the zombies to look the player
zombie[i].lookAt(cam.getLocation(), Vector3f.UNIT_Y);
//Here I get the direction where the zombies should go
Vector3f dir = zombie[i].getLocalTranslation().subtract(player.getPhysicsLocation()).normalize();
//Here I move towards player
zombie[i].getLocalTranslation().addLocal(dir.x * tpf * vel, 0, dir.z * tpf * vel * i);
}
The problem is that the zombies get a wrong position due to the lookAt. My question is how can I resolve it?
In the first picture is how they should stand, in the second how they’re if I write that code.
Do not directly modify the results of getters in JME. It may work sometimes but most of the time will cause you problems. In this case, there are update flags that never get set and so your zombies may appear in the wrong place, bounding boxes not recaclulated, etc.
If you want to move a spatial then you must call move() or setLocalTranslation(). (move() does what you are trying to do manually anyway.)
also, this may be a small thing, but here the player’s physics location and the zombie’s local translation are combined to get a direction vector…
take caution when creating a vector direction from two vector positions which do not strictly share the same basis (or same origin and orientation).
(for example, the physics space containing the player could be nested withing another physics space relative to the zombie, which could be contained in a chain of parent nodes up the scenegraph)
I tried the solution that @remy_vd wrote. It has the same problem, it’s like the zombies’ feet follow me and they aren’t stand erect.
@pspeed The first time that I’ve written the code, I changed it to move(), I tried another solution and then I re-wrote the first code and forgot to change it.
at first glance, that model’s origin marker appears to be oriented differently from the base coordinate system in jME. This page may help: 3-D Rotation