views:

168

answers:

3

Ok this question is going to be a little abstract.

I have an icon moving along a line which is represented by a series of coordinates stored in a vector, and I am iterating through them. The distance between coordinates is variable. So sometimes the icon will move slowly and smoothly, and others it will jump several 100 pixels at a time.

I am having trouble coming up with an algorithm to split up each set of coordinates it must travel between into a set of relative coordinates where the number is based on size, so that transition is smooth no matter how many co-ords are on a single line.

Any ideas would be much appreciated. Thanks :)

+1  A: 

If you want the object to move at a constant speed, I'd suggest a time-based model, where your object is actually moving at a speed (pixels/second). You can still get it to hit every point(ish) if you spline along a curve (such as a catmull-rom curve).

bkritzer
+1  A: 

Take a look at this discussion of the Main Game Loop.

And here's a quote from that page:

At this step, updates to all the objects in the game world are calculated and performed. Usually, a time step value is passed to all of the update methods indicating how much time has passed since the last update ...

You need to know 3 things:

  • how much time has elapsed since you last updated the position of your object?
  • what is the rate of movement of your object?
  • what is the direction (usually represented as a Ray) your object is moving?

From these, you can calculate the current position of the object.

Drew Wills
A: 

So you want to move from a initial point (x0/y0) to a end point (x1/y1) along a line by a variable number of steps but with a maximum distance for each step?

This could be done by something like this:

int stepdist = 10; // max pixels per step
double xdiff = x1 - x0;
double ydiff = y1 - y0;
double dist = sqrt( xdiff * xdiff + ydiff * ydiff );
int steps = (int) ( ( dist - 1 ) / stepdist );
if( steps > 0 )
{
   xdiff /= steps;
   ydiff /= steps;
   while( --steps >= 0 )
   {
       x0 += xdiff;
       y0 += ydiff;
       moveTo( (int) x0, (int) y0 );
   }
}
moveTo( (int) x1, (int) y1 );
x4u