clamp
clamp

Reputation: 34036

Move along a path of points with even speed

I have a sequence of points and i have an object that should move along the path described by these points.

I know that i need to find the corresponding segment depending on the time, but i dont know how to do this precisely.

Note that the speed should be constant along the path, so you have to take the distance of a segment into account.

Upvotes: 0

Views: 466

Answers (2)

maxim1000
maxim1000

Reputation: 6375

Beofre start assign time moments to each point: the first point gets time=0, every next point gets time[i+1]=time[i]+distance(i,i+1)/velocity.

Then for each time moment you can calculate position in the following way:

  1. Find first point, for which time[i]<=current_time
  2. Find exact position: point[i]+(current_time-time[i])*(point[i+1]-point[i])

Upvotes: 1

Richante
Richante

Reputation: 4398

Something like this? To find the segment at a particular time, loop through all the segments, add the length of the segment until you reach the target time.

public int findSegment(int [] segments, int time) {
   time_so_far = 0;
   int i = 0;
   while (time_so_far < time) {
     time_so_far += segments[i];
     i++;
   }
   return i-1;
}

If the segment lengths are distances, and you are considering a speed that isn't 1, then you'd need to do time_so_far += segments[i] / speed

Upvotes: 1

Related Questions