Featured image of post State Interpolation & Prediction Algorithms in Cloud Multiplayer Games

State Interpolation & Prediction Algorithms in Cloud Multiplayer Games

How state interpolation and prediction algorithms work in cloud multiplayer games

Introduction

In cloud-based multiplayer games, network latency is one of the biggest challenges.

Players expect smooth movement, but real-time communication between the server and clients comes with annoying delays.

To address this, State Interpolation & Prediction algorithms are used to create a smoother multiplayer experience.

What We Will Discuss:

  • The fundamentals of network lag and latency compensation
  • State Interpolation: How it smooths out movement in laggy environments
  • State Prediction: How it reduces perceived lag
  • The differences between server-authoritative and client-side prediction
  • How to implement these concepts in Unity with Photon

Understanding Network Latency in Multiplayer Games

1. The Problem: Lag and Delay

When a player moves, their action must be sent to the server and then distributed to all other players. This introduces:

  • Ping Delay: The time it takes for a packet to travel between a client and the server.
  • Packet Loss: When packets fail to reach their destination, causing stutter.
  • Out-of-Order Packets: Network congestion can cause packets to arrive out of order, leading to jerky motion.

We need State Interpolation & Prediction to counteract these issues. 🚀


What is State Interpolation?

How It Works

State Interpolation smooths movement by buffering past positions and rendering them slightly behind real-time.

  1. Buffer Incoming Updates 📦

    • Instead of immediately updating a character’s position when data is received, we store past positions in a buffer.
  2. Render Slightly Behind Real Time 🕰️

    • Instead of showing the latest received position, we render the character a few milliseconds behind.
  3. Use Linear Interpolation 📊

    • When a new position update arrives, we use lerp (linear interpolation) to transition between the two latest positions.

Example Code: Interpolating Player Movement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Photon.Pun;
using UnityEngine;

public class StateInterpolation : MonoBehaviourPun, IPunObservable
{
    private Vector3 lastReceivedPosition;
    private Vector3 previousPosition;
    private float interpolationTime = 0.1f;

    void Update()
    {
        if (!photonView.IsMine)
        {
            transform.position = Vector3.Lerp(previousPosition, lastReceivedPosition, interpolationTime);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(transform.position);
        }
        else
        {
            previousPosition = lastReceivedPosition;
            lastReceivedPosition = (Vector3)stream.ReceiveNext();
        }
    }
}

Pros:

  • Smooths out minor network fluctuations.
  • Works well for games where precise real-time action isn’t required (e.g., MMOs, RTS games).

🔴 Cons:

  • Adds a small input delay (~100-200ms) to ensure smoothness.
  • Doesn’t work well for fast-paced FPS or racing games.

What is State Prediction?

How It Works

Instead of waiting for server updates, State Prediction predicts where the player will be based on input and movement physics.

  1. Process Input Locally First 🎮

    • The client processes player input immediately rather than waiting for server confirmation.
  2. Send Data to the Server 📡

    • The predicted movement is sent to the server, but the client doesn’t wait for validation.
  3. Server Reconciliation 🔄

    • If the server detects a discrepancy, it corrects the player’s position.

Example Code: Predicting Player Movement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Photon.Pun;
using UnityEngine;

public class StatePrediction : MonoBehaviourPun, IPunObservable
{
    private Vector3 predictedPosition;
    private Vector3 lastServerPosition;

    void Update()
    {
        if (photonView.IsMine)
        {
            float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * 5f;
            float moveZ = Input.GetAxis("Vertical") * Time.deltaTime * 5f;
            predictedPosition += new Vector3(moveX, 0, moveZ);
            transform.position = predictedPosition;
        }
        else
        {
            transform.position = Vector3.Lerp(transform.position, lastServerPosition, 0.1f);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(predictedPosition);
        }
        else
        {
            lastServerPosition = (Vector3)stream.ReceiveNext();
        }
    }
}

Pros:

  • Eliminates perceived lag for local players.
  • Essential for fast-paced games like FPS and racing games.

🔴 Cons:

  • More complex to implement.
  • May cause “rubber-banding” if reconciliation isn’t handled well.

Interpolation vs. Prediction: Which One to Use?

FeatureState InterpolationState Prediction
Smoothness✅ Very smooth⚠️ May be jerky
Input Delay⚠️ Small Delay✅ No Delay
Fast-paced Games❌ Not Ideal✅ Best Choice
Implementation Complexity✅ Easy⚠️ Harder

Key Ideas

  • Use Interpolation for slower-paced games (RPGs, strategy, open-world games).
  • Use Prediction for fast-paced competitive games (FPS, racing, fighting games).
  • Hybrid Approach: Many AAA games use a combination of both!

NOTE
Unity3d is very versatile and can target many platforms. All the techniques here apply to iPhone-iOS, Android , as well as Windows and Mac.

Unity3d has the ability to target web pages- by compiling to web assembly. BUT the caveat is when you are running on a web page, your code can talk back to the server it came from , but cannot make calls out to other domains (cross origin issues).