Enemies follow the player

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.

your velocity seems strange, -2.2f.

concerning your problem it should be something like this, top of my head:

Vector3f playerLocation = cam.getLocation();
Vector3f directionToPlayer = playerLocation.subtract(zombieLocation).normalizeLocal();

Quaternion zombieRotation = new Quaternion(),
zombieRotation.lookAt(directionToPlayer, Vector3f.UNIT_Y);

Vector3f movement = directionToPlayer.mult(velocity * tpf);
Vector3f zombieLocation = zombie.getLocalTranslation();

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)

Thanks for the answers!

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.

Maybe I understand why this happens. The pivot of the model is on its feet. So, is there a way to change the position?

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


Where did the model come from and how did you get it into JME?

…because I agree it looks like it’s upside down in your picture.


1 Like

I’ve rotate the model by 90 degrees.
The models come from a site called mixamo.