r/howdidtheycodeit • • Apr 04 '22

Seeded 3D dice-rolling?

You could use a physics engine to simulate a die roll pretty easily by initializing the die with a random rotation and velocity, then waiting to see what face is up when it stops. However, what if you want to seed the random value of the die beforehand? Would you have to pre-record a simulation for each resulting die face and play back the appropriate animation, or is there a way to dynamically ensure the 3D die rolls believably and ends up on the corresponding face?

21 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/pmain8 Apr 10 '22

Ideally this should be true, however I think the engine I'm using is supposed to be non-deterministic due to floating point imprecision

2

u/nvec ProProgrammer Apr 10 '22

It's possible there's something subtle I'm missing but I can't see why this'd be the situation for fixed step physics.

If you're on about the Unity post here then that shouldn't be a problem in this case.

Part of what they're saying there is that the engine is non-deterministic in that floating point inaccuracies will accumulate over time and these errors will be slightly different on different CPU architectures.

In this case though if we're taking the position/rotation/force information from a random number generator with the same seed then each physics step will have exactly the same starting conditions, and so the first step will have the same rounding errors every run and so produce the same output... the input to the second physics step will be the same, same rounding errors, same output... and so on. Same input, same processing, same output.

The one exception to this which the post also mentions is across architecture. Here it's talking about the possibility that doing the same calculations on an x86 CPU, X64 CPU, Snapdragon 888, Apple Bionic, or Apple M1 could give different results as they could have different definitions of what a 'float, a 'double', or a 'long double' is.

This is technically true but not a problem unless you really think the player is going to go "Well, I'm having a bad result with this roll on the Windows PC, I'll see if I have more luck on my Android and iOS phones, or a shiny new Apple M1 computer". It could be an issue if you're doing a cross-platform network game and want all players to be able to view the rolls but for a single-player game, or single-platform game, it shouldn't be an issue.