用c ++创建游戏制作者的引力

时间:2012-07-26 12:42:09

标签: c++ gravity

在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毫秒)更新以减少lagg,因此我创建此脚本以在需要时获取正确的Y值而不是每一步。

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

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

测试1
 -Server:384
 -Client:384
  - 描述:完全静止,完美运作 测试2
 -Server:373
 -Client:349.50
  - 描述:完全静止,完美运作 测试3
 -Server:318
  - 客户:279.50
  - 描述:完全静止,完美运作。

因此,当玩家跳跃时,该值意味着减少,因为Y越来越小,这在客户端和服务器上都有效,除了vaules之外。 在玩家因重力而开始摔倒之后,服务器继续读取“318”,直到玩家到达地面并且值得到更新。

1 个答案:

答案 0 :(得分:1)

可能是因为整数除法:

此示例中的

1000/30将等于33而不是33.333

您需要将其更改为

1000.0/30.0

此外,您将结果保存为intrpt可能应为double类型。

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