I''m sure this one has been discussed before, but after reading everything I can get my hands on, I''ve still not reached a decent solution

. I''m not geared up with calculus yet, so papers with pages of figures aren''t helping. I''ve written a *near* working solution, it just has annoying bugs.
I''m trying to move an object (train) along a catmull-rom spline (track) at a constant speed. I decided to go for the mathsless approach of creating a lookup table. Currently, it goes a bit like this:
I chop up the curve into chunks of equal length. I do this by first getting an approximate length of the curve, then I divide it into the size of my table.
Next, I begin an evaluation of the curve in detail (in steps of like 0.0001 for t) and add up the distance travelled at each step. Once I get to a set distance (the chunk size I defined earlier) I shove the current t value into the table, then begin on the next chunk, until I reach the end of the curve.
When it comes to moving stuff along the curve, I find out where my object is on the curve in relation to a table entry. (Eg: halfway along the curve means I go halfway into the table). I pull out the t value stored there, plus the next one along, and convert ''em to coords. I use the remainder from the distance-table division to interpolate between these two coordinates and give me a final position.
Currently, this lets me draw beautifully curved accurate looking track, and my train moves with constant speed along it, to an extent. There''s two major problems though:
Firstly, for very long curves, steps of 0.0001 are just not small enough when generating the table. I find that I end up stepping right past the distance I''m looking for, and therefore each chunk isn''t the same size. The result? The train stutters and jumps along the track at varying speeds as it switches between chunks. I can decrease the step size, but that leads me onto the next problem..
Generating the lookup table is just too damn slow. Since I have to get the distance travelled between each evaluation, I''m doing thousands of square roots. For 20 curves, it takes forever. If I use smaller steps to eliminate the stutter, I may as well go make a cup of coffee while my program initializes..
Is there a better way of tackling this problem or have I missed something? Should I start reading my industrial strength calculus books?

David