Interpolator

Available since @irtio/client 5.0BrowsersNode

The Interpolator interface of the Client API records numeric, angle and quat fields of every entity of one kind as state updates arrive, and returns them between those updates for drawing. RoomClient.interpolate() returns one.

Instance properties

All are read only.

Interpolator.kind and Interpolator.fields
The kind and the fields it draws.

Instance methods

Interpolator.get()
Returns an entity’s fields at the client’s render time.

Description

A room sends state updates at its tick rate, which is usually below the screen’s frame rate, and an entity drawn from them moves in steps. An interpolator draws remote entities a little in the past, at the client’s render time, between the two states around it. Use it for other players, and Prediction for the local player.

The render time is the room’s estimated tick, from RoomClient.serverTick(), less two ticks or 100 ms, whichever is longer. Each input the client sends carries the render time of the last frame drawn, which the room reads as connection.viewTick for lag compensation.

kind is the entity kind passed to interpolate(), and fields is the array of field names passed with it, the ones get() returns.

Examples

Drawing the other players

A party game interpolates every player’s position and heading, and draws each frame.

// party.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Guest = entity("guest", { owner: "string", x: "f32", y: "f32", facing: "angle" });
const schema = defineSchema({ entities: [Guest] });

const room = await connect("ballroom", { schema });
const guests = room.interpolate(Guest, ["x", "y", "facing"]);

function frame() {
  for (const g of room.entities.all(Guest)) {
    const { x, y, facing } = guests.get(g);
    console.log(g.owner, x, y, facing);
  }
  requestAnimationFrame(frame);
}
frame();

See also

Runtime availability

BrowsersNode
Interpolator5.05.0