# Java LWJGL - Monster follow script not working properly

I have written a script to be set off whenever a player is within a distance of the monster. The script checks if the x position is greater than or less than the players x, and same for the z. (y is automatically set to terrain) The checkWalkX and checkWalkZ functions work with my monsters walk function, which specifies a new position on a timer and walks to that position. But when I use the same kind of idea for the following, it doesnt work correctly.

public int checkWalkX(Vector3f position) {
if (Math.floor(this.getX()) != Math.floor(position.x)) {
if(this.getX() > position.x) return 1; //Greater
if(this.getX() < position.x) return 2; //Less
}
return 0;
}
public int checkWalkZ(Vector3f position) {
if (Math.floor(this.getZ()) != Math.floor(position.z)) {
if(this.getZ() > position.z) return 1; //Greater
if(this.getZ() < position.z) return 2; //Less
}
return 0;
}

public void follow(Player player) {
walking = false;
following = true;

if(checkWalkX(player.getPosition()) == 1) this.setX(this.getX() - mobSpeed);
else if(checkWalkX(player.getPosition()) == 2) this.setX(this.getX() + mobSpeed);

if(checkWalkZ(player.getPosition()) == 1) this.setZ(this.getZ() - mobSpeed);
else if(checkWalkZ(player.getPosition()) == 2) this.setZ(this.getZ() + mobSpeed);

if(Math.floor(checkWalkX(walkToPosition)) == 0 && Math.floor(checkWalkZ(walkToPosition)) == 0) following = false;
}


For some reason when I run this script, the monster will only move within a distance of 2ish. He moves the right ways kinda, but he doesnt follow me. Would anyone know why this is?

• Why are you using Math.floor()? Why in the last line of follow()? There it is not necessary since you only return 0, 1 or 2. – Sebastian Barth Jan 27 '15 at 12:52
• What is the unit of mobSpeed? Way per timestep? – Sebastian Barth Jan 27 '15 at 12:55
• If I didnt floor it would always be off by .000* whatever and the monster jerks side to side – Kyle Jensen Jan 27 '15 at 20:21
• mobspeed is the amount to move each render – Kyle Jensen Jan 27 '15 at 20:21
• So you Limit the position to be at whole numbers? 1 2 3.., no 1.3674? You then really should use an int based vector. – Sebastian Barth Jan 28 '15 at 8:56

I want you to think about a refactoring of your code. Since you already have a vector structure (Vector3f - I guess its from lwjgl) why not use it?

// Calculate the vector pointing from the mob toward the position with
// a length of their distance.
// This is equal to: wayTowardPlayer = playerPosition - mobPosition
Vector3f wayTowardPlayer = new Vector3f();
Vector3f.sub(player.getPosition(), this.position, wayTowardPlayer);

// Check if both are not at the same position, yet.
if (wayTowardPlayer.lengthSquared() > 0) {

// Here you also could:
// - Clamp your vector to let the mob not walk more than some pixels,
//    regardless of its distance to the player.
// - Scale it by e.g. 0.5 let the mob walk only half the way toward player.
// - Clamp it to [-1,0,+1] so it reflects the direction you want to move
//   and then multiply it with a velocity and passed time since last update
//   to let it move smoothly toward the player over time without jumping.

// Add the calculated vector to the mobs postion.
// If you didn't manipulate it as described before it's position then
// is that of the player.
// This is equal to: mobPosition = playerPosition + wayTowardPlayer

I also recommend you to quit with using lwjgl Vector3f since it only has static members for addition and substraction. What about using those classes from libgdx? Libgdx uses LWJGL for desktop rendering as well and has a lot of tools integrated that makes programming easier. It also allows to easy build portable apps for different plattforms, e.g. Android. In my games I always seperate game logic from graphics.
this.position.add(wayTowardPlayer);