I am trying to create a small racing game in which the track would be modeled using a BSpline curve for the path's center line and directional vectors to define the canting of the track at each point.

My problem is that I don't know how to calculate the correct canting / slope of the curve, in such a way that it would be optimal or at least visually nice for a car to 'slant in the corner'.

My idea was to use the direction of the 2nd derivatives of the curve, however while this approach looks fine for most of the track, there are points in which the 2nd derivative makes sharp 'twists' / very quick 180 degree flips. I also read about 'knots' of bsplines, but I don't know if such 'twist' in 2nd derivatives is a knot or knots are something else.

Can you tell me that using a BSpline: 1. How could I calculate a visually nice canting of a track for a racing game? 2. Is it possible to do this by using some simple calculations of centripetal force / gravity? 3. Is it possible to do this by using 1st, 2nd and 3rd derivatives of the BSpline curve?

I am not looking for the 'physically correct' canting angle for the track, I would just like to create something which is visually pleasing in a simple game.

I am using a framework which has a built-in class for BSpline, including support for 1st, 2nd and 3rd derivatives of the curve.

  • 2
    \$\begingroup\$ I don't quite understand what your desired result is. I think you are talking about the path of a car following the track, not rendering the track itself; is that right? Are you trying to simulate the ideal racing line? Or if something else, maybe you could post a diagram with an example of the desired path vs what you're getting now? \$\endgroup\$ Sep 12 '13 at 23:11
  • \$\begingroup\$ @Nathan: I read this more as a request for help determining the ideal camber (see en.wikipedia.org/wiki/Cant_(road/rail)) of the road surface. \$\endgroup\$
    – Mac
    Sep 12 '13 at 23:51
  • \$\begingroup\$ @caius: I agree with Natan. You should elaborate a little more. Is your problem to find the best track curve for a given car angular velocity? \$\endgroup\$ Sep 14 '13 at 14:24
  • \$\begingroup\$ Thanks for answering, and @Mac you're right, I was looking for that Cant. I have realized that the quirks at a certain point are due to cubic splines being only approximations. I finally solved it by manually filtering the discrete points. \$\endgroup\$
    – caius
    Sep 17 '13 at 15:15
  • \$\begingroup\$ I once made a road using Gaussian math, look into that :) \$\endgroup\$
    – Shaun Wild
    Dec 20 '13 at 0:19

The first derivative of the spline points along it: this is the axis which you rotate the roadway about in order to cant. The second derivative is (roughly) the curvature. The second derivative points "in" to the curve. So in a perfectly circular track, the second derivative at each point points directly towards the center of the circle. If you cant so that the second derivative direction is "lower", then you will achieve your desired outcome.

Now your problem is that the curvature changes abruptly. Splines can have different continuities, which basically means that different numbers of derivatives can be continuous. Most of the time a spline is made out of a number of sections, which are joined at then ends. For example, we can make one curve end where the other starts, but with no other constraints there would be a "kink" in the curve, as the derivatives don't have to be the same. Similarly we could require the directions to match (which is the first derivative), but there is nothing to require the second derivative to do so. What you need is cubic splines with continuous second derivatives. How you do this depends on where you are getting these curves from. If you are drawing them in Inkscape, for example, there is an option to make a given node "symmetric," which basically means continuous second derivative. Normally to get smoothness you make sure both handles are on the same line as the node, and to get nice curvature make sure both handles are the same distance from the node.

Finally, I would point out that you should cant based on the curvature, k, which is

k = y''/(sqrt(1 + |y'|^2)^3)

as the size second derivative depends on the magnitude of the first. Curvature is "physical," but a good idea because it will make the canting look natural and adapt to the scale of the turns. Just using the length of the second derivative directly might be OK, but only if the parameterization of the curve is by arc length, or at least consistent. Note curvature is a vector quantity, where it points "in" and is bigger when the turn is tighter. This article derives a formula from centripetal/gravity forces which states the optimal slope should be proportional to the curvature. (k=1/r, slope = (h_a+h_b)/G, v=speed of cars.)

From wikipedia, some useful formula: http://en.wikipedia.org/wiki/Spline_interpolation

You might need to introduce transition curves in order to keep the canting from changing too much. This is also something professional tracks would have.


Consider this generated line to be the direction of your track:

enter image description here

You could have your horizontal 'balance' line to be adjusted by the curvature of the line itself (consider you can find the red dots position like in the example i provided). Each red dot would higher the horizon line on the side it lives depending on its distance with the curve. (The challenge is to find the equivalent of the red dots. Maybe some visual representation of its derivatives can help.)

To have this basic principle working, you could calculate those horizon lines at each dot and make any kind of blending method between them (linear[not], cosine, quadratic, w/e).

Its a basic idea but i hope it will help.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.