我不太确定正确的术语是什么,但是像游戏和模拟这样的基于时间的程序是如何制作的呢?我刚刚意识到我只编写了等待输入然后执行某些操作的程序,并且令我惊讶的是我不知道如何编写像 pong 这样的东西:)
如何对飞行模拟器之类的东西进行编码?显然它的运行速度不如计算机的运行速度。我猜一切都是在某种循环上执行的。但是当计算时间比周期长时如何处理呢?
另外,这个的正确术语是什么?搜索“基于时间的编程”并没有真正给我有用的结果。
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 really give me helpful results.
发布评论
评论(8)
游戏分为模拟(决定什么出现、消失或移动)和渲染(在屏幕上显示、播放声音)。模拟被设计为与时间相关:您可以告诉模拟器“已经过去了 50 毫秒”,它将计算 50 毫秒的模拟值。典型的游戏循环将进行渲染(需要任意时间),然后在自上次运行模拟器以来的持续时间内运行模拟器。
如果代码运行速度快,那么模拟器步骤将会很短(只有几毫秒),游戏将更频繁地渲染场景。
如果代码运行缓慢,模拟器步骤将有更长的步骤,并且渲染也会相应减少。
如果模拟器的运行速度比模拟本身慢(计算 50 毫秒的模拟需要 100 毫秒),则游戏无法运行。但这种情况极其罕见,游戏有时会有紧急系统,在发生这种情况时会降低模拟质量以提高性能。
请注意,时间相关并不一定意味着毫秒级精度。一些系统使用基于时间的函数(行驶距离等于速度乘以经过的时间)来实现模拟,而其他系统则运行固定持续时间的模拟步骤。
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.
我认为正确的术语是“实时应用程序”。
对于第一个问题,我同意spender的回答。
如果您知道两帧之间经过的时间,则可以根据先前的位置计算(例如,通过物理)元素的新位置。
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.
有两种方法可以实现这一点,每种方法都有优点和缺点。
您可以基于帧,计时器每秒发出
n
个新帧信号。您只需计算经过的帧即可计算运动。如果计算超过可用时间,游戏就会变慢。...或者,保留帧概念,但这次您保留绝对的时间度量,当下一帧发出信号时,您可以通过经过的时间量来计算世界运动。这意味着事情是实时发生的,但在 CPU 严重不足的情况下,游戏玩法将会变得不稳定。
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.
基本上,有两种不同的方法可以让您向游戏添加动画:
基于帧的动画:更容易理解和实现,但有一些严重的缺点。这样想:想象一下您的游戏以 60FPS 运行,并且需要 2 秒才能绘制一个从屏幕一侧到另一侧的球。换句话说,游戏需要 120 帧才能让球沿着屏幕移动。如果您在只能渲染 30FPS 的慢速计算机上运行此游戏,则意味着 2 秒后球将位于屏幕中间。所以这种方法的问题是渲染(绘制对象)和模拟(更新对象的位置)是由相同的人完成的
基于时间的动画:一种将模拟代码与渲染代码分开的复杂方法。计算机可以渲染的 FPS 量不会影响 2 秒内必须完成的运动量(动画)。
Steven Lambert写了一篇关于这些技术的精彩文章< /a>,以及第三种方法,解决了基于时间的动画的一些问题。
不久前,我编写了一个 C++/Qt 应用程序来演示所有这些方法,您可以找到一个视频此处运行的原型:
源代码可在 Github 上获取。
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:
Source code is available on Github.
有句老话叫“时钟是演员”。基于时间的程序是事件驱动的程序,但时钟是事件的恒定源。至少,这是一种相当常见且相当简单的做事方式。如果你正在做硬实时或非常高性能的事情,它就会崩溃。
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.
您可以在这里学习基础知识:
http://www.gamedev.net/reference/start_here/
几乎所有游戏都是实时架构编程和计算机能力(当然还有编码:))决定帧速率。
游戏编程是一项非常复杂的工作,包括对象建模、脚本编写、数学计算、快速而漂亮的渲染算法以及像素着色器等其他一些东西。
所以我建议你首先检查可用的引擎。(只需谷歌“免费游戏引擎”)
基本逻辑是创建一个无限循环(while(true){})并且循环应该:
玩得开心
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:
Have fun
搜索基于时间的移动将为您提供更好的结果。
基本上,您要么有一个计时器循环,要么有一个在常规时钟上触发的事件,具体取决于您的语言。如果是循环,则检查时间并仅每隔 1/60 秒左右做出反应。
一些网站
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
飞行模拟是实时模拟中较为复杂的示例之一。对流体动力学、控制系统和数值方法的理解可能是压倒性的。
作为飞行模拟主题的介绍,我建议用 C++ 构建您自己的飞行模拟< /a>.该书已绝版,但似乎可以使用。这本书是 1996 年的,而且已经过时了。它假定一个 DOS 环境。然而,它很好地概述了这些主题,涵盖数值积分、基本飞行力学和控制系统。这些代码示例非常简单、相当完整,并且没有假设当今用于图形的更常见的工具集。与大多数事情一样,我认为通过更基本的参考来学习该主题会更容易。
更高级的文本(大学四年级,研究生一年级)是飞行模拟原理 很好地涵盖了飞行模拟所涉及的广泛主题。对于任何对飞行模拟作为工程任务或更现实的游戏开发感兴趣的人来说,这本书都是一本很好的参考书。
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.