There are no random numbers in the engine and everything is calculated the same way. How can it happen differently each time?

This is on my own small physics engine that I made earlier that is written in lua and runs in the Moai SDK

  • \$\begingroup\$ Are you using time since last frame? That could very well cause suble (sometimes even brutal) difference. And are you using floating point numbers? \$\endgroup\$
    – Roy T.
    Commented Apr 10, 2012 at 19:49
  • 3
    \$\begingroup\$ It's obviously chaos theory and quantum mechanics. \$\endgroup\$
    – jcora
    Commented Apr 10, 2012 at 20:22
  • 1
    \$\begingroup\$ You wrote the code, and something in that code is not deterministic, you have got a few bids, but they are shots in the dark, and provide no info you couldn't have found on Google yourself. If you want real help we got to know how your engine works. \$\endgroup\$ Commented Apr 10, 2012 at 21:04
  • \$\begingroup\$ Too broad, too vague. I'd vote to close as NARQ if I had the rep. Without lots of additional information, anyone's guess is as good as anyone else's (as you can see from the various answers so far, which really should be just comments). Give us something to work with. This is in your own interest. A good, clear question will get you a good, clear answer. Throwing a one-liner at the wall and hoping that it sticks will only get you people throwing one-liners right back at you. \$\endgroup\$
    – RegDwight
    Commented Apr 10, 2012 at 23:02
  • \$\begingroup\$ Had to give you the downvote. This question can be boiled down to "my physics engine acts funny, why is that?" Without more details I wouldn't consider this a real question. \$\endgroup\$
    – Mike Cluck
    Commented Apr 11, 2012 at 0:13

3 Answers 3


Use the same time steps every time. My physics engine is set to use 33 ms time steps, and I can produce the exact same simulations that way (assuming I use the same machine.) If I use different time steps, even 1 ms more, the game will slowly diverge.

You can do this with this basic loop:

void update( long timeMS ) {
    _accumulatedTime += timeMS;
    while ( _accumulatedTime >= _timeStepMS ) {
        // do physics simulation
        _accumulatedTime -= _timeStepMS;

This will ensure that you always run your physics simulation with exact time steps, and will also ensure that you never lose any time.

  • \$\begingroup\$ What sort of differences in floating-point number handling are you thinking of? While that's certainly a possibility, there's more than enough commonality in floating-point arithmetic at this point (including the IEEE standard, even if devices aren't always 100% faithful to it in some corner cases) that that would be way, way down my list of guesses. \$\endgroup\$ Commented Apr 10, 2012 at 20:17
  • \$\begingroup\$ I mostly threw that out there to make sure he was aware that it's one possible issue. You're right, though, it's not likely the issue here. I've removed that bit as it's probably not constructive. \$\endgroup\$
    – notlesh
    Commented Apr 10, 2012 at 20:25

Read here for a good explanation of timestep http://gafferongames.com/game-physics/fix-your-timestep/ Timestep should be fixed, and this article takes a good approach and explains everything clearly.

  • 2
    \$\begingroup\$ Also note that there are some differences when handling floating points on different computers which can cause differences. \$\endgroup\$
    – Roy T.
    Commented Apr 10, 2012 at 20:14
  • \$\begingroup\$ Pointing to helpful articles is always nice, but I thin you should put some effort and write your own explanation (answer) as well. \$\endgroup\$ Commented Oct 2, 2012 at 16:50
  • \$\begingroup\$ @TheLima I know that now, but seeing this was my first answer here I did not know back then. Feel free to edit my answer to make it better. \$\endgroup\$
    – Matsemann
    Commented Oct 2, 2012 at 16:57
  • \$\begingroup\$ Well, you are the author of the post, not me; I can "make your answer better", but I cannot add an answer in your stead. If I were to do that, I would rather add my own answer and receive the credit for what I answer; This tough is unnecessary, as the problem here has already been addressed in ways very similar to what my answer would be. \$\endgroup\$ Commented Oct 2, 2012 at 18:23

This is difficult to answer without more details regarding the engine itself. My hunch, however, would be to look for race conditions in your code.

The kinds of details that would be useful include:

  • Is your engine multi-process or multi-threaded?
  • Is your engine event-driven?
  • How are you guaranteeing your test is consistent?

You must log in to answer this question.