I would like to implement tile-based grid movement and I've heard that linear interpolation comes handy in the implementation process. I've tried to use it but I'm not sure how to make it stop once the target position is reached. I can't find a way to check that on the interpolation. Here's the code I'm using:
function movePlayer(deltaTime) {
if buttonUp then
velocity.add(0, speed);
if buttonRight then
velocity.add(speed, 0);
if buttonSouth then
velocity.sub(0, speed);
if buttonLeft then
velocity.sub(speed, 0);
velocity.scl(deltaTime);
if (targetPosition.isZero()) {
targetPosition.add(getNextPosition());
}
position.lerp(targetPosition, speed * deltaTime);
float distance = position.dst(targetPosition);
if (allowMovement && distance < 0.1f) {
position = targetPosition;
allowMovement = false;
targetPosition.set(0, 0);
}
}