Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D Spline/Path

Started by Endemoniada Jan 9, 2007 at 8:31 PM 5 replies 900+ views
Original Post
Endemoniada
Endemoniada
Hi guys, I have a 2D height map of the form: float map(float x,float z); ...which simply returns the y-value at a specific point. What I want to do is collect map values along a path. I'd like to specify the path with nodes (x,z coordinates) and use cubic interpolation (in x and z) to move along it. It seems straightforward until I try to figure out which two nodes I'm in between so I can interpolate. It would be easy if x and z were increasing as I go along the path but I want circular/looping paths. Maybe I shouldn't use splines ? How can I do this ? Thanks.
Vorpy
Vorpy
A path is just a line. If a function has two independent parameters, it describes a plane. To describe a path, you want one independent parameter (let's call it time). The (x,z) position on the path is a function of time. So you take the current time (or whatever variable is measuring progress along the path) and find the control nodes on the timeline around that time. The control nodes have the x and z values which you interpolate between using time. Then you get the corresponding height to use as the y coordinate.
Endemoniada
Endemoniada
Quote:
Original post by Vorpy
So you take the current time (or whatever variable is measuring progress along the path) and find the control nodes on the timeline around that time.


That's the problem I'm having.

I want to specify three or more points(nodes) and go from point to point in a looping manner. A three point example would be to start at P1, move to P2, move from P2 to P3, then from P3 back to P1, all with a constant step size.

How do I know when I get to the next node ? I need to know that in order to do the interpolation.

Thanks.
Zipster
Zipster
If you're using a piece-wise cubic curve, then each curve is parameterized over t=[0,1], so when t=0 you're at the start point and when t=1 you're at the end point. So you pick a curve (either the one between P1-P2, P2-P3, or P3-P1) and pick a value for t, and then determine the (x,z) coordinate. The nodes you're between is chosen by you, depending on which curve you're interpolating. If you're using a B-Spline, then you're using a knot vector where you explicitly set the value of t for each point on the path.

Unless your question is, if you have some random point you want to know the closest curve and the corresponding value of t... that's also possible but a lot more complicated.
Endemoniada
Endemoniada
I am finally understanding this.

I am actually using the path to select samples from the height map. The only thing I can think of that seems unsatisfying is when the nodes aren't equally spaced, then the sample gaps will be different.

So let's say my time step (parametric) is 0.01, I'll get 100 samples from node to node, but if one span is twice the length as another span the samples will have gaps twice as large.

Even if I measured the distances from node to node and adjusted the time step I doubt I'd be able to account for the curvature of the lines due to the spline.

Is there any way to get equal sample spacings ?

Thanks guys.
Vorpy
Vorpy
Let's say the cycle was from t1 to t2 and we're at time t. If t < t2, we haven't reached the part where the cycle repeats so finding the correct node is simple (just do a linear search for it, or if you need it to be faster use a tree).

If t > t2, it's like having gone around a circle.
You've gone around x = floor((t - t1)/(t2 - t1)) times.
Time t is the same position as time t - x*(t2-t1).

It's just like how adding 360 degrees to an angle results in another representation of the same angle (with one more twist).

Spacing the samples evenly is an entirely different issue. I think the solution will involve numerical methods. Even if you knew the length of the curve to calculate a step size to get the right number of steps for that curve, it would only give you a correct average step size for that curve. A constant step will move a different distance at different parts of the curves.
Zipster
Zipster
Moving along a curve at a constant speed is a common problem, and requires the use of what is known as arc-length parameterization. Dave Eberly has a nice reference here which goes over how to numerically reparameterize the curve as a function of arc-length. In essence, you input the arc-length (distance), and it outputs the t you should use to get that arc-length. Note that for constant speed, the arc-length will be linear with time, but t will most likely not be. It doesn't matter how t changes as a function of time though, because you will be moving along the curve at a constant speed.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.