Why does a physics engine produce different results each run through?

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

• Are you using time since last frame? That could very well cause suble (sometimes even brutal) difference. And are you using floating point numbers? – Roy T. Apr 10 '12 at 19:49
• It's obviously chaos theory and quantum mechanics. – jcora Apr 10 '12 at 20:22
• 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. – aaaaaaaaaaaa Apr 10 '12 at 21:04
• 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. – RegDwight Apr 10 '12 at 23:02
• 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. – Mike Cluck Apr 11 '12 at 0:13

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.

• 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. – Steven Stadnicki Apr 10 '12 at 20:17
• 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. – notlesh Apr 10 '12 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.

• Also note that there are some differences when handling floating points on different computers which can cause differences. – Roy T. Apr 10 '12 at 20:14
• Pointing to helpful articles is always nice, but I thin you should put some effort and write your own explanation (answer) as well. – XenoRo Oct 2 '12 at 16:50
• @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. – Matsemann Oct 2 '12 at 16:57