# Spline Curves

This topic is 4990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

A *few* part question. Basic backround. I'm trying to make a rail road model, and so I need to create transition curves between two straight lines of track, or a straight line and a fixed radius curve, or (possibly) between two different radius curves. (Also, these same curves will be used to connect different slopes as well) I also need to calculate the length of said spline. All this will be done (at some point) in 3d, but for now 2d is probably fine. I've been looking at several math places online, but they lose me when they start whipping out greek letters and matrices and stuff. I really don't need a complete understanding of all types of splines and all coordinates systems. Would be nice for later, but not right now. I guess I need to pick a type of spline (too many out there!!). I really will allready have the start and end points. I will also be able to calculate the tangents (derivitive?) at the starting and ending points of the spline (Derivitive is the slope, right?? Not the best at math anymore). So which kind is this?? ( I assume its a quadratic, but what do I know ) I need the length. I've seen a few examples where they recursively break down a spline / curve to calculate its length. I was hoping to avoid that, but if I can't, then I won't :). I also need to calculate X distance from the start or end, and also Y distance from X. For example, if the trucks of rail car 1 are 40 feet into the curve, then the trailing trucks could be 80 or so feet later. Keep in mind that these distances are STRAIGHT lines (Rail cars aren't suppose to curve). For fixed curves (Circles) I can use coord distances, but I haven't seen anything for any type of spline. As a bonus: Can you limit the curvature of the spline?? In railroads they measure curvature in Degrees, where 1 degree is about 5700 Feet radius. I was hoping that if the curve is tighter than a certain degree, I could flag it somehow. This would mean a test or some sort, and would only be needed when trying to connect two straight lines with a smooth curve. Hopefully this ain't too much. I've been looking for over a week now, so I'm getting desperate. Hopefully someone can point me in the right direction. Code and / or a "Splines for dummies" will work GE

##### Share on other sites
Quote:
 Original post by fireangelA *few* part question.

I'm not an expert myself, but I can still try to help you! ;)

Quote:
 Basic backround. I'm trying to make a rail road model, and so I need to create transition curves between two straight lines of track, or a straight line and a fixed radius curve, or (possibly) between two different radius curves. (Also, these same curves will be used to connect different slopes as well) I also need to calculate the length of said spline. All this will be done (at some point) in 3d, but for now 2d is probably fine.I've been looking at several math places online, but they lose me when they start whipping out greek letters and matrices and stuff. I really don't need a complete understanding of all types of splines and all coordinates systems. Would be nice for later, but not right now.

I'd recommend trying to understand Bezier-Curves, I've found them to be the easiest by far. If you need to create circles or anything related, have a look at this article.

Quote:
 I guess I need to pick a type of spline (too many out there!!). I really will allready have the start and end points. I will also be able to calculate the tangents (derivitive?) at the starting and ending points of the spline (Derivitive is the slope, right?? Not the best at math anymore). So which kind is this?? ( I assume its a quadratic, but what do I know )

This is the cool think about Bezier-Curves. The slope at the starting point is the slope of the line segment from your starting point to your first control point (of which you have one if you use quadratic curves, and two if you use cubic curves). If you patch Bezier curves together, make the starting point of the second courve the ending point of the first curve, and make the last control point of the first curve in one line with the starting/ending point and the first control point of the second curve, optimally give both control points the same distance from the starting/ending point. This makes your curve third degree continious (or whatever you call it), if I recall it correctly.

Quote:
 I need the length. I've seen a few examples where they recursively break down a spline / curve to calculate its length. I was hoping to avoid that, but if I can't, then I won't :).

I've spent many hours trying to find this out myself. But I think there is no descrete way of calculating a Bezier curves arc length, you have to do it infinitesimally. This doesn't really matter though, as you can precalculate the length, since I assume that your railroad isn't gonna change much during the simulation! ;)

Quote:
 I also need to calculate X distance from the start or end, and also Y distance from X. For example, if the trucks of rail car 1 are 40 feet into the curve, then the trailing trucks could be 80 or so feet later. Keep in mind that these distances are STRAIGHT lines (Rail cars aren't suppose to curve). For fixed curves (Circles) I can use coord distances, but I haven't seen anything for any type of spline.

I'm not exactly sure, but do you mean the straight distance between two points on the curve, right? I'm sure this is possible, but I'm also sure I don't know how! ;) You can't use a simple linear equation, since your direction vector changes when "moving along the curve" to find the desired distance. If you only use circle segments, this becomes a whole lot easier, I would think.

Quote:
 As a bonus: Can you limit the curvature of the spline?? In railroads they measure curvature in Degrees, where 1 degree is about 5700 Feet radius. I was hoping that if the curve is tighter than a certain degree, I could flag it somehow. This would mean a test or some sort, and would only be needed when trying to connect two straight lines with a smooth curve.

The "curvature" is the second derivative.

Quote:
 Hopefully this ain't too much. I've been looking for over a week now, so I'm getting desperate.

Man, I know how you feel! ;)

Quote:
 Hopefully someone can point me in the right direction. Code and / or a "Splines for dummies" will workGE

Bezier curves are really easy. If you have four control points, A, B, C, and D, where A ist your starting point and D your ending point, then the curve is given by: At^3 + Bt^2(1-t) + Ct(1-t)^2 + D(1-t)^3, where t is in the interval [0,1]. The curve then passes through the starting and ending point, and the shape of the curve is influenced by the two inner control points. If you draw this, you'll probably increase t by small amounts. The problem with this is that the output segments are shorter if the curvature is high, and longer if the curvature is low. I've not found any solution (that was immediately obvious to me) to this yet, but you would need a function that gives you the t value for a specific arc length.

Ok, this is as much as I can tell you right now, it's 1am here and I should really go to bed! ;)

Hope to have helped!

Cheers,
Drag0n

##### Share on other sites
Quote:
 Original post by Drag0nThe "curvature" is the second derivative.

This is incorrect terminology. Curvature measurements are scalars, whereas second derivatives are vectors. In the case of a scalar function y = f(x), though, they are both scalars. The first derivative is denoted f'(x) and the second derivative is denoted f"(x). The curvature is f"(x)/pow(1+f'(x)*f'(x),1.5). This is not the same quantity as the second derivative.

For a parametric 2D curve (x(t),y(t)), the second derivative is the vector (x"(t),y"(t)), but the curvature is K(t) = (x'(t)*y"(t)-x"(t)*y'(t))/pow(x'(t)*x'(t)+y'(t)*y'(t),1.5).

##### Share on other sites
beziers are a good way.

although the most inituatuative way to look at them is in terms of de casteljau steps. id search for that term.

##### Share on other sites

I had kind of got the understanding that with Cubic and Quadratic Splines you CAN calculate the length. (Are they really the same thing?) But nobody actually did it. That is why I was leaning toward using them.

I also thought that since with a bezier curve, since you had two control points, it made the math WAY to hard to calculate, and so people didn't. Hence, why splines are used instead (Excpet in Microsofts GDI+ / .net world).

Some places that I'm still trying to figure out (I Hate it when all things aren't labeled):

This is the one I really hope to solve, since it looks like this is what I need. However, I can't seam to figure out what b (little b) is...
http://www.du.edu/~jcalvert/railway/transpir.htm

This one kind of just shows it and gives some other info. But, of course, not any mention of how to get the length. However, this would help in drawing if I go this route.
http://www.trackplanning.com/easements.htm

http://www.gamedev.net/reference/articles/article1808.asp
Ok. This is a good one. HOwever. It looks like the Quadratic listed here would infact be a smooth curve. Infact, it does look like it could be a Bezier curve with control points 1 and 2 being the same point ( which is what Drag0n was saying )

More thoughts. Wouldn't I need a spiral of some kind?? The problem with bezier curves is that I need to have the ending point being the tightest curve if I have a circle involved in any way ( IE, the curve gets more pronouned the closer I get to the sharper curve ). For two straights, they are perfect.

I'm going to see if I can figure out the first website. Atleast start on a simple program to see what I can get from it. Got other deadlines to meet and all :)

Thanks again
GE

##### Share on other sites
Quote:
 Original post by fireangelI had kind of got the understanding that with Cubic and Quadratic Splines you CAN calculate the length. (Are they really the same thing?) But nobody actually did it. That is why I was leaning toward using them.I also thought that since with a bezier curve, since you had two control points, it made the math WAY to hard to calculate, and so people didn't. Hence, why splines are used instead (Excpet in Microsofts GDI+ / .net world).

As for arc length, with cubic splines remember you get a set of parametric equations (for example, x = f(t) and y=g(t) where your coordinates are (x,y) and t is the "parameter") Finding the arc length is simply a matter of using the arc length formulae for parametric equations(google on this), which is not terribly advanced calculus(maybe 2nd or 3rd semester calc).

For Bezier curves: you could calculate, IIRC, a "polygon" around the points which you wish the bezier to pass though(not just the 1st or last but all of them), and calculate the proper control points to influence the curve to pass though the desired point. My experience with beziers ends here so someone else will have to jump in.

As for the matrices and greek letters, unfortunately they are intimately intertwined with the mathematics of curves. If you search more for splines for computer graphics, you may find more explainations more "algorithmically", as pseudocode and such, rather than matrix equations, that might be easier to understand.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 11
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015646
×