I have this tilemap and a player in my game, and I got the player to move by adding the velocity to the position. However, it is very hard to control the player exact this way of the tilemap. So, I began trying to do something like the old final fantasy games did (and a heap of other games too): moving tile by tile, but making it look smooth.
However, I failed. So, if anyone have any idea of what the best way to make the player move tile by tile is, then shout out. If you have played final fantasy V for example, then that's the kind of movement I want to try to implement in my game.
To explain it a bit further; each tile in my game is 16x16, so is the player. I don't want the player to "jump" between the tiles, but I want it so that say, if you press "A", then move the player to the tile to the left in a smooth way.
I tried something like this:
private Vector posA;
private Vector posB;
private double time = 0.6 / 60;
private void move(Direction dir) {
if (!moving) {
//Initialize Movement
posA = new Vector(position);
posB = new Vector(position);
switch (dir) {
case UP:
posB.subtract(0, 16);
break;
case DOWN:
posB.add(0, 16);
break;
case LEFT:
posB.subtract(16, 0);
break;
case RIGHT:
posB.add(16, 0);
break;
}
moving = true;
}
//Update Position
if (moving) {
Vector v = new Vector();
v.set((posA.x + (posB.x - posA.x) * time), (posA.y + (posB.y - posA.y) * time));
//Set Position
position.add(v);
if (position.x >= posB.x && position.y >= posB.y) {
position.set(posB);
moving = false;
System.out.println("DONE MOVING!!!");
posB = null;
posA = null;
}
}
}