在单独的线程中更新物理引擎,这明智吗? [英] Updating physics engine ina separate thread, is this wise?

查看:91
本文介绍了在单独的线程中更新物理引擎,这明智吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是线程技术的100%的新手,一开始,我已经决定要考虑使用它在单独的线程中更新物理特性.我正在使用名为Farseer的第三方物理引擎,这是我正在做的事情:

I'm 100% new to threading, as a start I've decided I want to muck around with using it to update my physics in a separate thread. I'm using a third party physics engine called Farseer, here's what I'm doing:

// class level declarations
System.Threading.Thread thread;
Stopwatch threadUpdate = new Stopwatch();

//In the constructor:
PhysicsEngine()
{
(...)

            thread = new System.Threading.Thread(
                        new System.Threading.ThreadStart(PhysicsThread));
            threadUpdate.Start();

            thread.Start();
}


        public void PhysicsThread()
        {
            int milliseconds = TimeSpan.FromTicks(111111).Milliseconds;
            while(true)
            {
                if (threadUpdate.Elapsed.Milliseconds > milliseconds)
                {
                    world.Step(threadUpdate.Elapsed.Milliseconds / 1000.0f);
                    threadUpdate.Stop();
                    threadUpdate.Reset();
                    threadUpdate.Start();
                }
            }
        }

这是更新物理学的一种好方法还是应该寻找一些东西?

Is this an ok way to update physics or should there be some stuff I should look out for?

推荐答案

在游戏中,您需要将物理更新同步到游戏的帧频.这是因为您的渲染和游戏玩法将取决于物理引擎每帧的输出.而且您的物理引擎将取决于用户输入和每一帧的游戏事件.

In a game you need to synchronise your physics update to the game's frame rate. This is because your rendering and gameplay will depend on the output of your physics engine each frame. And your physics engine will depend on user input and gameplay events each frame.

这意味着在单独的线程上计算物理量的唯一优势是,它可以在其他游戏逻辑和渲染的单独CPU核上运行. (这些天对PC来说已经相当安全了,移动领域才刚刚开始使用双核.)

This means that the only advantage of calculating your physics on a separate thread is that it can run on a separate CPU core to the rest of your game logic and rendering. (Pretty safe for PC these days, and the mobile space is just starting to get dual-core.)

这允许他们同时进行物理操作和游戏/渲染-但是缺点是您需要某种机制来防止一个线程修改数据而另一线程正在使用该数据.通常这很难实现.

This allows them to both physics and gameplay/rendering run concurrently - but the drawback is that you need to have some mechanism to prevent one thread from modifying data while the other thread is using that data. This is generally quite difficult to implement.

当然,如果您的物理学不依赖用户输入(例如,愤怒的小鸟或超人的机器)(例如:用户按下播放",然后运行模拟),在这种情况下,您可以预先计算物理模拟,并记录其输出以进行回放.但是,除了阻塞主线程之外,您还可以将此耗时的操作移至后台线程-这是一个众所周知的问题.您甚至可以走到尽头,甚至在记录结束之前就开始在主线程中播放记录!

Of course, if your physics isn't dependent on user input - like Angry Birds or The Incredible Machine (ie: the user presses "play" and the simulation runs) - in that case it's possible for you to calculate your physics simulation in advance, recording its output for playback. But instead of blocking the main thread you could move this time-consuming operation to a background thread - which is a well understood problem. You could even go so far as to start playing your recording back in the main thread, even before it is finished recording!

这篇关于在单独的线程中更新物理引擎,这明智吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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