![]() |
We’ve a made a new demo project using Castle Game Engine:
Throw a 6-sided dice, using physics, and cheat to make the outcome predictable. Before throwing the dice, you select the desired outcome, and we guarantee the dice will land the desired face “up”.
Full source code and data are available at https://github.com/castle-engine/castle-dice-throwing.
- You can run the demo in a web browser, done thanks to our web target support.
-
You can also download a precompiled version for Windows and Linux.
The dice trajectory is truly random, following physics and some initial (random) parameters. So, how is it possible we can guarantee the outcome?
- First we run the physics simulation without rendering it. It’s a very fast process, takes about 1/50 of the second (so you don’t even notice that we do this whole simulation when you press the button “Throw Dice”).
-
Then we apply a local rotation to the dice, to change the simulated outcome (face that is up after simulation) into the desired outcome. Since a dice has the same shape (at least for our physics approximation, and for our human feelings; in reality of course the shape is a bit different from each side), no matter how you rotate it by 90 or 180 degrees around one the primary axis, the rotated dice will look equally good during the same trajectory.
-
Only then we actually render the simulation. We simply play the previously-recorded physics simulation.
You can look at the source code, naturally, to see how this is done. The ClickThrow method with the nested procedures StartSimulation
and RecordSimulation
does the core job. If you build the example yourself, in debug mode (toggle it using the editor “Run -> Debug Mode” menu item, or command-line --mode=debug
for the build tool), then additional “debug UI” will be visible, allowing you to tweak the physics parameters and also to perform the simulation without prerecording (so the outcome is unknown).
We hope that you enjoy this demo 🙂 Recording the physics simulation (to later replay it) may have a number of use-cases, we hope that our demo makes it clear how to do it.
If you like this, please consider donating, for example by supporting us on Patreon.
Start the discussion at Castle Game Engine Forum