如何从展示中分离游戏逻辑? [英] How do you separate game logic from display?

查看:130
本文介绍了如何从展示中分离游戏逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让每秒显示的帧从游戏逻辑是独立的?之所以如此,是游戏逻辑运行在相同的速度,无论显卡如何能快速呈现。

How can you make the display frames per second be independent from the game logic? That is so the game logic runs the same speed no matter how fast the video card can render.

推荐答案

我觉得这个问题揭示了一点如何游戏引擎的设计应误解。这是完全正常的,因为他们都是该死的复杂的东西,是很难得到的权利;)

I think the question reveals a bit of misunderstanding of how game engines should be designed. Which is perfectly ok, because they are damn complex things that are difficult to get right ;)

您正在你想要什么叫做帧速率独立正确的即时通讯pression。但是,这并不仅指渲染帧。

You are under the correct impression that you want what is called Frame Rate Independence. But this does not only refer to Rendering Frames.

在单线程游戏引擎的框架通常被称为蜱。每蜱你处理输入,处理游戏逻辑,和呈现基于断处理的结果的帧

A Frame in single threaded game engines is commonly referred to as a Tick. Every Tick you process input, process game logic, and render a frame based off of the results of the processing.

您想要做的就是能够处理你的游戏逻辑,在任何FPS(每秒帧数),并有确定的结果是什么。

What you want to do is be able to process your game logic at any FPS (Frames Per Second) and have a deterministic result.

这成为在以下情况下一个问题:

This becomes a problem in the following case:

检查输入:   - 输入是关键:'W',这意味着我们将玩家角色前进10单位:

Check input: - Input is key: 'W' which means we move the player character forward 10 units:

playerPosition + = 10;

playerPosition += 10;

现在,因为你这样做的每一帧,如果你在30 FPS的速度运行,你将移动每秒300单位。

Now since you are doing this every frame, if you are running at 30 FPS you will move 300 units per second.

但如果你是不是在10 FPS的速度运行,你只会将100台每秒。就这样,你的游戏逻辑的没有的帧速率无关。

But if you are instead running at 10 FPS, you will only move 100 units per second. And thus your game logic is not Frame Rate Independent.

令人高兴的是,要解决这个问题,让你的游戏逻辑框架率独立是一个相当简单的任务。

Happily, to solve this problem and make your game play logic Frame Rate Independent is a rather simple task.

首先,你需要一个计时器,它会计算每帧需要渲染的时间。这个数字以秒计算(所以0.001秒〜完成一个节拍),然后通过什么都那就是你想要帧率独立成倍增加。因此,在这种情况下:

First, you need a timer which will count the time each frame takes to render. This number in terms of seconds (so 0.001 seconds to complete a Tick) is then multiplied by what ever it is that you want to be Frame Rate Independent. So in this case:

当持有W

playerPosition + = 10 * frameTimeDelta;

playerPosition += 10 * frameTimeDelta;

(三角洲是一个奇特的字改变的东西)

所以,你的播放器将移动10某一部分在一个单一的蜱和蜱后一个完整的第二个,你会感动的全部10台。

So your player will move some fraction of 10 in a single Tick, and after a full second of Ticks, you will have moved the full 10 units.

不过,这会掉下来,当涉及到性能,其中变化率也随时间而改变,例如加速车辆。这可以通过使用更先进的积分器来解决,如verlet的

However, this will fall down when it comes to properties where the rate of change also changes over time, for example an accelerating vehicle. This can be resolved by using a more advanced integrator, such as "Verlet".

如果您仍然有兴趣在回答你的问题(因为我没有回答,但presented替代),在这儿呢。分离游戏逻辑和渲染成不同的线程。它有它的背上画不过。足以使游戏引擎的绝大多数仍然单线程的。

If you are still interested in an answer to your question (since I didn't answer it but presented an alternative), here it is. Separating Game Logic and Rendering into different threads. It has it's draw backs though. Enough so that the vast majority of Game Engines remain single threaded.

这并不是说没有在所谓的单线程引擎上运行的永远只能有一个线程。但是,所有显著的任务通常是一个中心主题。有些东西像碰撞检测可以是多线程的,但一般的蜱阻塞,直到所有线程的冲突阶段已经返回,并且发动机是回执行一个线程。

That's not to say there is only ever one thread running in so called single threaded engines. But all significant tasks are usually in one central thread. Some things like Collision Detection may be multithreaded, but generally the Collision phase of a Tick blocks until all the threads have returned, and the engine is back to a single thread of execution.

多线程presents一个整体,非常大类问题,甚至因为一切的一些性能的,甚至是容器,必须是线程安全的。而游戏引擎是非常复杂的程序,首先,所以很少值得多线程他们的更加复杂。

Multithreading presents a whole, very large class of issues, even some performance ones since everything, even containers, must be thread safe. And Game Engines are very complex programs to begin with, so it is rarely worth the added complication of multithreading them.

最后,作为另一个评论者指出,有一个固定大小的时间步骤和控制多久你步游戏逻辑也可处理这一具有很多好处非常有效的方法。

Lastly, as another commenter noted, having a Fixed size time step, and controlling how often you "step" the game logic can also be a very effective way of handling this with many benefits.

链接是为了完整性,但其他评论者还链接到它: 修复你的时间步长

Linked here for completeness, but the other commenter also links to it: Fix Your Time Step

这篇关于如何从展示中分离游戏逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆