I'm making a game with LibGDX where the player is on a grid (like a Chess board). For now, I'm moving the player by teleporting him 16px in a direction. I'd like to use the delta time to make smooth movements, I want the character to move to 16px in a direction smoothly and when he's arrived, he stops moving.

Here's the code of my Player's Class:

public Player(Sprite sprite){

public void draw(Batch batch){

public void update(float deltaTime) {

public void move(int direction){

    case UP:
        setPosition(getX(), getY()+16);
    case DOWN:
        setPosition(getX(), getY()-16);
    case LEFT:
        setPosition(getX()-16, getY());
    case RIGHT:
        setPosition(getX()+16, getY());

public boolean canMove(int direction){
    if(direction == UP){
        if(getY() == 160-getHeight())
            return false;
    else if(direction == DOWN){
        if(getY() == 0)
            return false;
    else if(direction == RIGHT){
        if(getX() == 160-getWidth())
            return false;
    else if(direction == LEFT){
        if(getX() == 0)
            return false;
    return true;

1 Answer 1


The lerp method from Vector2 seems to be what you are looking for.

From the documentation:

Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.

So basically, you need to calculate where the player should be at the end of the movement:

targetPosition = new Vector2(/* calculate the target position here regarding the targetted cell */)

And then you lerp (interpolates linearly) the player current position towards the previously calculated target position.

//in update method
alpha = calculateAlpha();
playerPosition.lerp(targetPosition, alpha)

Last step is to calculate the alpha. As the documentation state:

[...] alpha which is in the range [0,1].
  • A alpha of 0 means that the movement has not started yet
  • A alpha of 0.5 means that the player is half way from reaching the targetPosition
  • A alpha of 1 means that we are done with our movement and the player has reached the targetPosition (playerPosition = targetPosition).

One way to calculate it here would be to use time.

float calculateAlpha() {
    _currentTime += Gdx.graphics.getDeltaTime();
    return MOVETOCELL_TIME / _currentTime;

In _currentTime we store the time in seconds since the player has started moving, we increment it each frame by the time in seconds since the last frame (the delta time). Then we calculate and return our alpha by dividing the total time to move by one cell (in seconds) by _currentTime.

When the movement is done, you should not forget to reset _currentTime to 0.

  • \$\begingroup\$ Thank you for this interesting answer, I'll look into it when I will have time for. \$\endgroup\$
    – Lisible
    May 26, 2014 at 15:58
  • \$\begingroup\$ Thank for you this answer. It helped me a lot but I think it should be _currentTime / MOVETOCELL_TIME to make alpha be within 0 and 1 \$\endgroup\$ May 14, 2018 at 12:14

You must log in to answer this question.

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