Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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
3  
It's obviously chaos theory and quantum mechanics. –  jco Apr 10 '12 at 20:22
1  
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. –  eBusiness 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 C Apr 11 '12 at 0:13
show 2 more comments

3 Answers 3

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  stephelton Apr 10 '12 at 20:25
add comment

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.

share|improve this answer
2  
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. –  TheLima 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
    
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. –  TheLima Oct 2 '12 at 18:23
add comment

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?
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.