Server code
Lag compensation
A player sees the others a little in the past: their moves reach it half a ping late, and the SDK draws them a tenth of a second behind so they move smoothly. When the player shoots, the shot reaches the room another half a ping later. By then the target has moved on. Without help, a player has to aim ahead of what it sees.
Lag compensation turns the room back to the moment the player saw. The room keeps the hit shapes of the last second, and a shot is checked against where they were then.
Record the shapes
Every tick, the code records the shapes of every body that can be hit:
ansekai.tick(function(ctx, seconds)
for id, player in players do
local p = player.position
ansekai.history.record(id, {
head = { sphere = p + vector.create(0, 1.7, 0), radius = 0.25 },
body = { capsule = { p, p + vector.create(0, 1.4, 0) }, radius = 0.4 },
})
end
end)A part is a sphere, a capsule between two points, or a box that stands straight:
| Part | Fields |
|---|---|
| Sphere | sphere is the center, radius |
| Capsule | capsule is its two ends, radius |
| Box | box is the center, size is the full size |
The room keeps one frame for every tick of the last second, 129 frames at 128 Hz. A body stays in the next frames until it is recorded again; ansekai.history.forget(id) takes it out, like a player who died or left. A frame holds 128 bodies with 8 parts each, a room has 32 different part names at most.
Check a shot
The SDK gives the moment the player sees with room.renderTime(). The shot goes to the room with it:
const room = await ansekai.rooms.join(roomId);
function shoot(origin: Vec3, direction: Vec3) {
room.send({ kind: "shoot", at: room.renderTime(), origin, direction });
}The room looks back to that moment:
ansekai.on("message", function(ctx, player, message)
if message.kind == "shoot" then
local origin = vector.create(message.origin.x, message.origin.y, message.origin.z)
local direction = vector.create(message.direction.x, message.direction.y, message.direction.z)
local hit = ansekai.history.raycast(message.at, origin, direction, 200, { player = player })
if hit then
local damage = hit.part == "head" and 100 or 30
ansekai.room.broadcast({ hit = hit.id, by = player, damage = damage })
end
end
end)| Call | Gives |
|---|---|
history.raycast(at, origin, direction, distance, options) | The nearest part on the ray: id, part, point, normal, distance and at, the moment it really looked at. nil when nothing is on the ray |
history.overlap(at, center, radius, options) | Every part a sphere touches, a list of id and part, for melee and explosions |
history.at(at, id) | Where the parts of one body were. Without an id, every body by its id |
history.record(id, parts), history.forget(id) | Write the frame of this tick |
history.oldest(), history.newest() | The times of the first and the last frame |
Between two frames the shapes are blended, so a shot between two ticks sees the target in between.
A player can't look too far back
A cheater could send a moment from a second ago and hit a player who already ran behind a wall. With player in the options, the room holds the moment to what that player can really see:
| Option | Meaning |
|---|---|
player | The shooter: its body is skipped, and the moment goes back at most its ping, the delay and 50 ms more |
delay | How far behind the SDK draws others, 0.1 seconds unless renderTime was given another one |
maxRewind | The furthest back a moment may go in any case, 0.5 seconds unless set, a second at most |
ignore | An id or a list of ids to skip, like a team mate |
The room measures the ping itself: it pings every player once a second and the SDK answers at once, so a player can't claim a bigger ping than it has. ansekai.room.ping(player) gives it to the code too.
Draw others smoothly
The room sends snapshots, the SDK keeps them on a Timeline and draws them at renderTime(), the same moment a shot is checked at:
import { Timeline, lerp } from "@ansekai/web";
const enemies = new Timeline<{ x: number; y: number; z: number }>();
room.on("message", (message) => {
if (message.kind === "snapshot") {
enemies.push(message.time, message.enemy);
}
});
function draw() {
const moment = enemies.at(room.renderTime());
if (moment) {
enemy.position.set(
lerp(moment.before.x, moment.after.x, moment.amount),
lerp(moment.before.y, moment.after.y, moment.amount),
lerp(moment.before.z, moment.after.z, moment.amount),
);
}
requestAnimationFrame(draw);
}The room puts ansekai.room.time() into its snapshots as time. room.serverTime() gives the clock of the room on the player, and room.ping the last ping in seconds.
Updated November 21, 2026