A Track to Control a Carriage Following It

This impossible roller coaster https://www.babylonjs-playground.com/#SQFG0Q#6 is built from a new function I have developed that builds a track that also contains data on the orientation of any mesh that follows it. You set this orientation data as you wish over sections of the track. It is also possible for a ‘passenger’ in the carriage to turn (relative to its local y axis) to look around as it travels with the carriage. You can see that the passenger turns to look at its friend in red as it starts its ride. This PG gives an overview of the coaster https://www.babylonjs-playground.com/#SQFG0Q#5 .

The data in the track is also used to build the roller coaster rails and sleepers. Over the next week (or so) I will write how to build with the track and details of the function’s coding and PR these into the snippets section of the docs.

At some stage I might even write an editor to make building a track easier. Happy playing :smiley:

EDIT: I really should mention that I was inspired, as I often am, by one of @Wingnut’s ‘can we code Babylon.js to do this’ suggestions. Often like to give them a go.


What causes the flickering to occur when going around curves? At some point I am going to have to make a moving camera that follows a path, and am looking to avoid any issues that might occur.

Good question to which I do not know the answer. I was just pleased to get the camera to do what I wanted. Smoothing it out would be good.

This looks great and fun to play with! It reminds me of @jerome’s roller coaster:

1 Like

excellent !!
To avoid the flickering, you could force the camera view matrix recomputation before each frame (I can’t remember the method … scene.getViewMatrix() or something like this)

1 Like

Those curves are fantastic!!! :grin:

For “A Track to Control a Carriage Following It”… it is a tough challenge (to me).

Would a bone make a good train-car-coupler?

OR MAYBE just run a clone n sections back…?

Through the same exact normal math.

Thanks JohnK


1 Like

That’s one way - https://www.babylonjs-playground.com/#SQFG0Q#7




(link above) by JohnK

Rollercoaster editor across (infinite) random-block-city :+1:

1 Like

Docs now ready A Carriage Following a Track - Babylon.js Documentation

Docs TL;DR then just have a play with a short straight track https://www.babylonjs-playground.com/#HSMDF2#10 everything except show vectors requires the Update Track button for implementation


Thanks for this @JohnK ! :slight_smile:

Your documentation and math will probably help me fix some train track bending issues on helix curves in some of my BabylonJS :steam_locomotive::railway_car::railway_car: Train Game levels (http://www.geektrains.com).

I’ll take a further studying look in the weekend.


1 Like

I’ve looked at the playground and the documentation page you’ve created @JohnK It’s very well documented! Also with options to specify how you want the track between the points to lean and wave. This will definitely help more users get some kind of rollercoaster into their app or some scenic camera movement play.

I have the idea that in my game http://www.geektrains.com I take the rail track building from a different approach. It’s just different, not better or worse. The recipe is as follows: I take a carriage, or actually a wheel axle (which is bound to the track), and draw a set (trace) of points in 3D world space by moving that wheel axle around in local space. When I want it to turn (around world Y axis) to create a track circle for example, I create a pivot point (or actually, a PivotMesh) to the left or right of it’s local space point and move the wheel axle mesh using the inbuild rotate around pivot function.

I chose this approach simply because it is then easier for me to describe a railroad track in a game level: as if you were laying fixed track parts (straight track parts, left and right curve track parts) and connecting them to each other in an array. Very intuitive to buying model train track in a store and then placing it in a layout at home. This saves the level editor on have to know all the 3D world points beforehand (which was a difficult thing to do in more complex track layouts). One just takes a starting point and direction in 3D world space and then starts placing straight and curved tracks from that point onward to one’s desire.

The lean in my top-down (XZ-plane) approach increases when I create a Path which both turns (Y rotation) and rises/lowers (Y location). The lean comes from the Normals, Binormals and Tangents derived from the Path. So with some math tweaking taken from your sample code I might be able to restore the lean (e.g. remove the inward lean on the end of a helix curve). So it’s great to have an example now which contains many of the direction and rotation Math I’ll be needing to adjust for the built in Tangents, Normals and Binormals of Path3D. :slight_smile:

Also the lean and waves options are a funny feature to introduce on my game railroad tracks. It will definitely add to the fun!

If you want to go more complex/advanced here are some ideas: make a train (several carriages in a row, following the track), add points/turnouts/switches (a track can split into 2 tracks etc.), add bogeys under carriages (definitely some challenging math in curves as the bogey wheel axles are tied to the track and the carriage length is fixed to the bogeys center, but not straight above the track any more). :wink:

Happy coding and thanks again for taking the time to write a doc on it!