More roller-coaster fun. Well not so much a roller-coaster at the moment just a track with roll. I wanted to examine just the roll effect of going round a curve, so as yet no height changes.

https://www.babylonjs-playground.com/#LEWFVI#1

Explanation.

As usual I like to start simply. We are building a railway consisting of a single line of rails. Each rail has a fixed length and all rails are the same height above the ground. A car moves along the rail balanced along a central fixing. For a straight railway people in the car remain vertical. For a curve made of the rails the people in the car lean towards the center of the curve.

Building a closed loop with the rails will produce a regular polygon. The more rails the larger the radius of the circle around it and the smaller the angles between the rails.

The larger the radius the less the curvature of the circle and the less the people will lean. Curvature is in fact 1/radius. At any position along the railway we will need to know its curvature to determine the amount of lean needed at that point.

My first attempts still produce wild variations in normals and binormals

So current attempts smooth these out.

TL; DR

The baseCompute function takes the generated points, an angle, and a damping factor.

The tangents and curvature at all points are calculated. The cross products of successive tangents are used to determine if the curve is inwards or outwards. When there is little difference in the x or z components of successive tangents they are in a straight line so curvature is zero. The amount of lean, theta, is calculated from the curvature found * angle * damping.

The larger the angle used the more the lean, the larger the damping the less the lean.

Angles and tangents are stored.

The angles are then smoothed using a moving average in the function smooth. This takes two parameters the array to be smoothed and the width of the smoothing w, which is how far either side of the current value to take averages from. With w = 2, the value at index 6, for example, with become the average of the values at indices, 4, 5, 6, 7, and 8. That is the larger the w the smoother the angles. Of course too large wipes out essential differences.

Once the angles have been smoothed the rollCompute calculates the normals which give the amount the car will lean away from the vertical and then the binormal using the cross product of thee tangent and normal.

Also I found the more points used the wilder the normals at the turns of the curve. So used less points and set the speed using a weight, the greater the weight the slower the car.

You can see that a large number of points with a very large width for the smooth function does not give a smooth ride https://www.babylonjs-playground.com/#LEWFVI#2. I am guessing it is something to do with the handling of very small numbers.

Plenty of parameters to play with.