# Train - Uniform Motion on Curve Track

## Recommended Posts

Sevans    204
Hi, I am trying to implement a train track using a Cardinal cubic spline in 3D. I would like a train, for now just a block :), to travel at a uniform speed down the track and around curves. How can this be done? I have been told arc length parameterization but I have not yet been able to find an algorithm that actually tells me how to do this in 3D and have thus botched everyone I have tried to adapt. Can anyone explain how to do this to me? an example would be best! Thanks much, -sevans

##### Share on other sites
Wasting Time    411
Moving at Constant Speed

I believe the same website has an implementation for 2D and 3D curves.

##### Share on other sites
Sevans    204
I have looked over that paper. I have a post here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=423014
about how I was not able to get it to work. The train appears to speed up and slow down, thus is not traveling at a constant speed.
I was wondering if anyone knew of some other resources to do this? Or otherwise could respond to my simpsons rule post.

-sevans

##### Share on other sites
Wasting Time    411
I do not read the OpenGL forum here. In your post you mention using Simpson's rule for integration of a function f(x) on the interval [a,b]. If f(x) is a positive function, Simpson's rule gives you an approximation for the area under the graph of f(x) and between the lines x = a and x = b. The problem in your code is that you apply the rule only to the _entire_ interval. This is not accurate enough for the purpose of computing arc length.

In general, numerical integration involves selecting a "partition" of [a,b], which is a set of n+1 points x[i] = a + i*h, where h = (b-a)/n and 0 <= i <= n. You apply Simpson's rule to each _subinterval_ [x[i],x[i+1]]. The practical problem is how to choose n. You want it to be large enough to give you enough accuracy in the approximation but you want it small enough to keep the computation time to a minimum. But in your application, you certainly want n to be larger than 1!

##### Share on other sites
I don't know anything about splines and curves.
So the approach I'd probably take is...

1 move the box forward based on prior velocity
2 measure the vector length from past to new location (speed approx)
3 project the position onto the path(some kinda nearest point function?)
4 take the vector from prior to new location and rescale it to match the above length/speed
5 repeat steps 3-4 a few more times, till the box is 'close enough' in terms of both distance from path and change from past position(speed)

uhh... that should end up making the train more or less follow a near constant speed... based on segmented approximation of the path to decide speed...
If your update time increment is small, who's going to notice?

##### Share on other sites
Bob Janova    769
Generally, you need to recursively split the spline into small segments and get the (approximate) length of each segment. You can do this once and store the information in the spline's data structure if memory is cheap and CPU time isn't.

Alternatively, for relatively short frame times and if this type of spline is parametric: calculate the size of ds/dt (t being the parametric parameter) at the current point of the train and assume that remains constant over the current frame, then move the train in t-space. This only works for low curvature tracks, but it's a train track so that might be a fair assumption.

Neurovore    127

##### Share on other sites
erissian    727
I would just like to note, even though this may be a matter of aesthetics, that an otherwise unrestricted object traveling along a straight path will speed up if its path is constrained to a curve.

Therefore, if you're seeking realism, you may not have a problem. If it's just a matter of taste, then you can use the first derivative of the spline to calculate how far you should be moving during your integration time.