How to determine where on a path my object will be at a given point in time?

I have map and an obj that is meant to move from start to end in X amount of time.

The movements are all straight lines, as curves are beyond my ability at the moment. So I am trying to get the object to move from these points, but along the way there are way points which keep it on a given path.

The speed of the object is determined by how long it will take to get from start to end (based on X).

This is what i have so far:

//get_now() returns seconds since epoch
var timepassed = get_now() - myObj[id].start; //seconds since epoch for departure
var timeleft = myObj[id].end - get_now(); //seconds since epoch for arrival
var journey_time = 60; //this means 60 minutes total journey time
var array = [[650,250]]; //way points along the straight paths

if(step == 0 || step =< array.length){
var destinationx = array[step][0];
var destinationy = array[step][1];
}else if( step == array.length){
var destinationx = 250;
var destinationy = 100;
} else {
var destinationx = myObj[id].startx;
var destinationy = myObj[id].starty;
}
step++;


When the user logs in at any given time, the object needs to be drawn in the correct place of the path, almost as if its been travelling along the path whilst the user has not been at the PC with the available information i have above.

How do I do this?

Note: The camera angle in the game is a birds eye view so its a straight forward X:Y rather than isometric angles.

• Is speed constant? If so, you can just use linear interpolation. – Vaughan Hilts Aug 1 '13 at 23:04
• Yes its constant but i've not heard of linear interpolation. – Dave Aug 2 '13 at 0:31

I am not completely certain that this is the best answer to your problem but I know this is a way to at least solve your problem.

In times like these it's important to remember the formula for uniform motion from which you know:

Distance = Rate * Time

Rate = Distance/Time

Time = Distance/Rate

Why do we need to know this? Well obviously because we are trying to figure out how far we are along a path we are at any given time. So at any given time, we obviously have the time component, but we don't have the rate to help us determine the distance! Or do we?

What we know is that starting at point A we would like to reach point B in X amount of time. Well hey there! We have a line segment and using the distance formula we can figure out what the total length of the line segment is.

So using the data from knowing our start and end state we now have a distance and a time from which we can determine our rate. Awesome! Now we just have to figure out how to find a point on a line given a certain distance which we can do with vector math and as it turns out, this answer covers the topic nicely and I'll paraphrase the answer here.

In short:

1. Calculate the vector from (X1, Y1) to (X2, Y2)
2. Calculate the magnitude / length of the vector
3. Normalize the vector
4. Calculate the new vector

The code / math for this last part would look something like this:

vectorX = X2 - X1; //X coordinate of the calculated vector
vectorY = Y2 - Y1; //Y coordinate of the calculated vector
magnitude = sqrt(vectorX*vectorX + vectorY*vectorY);
normalizedX = vectorX / magnitude;
normalizedY = vectorY / magnitude;
destinationX = x1 + normalizedX * distance;
destinationY = y1 + normalizedY * distance;