A basic solution to this problem, given the two positions of the objects (A
and B
):
- Compute the normalized direction vector from
A
to B
: v = normalize(B - A)
.
- Every game logic update, add that vector
v
(scaled by the desired object speed) to the position of A
: A += v * speed
.
- Stop doing this when
A
is within some desired tolerance of B
.
A simple implementation of this might involve storing "movement destination" point with each object, representing where it is trying to get each frame. Apply the above logic every frame in the object's update method (pseudocode):
if (target is assigned && !position.isWithinToleranceOf(target)) {
v = normalize(target - position)
position += speed * elapsedTime * v;
}
A more involved solution would involve abstracting out the movement destination and handling into a more generic "orders" object which can be associated with an in-game entity, allowing you to build "movement order" objects and "attack order objects," all of which could be assigned to various in-game objects.