Hey everyone,
I finally found some time to write a post about how the coasters work that I promised before, so here it is!
I hope it's not too technical but not too simplistic either.
Defining the shape of the track piecesIf we want to simulate a coaster the first thing we need is some method for defining the shape of the track. We want to have a mathematical description that allows us to model coaster elements like straights, curves, hills and more in 3D space. It has to ensure that there's a smooth transition without sharp edges between individual segments and should ideally be fairly intuitive to use.
One such method (that, along with others, is also used by real coaster architects) are cubic Bézier curves. They look like this:
You're probably already familiar with them from vector graphics programs or other programs that allowed you to edit a curve.
The curve is defined by four control points P0, P1, P2, P3. It always goes through P0 and P3. The direction from P0 to P1 defines the tangent at P0, the direction from P3 to P2 defines the tangent at P3; they're drawn in yellow above. The longer these yellow lines are, the stronger the curve gets "pulled" towards the tangent.
For defining the entire coaster track we stitch multiple of these curves together (to a "Bézier path"). If we are careful with the tangent at the end of one curve and the tangent at the beginning of the next curve we always get a smooth transition between segments (that is, P2 and P3 from one segment and P0 and P1 from the next segment always have to be on one line):
Alright, we have those four control points, but what we really want to have are the points on the curve - how do we get those?
We evaluate the function for cubic Bézier curves for that:
C(t) = (1–t)3*P0 + 3*(1–t)2*t*P1 + 3*(1–t)*t2*P2 + t3*P3This function uses our four control points to return a point on the curve depending on the parameter t.
t goes from 0 to 1 with C(0) = P0 and C(1) = P3 and everything inbetween being the points on the curve between them:
Awesome, now we have a method for defining the shape of the track and are able to position our train on it!
TangentsFor calculating the acceleration force acting on the coaster train we'll need the tangent of the curve at any given point. We get the tangent by calculating the first derivative which is fortunately very easy for Bézier curves:
T(t) = 3*(1–t)2*(P1-P0) + 6*(1–t)*t*(P2-P1) + 3*t2*(P3-P2)Divide by the length of the vector to normalize.
OrientationWe need one more thing, and that's the up vector defining in which direction the passengers heads are pointing. This one's actually kinda tricky - we know that it has to be perpendicular to the tangent, but that's still an infinite amount of possibilities...
So far I simply took the cross product between the tangent and world up vector to get a horizontal binormal...
...and then computed the cross product between that and the tangent to get an up vector.
This worked fine so far, but fails of course if the track goes vertical (since the tangent points into the same direction as the world up vector then and we can't get a perpendicular binormal anymore). I tried a couple of approaches so far but am not entirely satisfied with any of them
(any suggestions?
).
That's it for now
Let me know if this kind of post is interesting for you (more details? less details?) and I'll try to write some more.