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?

  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 27, 2015 at 12:52
  • \$\begingroup\$ What is the unit of mobSpeed? Way per timestep? \$\endgroup\$ Commented Jan 27, 2015 at 12:55
  • \$\begingroup\$ If I didnt floor it would always be off by .000* whatever and the monster jerks side to side \$\endgroup\$ Commented Jan 27, 2015 at 20:21
  • \$\begingroup\$ mobspeed is the amount to move each render \$\endgroup\$ Commented Jan 27, 2015 at 20:21
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 28, 2015 at 8:56

1 Answer 1


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
    add(this.position, wayTowardPlayer, this.position);

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.

With libgdxs vector classes the last addition would simply be:

  • \$\begingroup\$ I just have a question about this.. Where does it calculate the "speed" or how far to move each direction? What is the 'add(this.position, wayTowardPlayer, this.position);' ? \$\endgroup\$ Commented Jan 29, 2015 at 17:06
  • \$\begingroup\$ I updated my answer. \$\endgroup\$ Commented Jan 29, 2015 at 17:25
  • \$\begingroup\$ You may find this post informative, too. \$\endgroup\$ Commented Jan 29, 2015 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .