Archived

This topic is now archived and is closed to further replies.

nvdmerwe

Calculating the length of a curve

Recommended Posts

nvdmerwe    122
I have a set of control points and are using a Hermite Interpolation function to generate points between these as to draw a curve. Now I want to move along this curve at varying speeds say 8 m/s or 50 m/s. To do this I''m thinking I need to get the length between control points so I can move along the curve by a certain distance in a fixed time. Anyone got another approach? Or a way to get this length between points remembering that the control points aren''t evenly spaced and all the Hermite Interpolation function does is return an interpolated point between two control points based on a time value between 0 and 1?

Share this post


Link to post
Share on other sites
johnb    351
You can explicitly work out a length function, e.g. as a function of your interpolation parameter, using integration, but this is complex and still gives you something difficult to use. If you are doing your updates often enough you can make this easier by breaking down the motion along the curve into enough short linear steps.

E.g. At time 0 you are at x and want to get to x'' at time t. If the time t is short enough and the curve is smooth then the path from x to x'' will be close to a straight line, and the distance along the curve can be approximated by the straight line from x to x''.

First guess the parameter needed (e.g. by assuming it increases the same as last time) and work out the coordinates of x''. Work out the distance d from x to x''. Compare this to the desired distance = speed * t, and use the ratio of it to d to rescale the parameter increment, e.g.

increment = guessed increment * ((speed * t) / d)

For a smooth curve and small enough steps this should produce reasonable results. To improve it''s accuracy use smaller time steps and/or recheck the distance and repeat if necessary, e.g. if the distance is still out redo the cacluations using the newly caculated increment as a starting point.

Share this post


Link to post
Share on other sites
nvdmerwe    122
Excellent.

In reality the curve I''m drawing is just a set of smaller linear ones.
I know the start and end point of each such line segment, so now I can just calculate these distances and add them together.

Thanks a million.
What would I do without you guys?

Share this post


Link to post
Share on other sites