3
votes

So, I'm trying to make a game in LWJGL and it seems to work fine for me. Although, I ran into some issues in moving my entities around on the screen. I want to make it go from one point to another, at the same speed. Also, I'm animating my sprite according to the direction the entity is moving.

But! I have some issues:

1# It flickers because the movement is defined a modifier: delta (to make smooth movement defined by the FPS). Actually, it never really reaches it's point (because it recalculates and never hits the position). How can I solve this?

2# When 2 players join the same server, my character on the fastest computer runs faster. I think it's because of the FPS, how can that be solved?

private String name;
private float positionx,positiony; // Current
private int targetx,targety; // Target
private int dx, dy; // Direction
private int pointx, pointy; // Direction
private float speed;
private Sprite sprite;

public Entity(String name, int positionx, int positiony, Sprite sprite){
    this.name = name;
    this.speed = 0.1f;
    this.positionx = 720;
    this.positiony = 450;
    this.targetx = 1000; // fix this
    this.targety = 10; // this for testing.
    this.sprite = sprite;
    this.dx = 0;
    this.dy = 0;
}
//double distance = Math.sqrt((vx * vx) + (vy * vy));
public void move(long delta){
    if(positionx < targetx){
        dx = 1;
        pointx = 1;
    }else if(positionx > targetx){
        dx = -1;
        pointx = -1;
    }else{
        dx = 0;
    }

    if(positiony < targety){
        dy = 1;
        pointy = 1;
    }else if(positiony > targety){
        dy = -1;
        pointy = -1;
    }else{
        dy = 0;
    }


    //Set animations:
    if(positionx==targetx && positiony==targety){
        if(pointx<0){
            sprite.setAnimation(5, 2, 100); // Standing left
        }else if(pointx>0){
            sprite.setAnimation(6, 2, 100); // Standing right
        }else if(pointy<0){
            sprite.setAnimation(7, 2, 100); // Standing up
        }else if(pointy>0){
            sprite.setAnimation(4, 2, 100); // Standing down
        }
    }else{
        if(pointx<0){
            sprite.setAnimation(1, 2, 100); // Walking left
        }else if(pointx>0){
            sprite.setAnimation(2, 2, 100); // Walking right
        }else if(pointy<0){
            sprite.setAnimation(3, 2, 100); // Walking up
        }else if(pointy>0){
            sprite.setAnimation(0, 2, 100); // Walking down
        }
    }
    //movement here.
    positionx += dx*delta*speed;
    positiony += dy*delta*speed;

    System.out.println(dx*delta*speed);

    sprite.setPosition((int)positionx, (int)positiony);
}
1

1 Answers

2
votes

1# It flickers because the movement is defined a modifier: delta (to make smooth movement defined by the FPS). Actually, it never really reaches it's point (because it recalculates and never hits the position). How can I solve this?

If you store point A and point B between which it moves, you can set a time interval. Each time interval a set distance will be travelled and if at one iteration the object goes too far you can set its coordinates for point B. This can be easily done with a Timer. That way, after a certain amount of time, it will be on your specified position.

2# When 2 players join the same server, my character on the fastest computer runs faster. I think it's because of the FPS, how can that be solved?

Same answer as question #1, if you use a Timer. Each player will move at the same speed (because the elapsed time is the same for each gamer).

Bottom line:
fps is variable, while elapsed time is the same for everyone.