Java 多人游戏中的UDP缓冲和插值

Java 多人游戏中的UDP缓冲和插值,java,libgdx,real-time,multiplayer,kryonet,Java,Libgdx,Real Time,Multiplayer,Kryonet,我们正在开发一个相当简单的2D多人平台射击游戏,在为世界实体(玩家、子弹等)的状态构建UDP缓冲区时遇到了一些问题。目前,我们正在以30次/秒的速度发送每个游戏实体的位置(x,y)及其x和y速度。只要没有数据包丢失,这就给了我们一个相当平稳的体验。然而,如果一个数据包丢失了,我们就会感到不安。为了避免这种情况,我们希望在最后已知的位置之间使用插值 我们使用Libgdx进行游戏开发,我们的网络服务使用kryonet库完成 问题是: 如何构造简单可靠的UDP缓冲区?我们尝试使用发送时间和到达时间之间

我们正在开发一个相当简单的2D多人平台射击游戏,在为世界实体(玩家、子弹等)的状态构建UDP缓冲区时遇到了一些问题。目前,我们正在以30次/秒的速度发送每个游戏实体的位置(x,y)及其x和y速度。只要没有数据包丢失,这就给了我们一个相当平稳的体验。然而,如果一个数据包丢失了,我们就会感到不安。为了避免这种情况,我们希望在最后已知的位置之间使用插值

我们使用Libgdx进行游戏开发,我们的网络服务使用kryonet库完成

问题是:

  • 如何构造简单可靠的UDP缓冲区?我们尝试使用发送时间和到达时间之间的时间差,以便将数据包在接收端的执行延迟50毫秒。但要么我们做得不对,要么插值本身有缺陷,因为抖动变得更严重

  • 如何正确地线性插值旅行轨迹

  • 下面的代码是插值方法的一个片段

    //Called every time a UDP packet has arrived adding it to the queue.
    public void addEntityPacket(EntityPacket pkt){
        entityStates.add(pkt);
    }
    
    //Called each Gdx update (60times/s). Linearly interpolates (lerp)
    //the entity with the next entityState in queue using getAlpha().
    public void updateEntityState(){
        if (entityStates.size > 0) {
            EntityPacket pkt = entityStates.pop();
            currentPos.set(body.getPosition()); // sets currentPos to the entitys current position.
            targetPos.set(pkt.xp, pkt.yp); // sets the targeted to the next state position retrieved from the queue.
            interpolatedPos.set(currentPos).lerp(targetPos, getAlpha()); // calculates the linear interpolation using getAlpha().
            body.setTransform(interpolatedPos, 0); // Moves the entity to the calculated position
            body.setLinearVelocity(pkt.xf, pkt.yf); // Sets the velocity
        }
    }
    
    //Called when calculating interpolation.
    public float getAlpha(){
        long now = TimeUtils.millis(); // current time
        float alpha = (now - lastUpdateTime) / 30f; // time difference divided by update frequency.
        lastUpdateTime = now;
        return MathUtils.clamp(alpha, 0f, 1.0f); // We do not want the alpha to be less than 0 or greater than 1 or the interpolation will behave strangely.
    }