【问题标题】:Adjusting the speed of 2 different object on console在控制台上调整 2 个不同对象的速度
【发布时间】:2021-08-10 13:31:58
【问题描述】:

对不起,这是个很蹩脚的问题。

【问题讨论】:

    标签: c++ visual-studio console-application


    【解决方案1】:

    您不想使用sleep_for。它可以睡更长时间,并且不提供时间保证。你想要它一个固定的更新游戏循环。您可以查看流行的游戏引擎,看看它是如何实现的。

    但归结为以下伪代码

    //mainloop
    // get a number of frame based on time.
    // take a number of frame higher than expected frame rate. say 10 ms.
    int frame = now() / frameDuration; 
    
    while(true)
    {
        int frameNow = now() / frameDuration;
        
        while(frame != frameNow)
        {
            Update();
            frame++;
        }
    
        render();
    }
    

    具有以下更新功能:(再次伪代码)

    // there, you advance you object for a fixing duration, say 10 ms.
    // just use different speed for each
    Update()
    {
        obstacle.position += obstacleSpeed * frameDuration;
        character.position += characterSpeed * frameDuration;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2017-04-19
      • 2013-05-23
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2017-08-19
      相关资源
      最近更新 更多