[Open Source] Nerp - smooth 20hz server updates on a 60hz client

I just finished working on Nerp, a simple module that interpolates server driven transforms on the client.

If you move a part on the server, client get its it at about 20 times a second but draws 60+, so it steps, the usual fix is to set the CFrame on the client and do the smoothing manually, but the usual result is something that mostly works until the connection gets jittery.

Nerp does the smoothing part so you can keep the networking.

Nerp example

How it works

The server stamps a CFrame with GetServerTimeNow() and the client buffers it, then renders it on a fixed delay behind the newest one, so it always has a pair to interpolate between instead of guessing.

local store = Nerp.new(20. "SMOOTH")
store:track(workspace.Crate)
store:start()

remote.OnClientEvent:Connect(function(t, cf)
  store:feed(workspace.Crate:GetFullName(), { { t = t, cf = cf } })
end)

That is the whole API, track registers a part or a model, feed is your remote handler, start binds one PreRender conn.

The delay is two samples, and not 100ms

"SMOOTH" is two sample intervals, "LOW_LATENCY" is 1.5/2, so at 20hz that is 100ms and 75ms

Two intervals leaves one whole sample of slack, so a single late packet still has something to interpolate towards, at one interval the clock sits on the newest sample and any jitter pushes it past the end, which is where extrapolation starts so LOW_LATENCY is built for extrapolation, so it is for close combat and not vehicles.

The delay is a multiple so throttling on the server rate keeps the same speed instead of silently halving it.

What it doesn’t do

There’s no lag comp, bandwidth work, camera, prediction, replication, Roblox already interpolates player characters, so Nerp leaves them alone by default.

If you want the whole replication path handled, I’d suggest something like Chrono, it does it well. Nerp is the package you use when you have your own remote and just want the motion to look right.

Install

Wally:

[dependencies]
Nerp = "superdupernairobi/nerp@0.2.0"

(Information on release 0.2.0 is below)

Studio: download Nerp.rbxm from the release, and right click ReplicatedStorage and pick Insert > Import Roblox Model

About the clip

Two anchored parts with the same motion, the red one is moved by the servers own replication at 20hz, the green one is streamed on a UnreliableRemoteEvent at 20hz with a timestamp, and then interpolated on the client.

Both are anchored on purpose, an unanchored part is owned by physics on the client so a CFrame write gets removed.

Source is at dev/server/main.server.luau and dev/client/main.client.luau on the repo, neither of them come with the package.

Links

10 Likes

0.2.0 is out

Three things that are new.

Extrapolation. Off by default, but if a packet drops, the part used to freeze until the next one arrived. Now you can let it carry forward for a bounded amount of time.

local store = Nerp.new(20, "LOW_LATENCY", { extrapolate = 0.08 })

80ms here, 100ms max. Past that it holds, because a guess that snaps back is terrible compared to a freeze.

History for lag comp. Nerp.history() is a server sided ring of what you actually sent. Rewind to a time and you get the transforms back:

local h = Nerp.history()
h:record(player.Name, now, root.CFrame)

for _, entry in h:rewind(now - 0.12) do
  -- raycast against entry.cf
end

It’s the servers own record, so nothing in a lag comp decision depends on a number the client sent.

Better Interpolation. The loop now blends the sample before the pair. One late packet bends the path instead of being reproduced 1:1. Costs one extra Lerp per target.

Upgrading from 0.1.0 needs no code changes. Nerp.new just takes a third optional argument.

One thing for anyone who already tried extrapolation: it moved the part hundreds of studs/s during a gap in the first build. math.max(negative, 1e-4) returns to the floor, which turned a very small number into -800. Fixed in this release.

Docs: Nerp
Wally: Wally