【问题标题】:Creating gamemaker's gravity in c++在 C++ 中创建游戏制作者的重力
【发布时间】:2012-07-26 12:42:08
【问题描述】:

在 C++ 中,我一直在尝试重新创建游戏制作者的重力,到目前为止我有这个:

double script::lengthdir_y(double len,double dir){
        return -sin(dir* M_PI/180) * len;//Create equivalent GM lengthdir_y code.
}

double script::scr_findhsy(player* plr){
        if(plr->jumping==true)
        {
                int rpt, vspeed, y;//Make variables.
                rpt=(clock()-plr->LastMovingYUpdate)/(1000/30);
                //Check the current time, and take away the time when the player started jumping to find the time inbetween, then divide it by 1000/30 to find the ammount of GM steps.
                vspeed=plr->vspeed;//Players vspeed.
                y=plr->y;//players y.
                for(int i=0; i<rpt; i++)//Plus the lengthdir_y code to the vspeed, and plus the vspeed to the y the correct ammount of times.
                {
                        vspeed+=lengthdir_y(0.5, 270);
                        y+=vspeed;
                }
                return y;//return the new value.
        }
        else
                return plr->y;//If the player isnt falling, send the original Y value, because nothing needs to be updated.
}

玩家的 y 值不会每一步都更新(或 c++ 中的 33.33 毫秒)以减少延迟,所以我创建了这个脚本来在需要时获取正确的 Y 值,而不是每一步。

但它似乎永远不会正确 O.O

这里是在客户端调试的一些测试结果 在这些测试中,客户端发送正确的 Y 值,服务器发送它认为是玩家的 Y 值:

测试 1
-服务器:384
-客户:384
- 描述:完全静止,工作完美。
测试 2
-服务器:373
-客户:349.50
- 描述:完全静止,工作完美。
测试 3
-服务器:318
-客户:279.50
- 描述:完全静止不动,完美运行。

因此,当玩家跳跃时,该值意味着减少,因为 Y 越来越小,这在客户端和服务器上都有效,除了值相差甚远。 在玩家因重力开始下落后,服务器会一直读取“318”,直到玩家撞到地面并更新数值。

【问题讨论】:

  • 会发生什么?你用的是什么框架?为什么你的代码格式如此奇特?
  • 这是我正在制作的一款网络游戏的一部分,所以代码必须和gamemaker 8.0一样,如果你对此一无所知,那你可能会帮不上忙 O.O 什么是你在说,代码看起来不错不是O.O
  • 我想我不能,但是,呃,代码看起来很丑。给我一点时间。
  • 啊,部分原因是格式不正确。我会解决的。
  • 让我们了解您的期望与您得到的可能有助于我们为您提供更好的答案。 :-)

标签: c++ gravity


【解决方案1】:

这可能是因为整数除法:

此示例中的1000/30 将等于33 而不是33.333

您需要将其更改为

1000.0/30.0

此外,您将结果保存到int,也许rpt 应该是double 类型。

我承认,我在这里抓住了稻草。

【讨论】:

  • GameMakers 步长是 33.33 毫秒,所以不应该是对的吗?好吧,它每秒有 30 步,而 C++ 秒是 1000 步 O.O Edit 我现在明白了,它没有小数位,除非除法包括小数位:P 仍然我不认为是问题所在。
  • 这意味着您将在两秒钟内完成 61 步,而 GameMakers 需要 60 步。
  • 好吧我改成1000.0/30.0,结果和我在主帖里发的基本一模一样>.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多