Predictive firing (in a tile-based game)
- by n00bster
I have a (turn-based) tile-based game, in which you can shoot at entities.
You can move around with mouse and keyboard, it's all tile-based, except that bullets move "freely".
I've got it all working just fine except that when I move, and the creatures shoot towards the player, they shoot towards the previous tiles.. resulting in ugly looking "miss hits" or lag.
I think I need to implement some kind of predictive firing based on the bullet speed and the distance, but I don't quite know how to implement such a thing...
Here's a simplified snip of my firing code.
class Weapon {
public void fire(int x, int y) {
...
...
...
Creature owner = getOwner();
Tile targetTile = Zone.getTileAt(x, y);
float dist = Vector.distance(owner.getCenterPosition(), targetTile.getCenterPosition());
Bullet b = new Bullet();
b.setPosition(owner.getCenterPosition());
// Take dist into account in the duration to get constant speed regardless of distance
float duration = dist / 600f;
// Moves the bullet to the centre of the target tile in the given amount of time (in seconds)
b.moveTo(targetTile.getCenterPosition(), duration);
// This is what I'm after
// Vector v = predict the position
// b.moveTo(v, duration);
Zone.add(bullet); // Now the bullet gets "ticked" and moveTo will be implemented
}
}
Movement of creatures is as simple as setting the position variable.
If you need more information, just ask.