Let's say I want to have an object move along a set path between points A, B, and C repeatedly throughout a game, based on a variable of which point it should be at/moving towards. Now, I could program in full A* pathfinding, but that would take a lot of time and require a lot of code for something that really wouldn't be used to its full worth. Is there a more efficient way to do this? At the moment, the only idea I have is to have the object move a set number of pixels in one direction, then another, then another, but it would be very easy for the object to get several pixels off after a while. I should add that none of the points are withine line-of-sight of one another, so the object will have to move around walls. Any help is appreciated, as I feel like I'm missing something very obvious here.
I should clarify that the walls will not be changing position. They will remain where they are for the entire game.
I am assuming from your comment "it would be very easy for the object to get several pixels off after a while" that the key question you have is how to regularly update the position of an object so that it follows a path between two points. If you just update it by the same amount each time then, as you say, it's not likely to end up at the end point.
The easiest way to do this is to use a variable that represents how far along the path the object has travelled so far. The algorithm can very quickly recalculate the correct position.
For example:
class Position {
private final int x;
private final int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public static Position interpolatedPosition(Position from, Position to, float progress) {
return new Position(
from.x + Math.round(progress * (to.x - from.x)),
from.y + Math.round(progress * (to.y - from.y)));
}
}
You can fairly easily extend this to allow for constant speed by dividing progress
by distance in the call. Or you can support movement along a path by creating a class with a list of positions and stored progress.
class Path {
private final List<Position> positions = new ArrayList<>();
private int currentPosition = 0;
private float progress = 0.0f;
private float speed = 0.1f;
public void addPosition(Position position) {
positions.add(position);
}
public void update() {
progress += speed;
while (progress > 1.0f) {
currentPosition = nextPosition();
progress -= 1.0f;
}
}
public Position getCurrentPosition() {
return Position.interpolatedPosition(positions.get(currentPosition), positions.get(nextPosition()), progress);
}
private int nextPosition() {
return (currentPosition + 1) % positions.size();
}
}
The second part of your question is how to determine this path ahead of time around sets of obstacles. The answer to this depends on lots of factors - particularly how optimal you want the solution to be and how efficient the search algorithm needs to be. There aren't any easy shortcuts however - generic path finding is not a simple area. Depending on your application the simplest solution may be to just define the paths yourself if you already know the layout of the map.