MultiConnectMultiConnectDocs Website home
MultiConnectSDK › Physics

6. Multiplayer

Physics does not need a special connection. It uses the rooms you already have from MultiConnectSDK, so create your room first, as the Developer Guide explains. Then choose one of these two ways of working.

Shots Live world
Good for Pool, carrom, bowling: one action, then watch it play out Shooters, or anything always moving
What is sent One message per action The world, many times a second
Network use Very low Higher
Players see The exact same result The server's result, slightly delayed
Sync Mode Off Replicated
Players joining late Send them the current state Automatic

Shots: everyone simulates the same action

The server checks the action, then tells everyone to apply it on the same step. Because the engine gives the same result everywhere, every machine ends in the same place.

For this to work, all of these must be the same on every machine:

Script 6.1 goes in a script both your client and server scenes can use, as explained in the Developer Guide. The server uses Script 6.2 inside its room, and the client uses Script 6.3.

Script 6.1: ShotMessages.cs

using MultiConnect.Serialization;

public enum ShotMessage { TakeShot = 100, ApplyShot = 101, StateCheck = 102, FullState = 103 }

[McSerializationObject]
public class TakeShot
{
    public float x, y, z;
    public float power;
}

[McSerializationObject]
public class ApplyShot
{
    public float forceX, forceY, forceZ;
    public int atStep;
}

[McSerializationObject]
public class StateCheck
{
    public int step;
    public ulong hash;
}

[McSerializationObject]
public class FullState
{
    public byte[] bytes;
}

Script 6.2: Server side of a shot

using MultiConnect;
using MultiConnect.Physics.Core;
using MultiConnect.Physics.Unity;
using MultiConnect.Server;
using UnityEngine;

public class ShotRoom : MonoBehaviour
{
    public MCPhysicsSpaceComponent space;
    public MCRigidBodyComponent cueBall;

    private Room room;
    private const float MaxForce = 40f;

    // A few steps ahead, so the message reaches every player in time.
    private const int DelaySteps = 6;

    public void Bind(Room room)
    {
        this.room = room;
        space.headlessMode = true;   // A server has nothing to draw.
        space.EnsureSpace();
        room.AddResponseListener(ShotMessage.TakeShot, OnTakeShot);
        room.AddResponseListener(ShotMessage.StateCheck, OnStateCheck);
    }

    void OnTakeShot(object message)
    {
        var shot = (TakeShot)message;

        // The server decides what is allowed.
        var force = new UnityEngine.Vector3(shot.x, shot.y, shot.z).normalized * (Mathf.Clamp01(shot.power) * MaxForce);
        int atStep = space.Space.StepCount + DelaySteps;

        cueBall.AddForceAtTickValue(force, atStep, MCForceMode.Impulse);
        room.SendMcBinaryToAll(new ApplyShot { forceX = force.x, forceY = force.y, forceZ = force.z, atStep = atStep }, ShotMessage.ApplyShot, McProtocol.Reliable);
    }

    void OnStateCheck(object message)
    {
        var check = (StateCheck)message;
        if (check.step != space.Space.StepCount || check.hash == space.Space.ComputeBodyStateHash()) return;

        // A player has drifted, so send them the true state.
        var bytes = new byte[space.Space.GetStateSizeBytes()];
        space.Space.WriteState(bytes);
        room.SendMcBinaryToAll(new FullState { bytes = bytes }, ShotMessage.FullState, McProtocol.Reliable);
    }
}

Script 6.3: Client side of a shot

using MultiConnect;
using MultiConnect.Client;
using MultiConnect.Physics.Core;
using MultiConnect.Physics.Unity;
using UnityEngine;

public class ShotClient : MonoBehaviour
{
    public MCPhysicsSpaceComponent space;
    public MCRigidBodyComponent cueBall;

    private Room room;

    public void Bind(Room room)
    {
        this.room = room;
        room.AddResponseListener(ShotMessage.ApplyShot, OnApplyShot);
        room.AddResponseListener(ShotMessage.FullState, OnFullState);
    }

    public void Shoot(UnityEngine.Vector3 aim, float power)
    {
        room.SendMcBinary(new TakeShot { x = aim.x, y = aim.y, z = aim.z, power = power }, ShotMessage.TakeShot, McProtocol.Reliable);
    }

    void OnApplyShot(object message)
    {
        var shot = (ApplyShot)message;
        cueBall.AddForceAtTickValue(new UnityEngine.Vector3(shot.forceX, shot.forceY, shot.forceZ), shot.atStep, MCForceMode.Impulse);
    }

    // Call this when the table has come to rest, to catch any drift early.
    public void SendCheck()
    {
        room.SendMcBinary(new StateCheck { step = space.Space.StepCount, hash = space.Space.ComputeBodyStateHash() }, ShotMessage.StateCheck, McProtocol.Reliable);
    }

    void OnFullState(object message) => space.ApplySnapshotFromBytes(((FullState)message).bytes);
}

Live world: the server sends the world

The server runs the only true simulation and sends it to players many times a second. Players cannot cheat by changing their copy, and anyone joining late simply receives the world.

Set Sync Mode to Replicated on the Physics Space. Snapshot Interval Ticks sets how often the world is sent: at the default of 3, with Fixed Hz at 60, that is 20 times a second.

Script 6.4 goes on the server, and Script 6.5 on the client. To make the player's own character feel instant, Script 6.5 keeps that one body under the client's control.

Script 6.4: Server side of a live world

using MultiConnect.Physics.Networking;
using MultiConnect.Physics.Unity;
using MultiConnect.Server;
using UnityEngine;

public class LiveWorldServer : MonoBehaviour
{
    public MCPhysicsSpaceComponent space;

    public void Bind(Room room)
    {
        space.headlessMode = true;
        space.EnsureSpace();
        space.Space.EnableReplication(new MCServerRoomPhysicsTransport(room));
        space.Space.StartReplication();
    }
}

Script 6.5: Client side of a live world

using MultiConnect.Client;
using MultiConnect.Physics.Networking;
using MultiConnect.Physics.Unity;
using UnityEngine;

public class LiveWorldClient : MonoBehaviour
{
    public MCPhysicsSpaceComponent space;
    public MCRigidBodyComponent myCharacter;

    public void Bind(Room room)
    {
        space.EnsureSpace();
        space.Space.EnableReplication(new MCClientRoomPhysicsTransport(room));
        space.Space.StartReplication();

        // This body follows this player's input, not the server's snapshots.
        space.Space.SetReplicationOwned(myCharacter.Body, true);
    }
}

To make other objects look smooth between snapshots, add MCBodyRenderInterpolator to each visible object.