开发者

How is time-based programming done?

开发者 https://www.devze.com 2023-01-19 19:30 出处:网络
I\'m not really sure what the correct term is, but how are time-based programs like games and simulations made?I\'ve just realized that I\'ve only wrote programs that wait for input, then do something

I'm not really sure what the correct term is, but how are time-based programs like games and simulations made? I've just realized that I've only wrote programs that wait for input, then do something, and am amazed that I have no idea how I would write something like pong :)

How would something like a flight simulator be coded? It obviously wouldn't run as fast as the computer could run it. I'm guessing everything is executed on some kind of cycle. But how do you handle it when a computation takes longer than the cycle.

Also, what is the correct term for this? Searching "time-based programming" doesn't re开发者_如何学Cally give me helpful results.


Games are split into simulation (decide what appears, disappears or moves) and rendering (show it on the screen, play sounds). Simulation is designed to be time-dependent: you can tell the simulator "50ms have elapsed" and it will compute 50ms worth of simulation. A typical game loop will render (which takes an arbitrary amount of time), then run the simulator for the duration since the last time the simulator was run.

  • If the code runs fast, then the simulator steps will be short (only a few ms) and the game will render the scene more often.

  • If the code runs slowly, the simulator steps will have longer steps and there will be proportionally fewer renders.

  • If the simulator runs slower than the simulation itself (it takes 100ms to compute 50ms worth of simulation) then the game cannot run. But this is an exceedingly rare situation, and games sometimes have emergency systems that drop the quality of the simulation to improve performance when this happens.

Note that time-dependent does not necessarily mean millisecond-level precision. Some systems implement simulations using time-based functions (traveled distance equals speed times elapsed time), while others run fixed-duration simulation steps.


I think the correct term is "Real-time application".

For the first question, I'm with spender's answer. If you know the elapsed time between two frames, you can calculate (with physics, for example) the new position of the elements based on the previous ones.


There are two approaches to this, each with advantages and disadvantages.

You can either go frame based, whereby a timer signals n new frames every second. You calculate movement simply by counting elapsed frames. In the case that computation exceeds the available time, the game slows down.

...or, keeping the frame concept, but this time you keep an absolute measure of time, when the next frame is signalled, you calculate world movement via the amount of elapsed time. This means that stuff happens in real-time, but in the case of severe CPU starvation, gameplay will become choppy.


There's an old saying that "the clock is an actor". Time-based programs are event-driven programs, but the clock is a constant source of events. At least, that's a fairly common and reasonably easy way of doing things. It falls down if you're doing hard realtime or very high performance things.


This is where you can learn the basics: http://www.gamedev.net/reference/start_here/

Nearly all of the games are programmed in real time architecture and the computer capabilities(and the coding of course :)) determine the frame rate.

Game programming is a really complex job including object modeling, scripting, math calculations, fast and nice rendering algorithms and some other stuff like pixel shaders. So i would recommend you to check out available engines in the first place.(just google "free game engine")

Basic logic is to create an infinite loop (while(true){}) and the loop should:

  • Listen for the callbacks - you get the keyb, mouse and system messages here.
  • Do the physics due to the time passed till the previous frame and user inputs.
  • Render the new frame (gdi, derictX or openGL)

Have fun


Basically, there are 2 different approaches that allow you to add animation to a game:

  • Frame-based Animation: easier to understand and implement but has some serious disadvantages. Think about it this way: imagine your game runs at 60FPS and it takes 2 seconds to draw a ball that goes from one side of the screen to the other. In other words, the game needs 120 frames to move the ball along the screen. If you run this game on a slow computer that's only able to render 30FPS, it means that after 2 seconds the ball will be at the middle of the screen. So the problem of this approach is that rendering (drawing the objects) and simulation (updating the positions of the objects) are done by the same function.

  • Time-based Animation: a sophisticated approach that separates the simulation code from the rendering code. The amount of FPS the computer can render will not influence the amount of movement (animation) that has to be done in 2 seconds.

Steven Lambert wrote a fantastic article about these techniques, as well as 3rd approach that solves a few problems with Time-based Animation.

Some time ago I wrote a C++/Qt application to demonstrate all these approaches and you can find a video of the prototype running here:

How is time-based programming done?

Source code is available on Github.


Searching for time-based movement will give you better results.

Basically, you either have a timer loop or an event triggered on a regular clock, depending on your language. If it's a loop, you check the time and only react every 1/60th of a second or so.

Some sites

  • http://www.cppgameprogramming.com/
  • Ruby game programming
  • PyGame


Flight Simulation is one of the more complex examples of real-time simulations. The understanding of fluid dynamics, control systems, and numerical methods can be overwhelming.

As an introduction to the subject of flight simulation, I recommend Build Your Own Flight Sim in C++. It is out of print, but seems to be available used. This book is from 1996, and is horribly dated. It assumes a DOS environment. However, it provides a good overview of the topics, covers numerical integration, basic flight mechanics and control systems. The code examples are simplistic, reasonably complete, and do not assume the more common toolsets used for graphics today. As with most things, I think it is easier to learn the subject with a more basic reference.

A more advanced text (college senior, first year graduate school) is Principles of Flight Simulation provides excellent coverage of the breadth of topics involved in making a flight simulation. This book would make an excellent reference for anyone seriously interested in flight simulation as an engineering task, or for more realistic game development.

0

精彩评论

暂无评论...
验证码 换一张
取 消