Only send what they can see: interest management for large game worlds
In a multiplayer game, every player needs to know what's going on around them: who is moving, who is fighting, which NPC just walked into the tavern. On a small map with a handful of players, the server can simply send everything to everyone. In a large world populated by hundreds of players and thousands of NPCs, this approach falls apart. In this article, I explore how to make sure each player only sees, and only receives data about, the objects in their own field of view.
The problem
Sending every update to every player means the traffic grows with the number of players multiplied by the number of entities. Double both and you send four times as much data. Worse, most of it is useless: a player in a forest has no use for the position of a merchant walking around a town ten kilometres away, nor for the details of a fight on the other side of the map.
The idea is to give each player an area of interest: a set of objects around their character that the server keeps up to date, and nothing else. When an object enters a player's area, the server sends a spawn message containing everything needed to display it. While the object stays in the area, the player receives its updates. When it leaves, the server sends a despawn message and the client forgets about it.
The gains are significant: less bandwidth, less work for the server and lighter clients. It also closes a whole family of cheats: the client can't reveal what it never received.
The constraints
Before choosing an algorithm, let's be clear on what we expect from it. The picture is simple: the player stands in the middle of the map and sees everything up to a certain view distance. As characters and NPCs move around, they come into sight and leave it again. Our job is to make sure the client knows about everything inside that circle, and as little as possible about the rest.
- out of sight
- visible on screen
From there, a good solution has to respect a few constraints.
- Bandwidth. Each client has a limited budget per server tick. The area of interest must stay manageable even when the whole server decides to meet in the same town square. When it gets too crowded, we need to cap what we send, or prioritise the closest objects.
- Server CPU. Comparing every character with every other one is O(n²). With 2,000 entities, that's 4 million distance checks per tick. We'll need a smarter way to find who is close to whom, which is what partitioning the world is for.
- No pop-in. An object must be spawned before it becomes visible, otherwise it appears out of nowhere in front of the player. The client needs a little time to receive and load it, so the server has to start sending it slightly before it reaches the view distance.
- No flickering. An NPC walking along the edge of the area shouldn't appear and disappear every few steps. Leaving the area should require going a bit further than entering it.
- Not everything has the same range. A dragon should be seen from much further away than a rabbit. Some objects must always be tracked, whatever the distance: party members, a quest target, the player's own mount.
- Fast movers. A horse, a projectile or a teleport can cross the whole area between two updates. The server still has to tell the player about it.
- Never leak. Anything sent to the client can be read by a cheater. The rule is simple: only send what the player is allowed to know.
These constraints shape everything that follows. Let's start with the most basic question: who is around the character?
Neighbours around the character
What is a neighbour?
A neighbour is any object (player, NPC, item on the ground...) the server considers close enough to a player to be worth sending. Each player has their own list of neighbours, and the whole job of interest management is to keep these lists up to date.
An object's life in that list comes down to three interactions:
- Spawn. The object becomes a neighbour. The server sends everything the client needs to create it: its type, position, appearance, equipment, current animation...
- Update. As long as it stays a neighbour, the server only sends what changes: a new position, a health drop, an emote.
- Despawn. The object is no longer a neighbour. The server tells the client to remove it, and stops sending anything about it.
To decide who is a neighbour, we define an area of interest around the player. In its simplest form, it's a single circle with a fixed radius, centred on the character. Anything inside is a neighbour, anything outside isn't. To avoid the pop-in we mentioned earlier, this radius is a bit larger than the view distance.
- not a neighbour
- neighbour, receiving updates
Does it have to be a circle?
Not at all. The circle is a natural choice for a 2D or a mostly flat world, but any shape works as long as one rule is respected: testing whether a point is inside must be cheap. This test runs for every candidate object, for every player, on every tick. A few common options:
- Circle: the distance on the ground plane, ignoring height. Perfect for top-down games and open fields.
- Sphere: the same check with the height added. Great for space games, flying or swimming characters.
- Cylinder: a circle on the ground plus a maximum height difference. Handy for cities with several floors, or dungeons under the map.
- Box: two or three comparisons, no multiplication at all. Even cheaper, at the cost of seeing further in the corners.
Circles and spheres are both just Pythagoras. And since we only compare distances, we don't even need the square root: comparing the squared distance with the squared radius gives the same answer.
// Offset between the player and the object
let (dx, dy, dz) = (object.x - player.x, object.y - player.y, object.z - player.z);
// Circle (2D): ignore the height
let in_circle = dx * dx + dz * dz <= radius * radius;
// Sphere (3D)
let in_sphere = dx * dx + dy * dy + dz * dz <= radius * radius;
// Cylinder: a circle on the ground plus a height limit
let in_cylinder = dx * dx + dz * dz <= radius * radius && dy.abs() <= height;
// Box: no multiplication at all
let in_box = dx.abs() <= half_width && dy.abs() <= half_height && dz.abs() <= half_depth;More complex shapes, like a cone following the camera or a volume clipped by walls, are possible. But the more expensive the test, the fewer objects we can afford to check.
The flickering problem
Our single circle has a weakness. Look at the highlighted NPC below: it walks along the edge of the area, stepping in and out of it. Every time it crosses the line, the server sends a spawn, then a despawn, then a spawn again. Each spawn carries the full state of the object, so this is the most expensive message we have, sent over and over for an NPC that barely moved.
- not a neighbour
- neighbour, receiving updates
- edge walker: 0 messages sent
On the client side, it's not better: the object keeps being created and destroyed, and may visibly blink if the view distance is close to the edge.
Two thresholds: the Schmitt trigger
Electronics solved this problem a long time ago with the Schmitt trigger. Instead of one threshold, it uses two: the output switches on when the input goes above the high threshold, and only switches off when it goes back below the low one. Between the two, the output keeps its previous state. A noisy signal hovering around a single value no longer makes the output oscillate.
Applied to our area of interest, this means two radii:
- a spawn radius: an object becomes a neighbour when it gets closer than this;
- a larger despawn radius: a neighbour only stops being one when it gets further than this.
Between the two circles, an object keeps its current state. Our edge walker now gets a single spawn, and stays a neighbour as long as it wanders around the edge.
- not a neighbour
- neighbour, receiving updates
- edge walker: 0 messages sent
The check barely changes: we just need to know whether the object was already a neighbour.
use glam::Vec3;
const SPAWN_RADIUS: f32 = 90.0;
const DESPAWN_RADIUS: f32 = 115.0;
fn is_neighbour(player: Vec3, object: Vec3, was_neighbour: bool) -> bool {
let radius = if was_neighbour { DESPAWN_RADIUS } else { SPAWN_RADIUS };
player.distance_squared(object) <= radius * radius
}The gap between the two radii is a trade-off. It must be larger than the distance an object can move back and forth between two checks, otherwise the flickering comes back. But every object in that ring is a neighbour we keep paying for, so making it too large wastes bandwidth.
Partitioning the world
We now know how to tell whether an object is a neighbour. The question is: which objects do we test? Let's picture a large world, 16 by 16 kilometres, with 5,000 connected players and 20,000 NPCs. And this population is anything but evenly spread:
- a trading hub where hundreds of players stand still, selling their loot;
- cities, busy all day long;
- events: a world boss shows up and pulls 300 players to the same spot within minutes;
- the countryside, where you can walk for a while without meeting anyone.
Testing every player against every entity means 5,000 × 25,000 = 125 million distance checks per tick. Almost all of them answer "no": the player in the countryside has nothing to do with the trading hub on the other side of the map. What we want is to only look at objects that could plausibly be close, without scanning the whole list of connected characters.
A uniform grid
The first idea is to cut the map into square cells, like a chessboard. Each cell keeps the list of the entities standing in it. When an entity moves, we compute its cell with floor(position / cell_size); if it changed, the entity is moved from one list to the other. That's a constant-time operation.
To find a player's neighbours, we no longer scan the whole world, only the cells overlapping the area of interest. With cells twice as large as the radius, that's at most 4 cells. With cells as large as the radius, 9 cells. Everything else is skipped without even being looked at.
- skipped
- checked, too far
- neighbour
- 66 distance checks instead of 115
Watch the counter as the player travels. In the countryside, a handful of checks is enough. But as soon as the player reaches the city, the cells around them are packed, and we're back to testing a crowd. A uniform grid has a hard time with variable densities:
- in the countryside, most cells are empty. They still cost memory and we still visit them for nothing;
- in cities and events, a single cell can hold hundreds of entities;
- smaller cells help in the city, but multiply the number of empty cells in the countryside. Larger cells do the opposite.
There's no cell size that fits every density.
A grid that adapts to density
So let's stop choosing. We keep a grid of large cells, but a cell that gets too crowded splits into four smaller ones. If those are still too crowded, they split again. When the crowd leaves, the children merge back into their parent. Each top-level cell becomes a small quadtree: large cells in the countryside, small cells where people gather.
The query works the same way: we only go down into the cells touching the area of interest. Testing whether a square touches a circle is cheap: clamp the circle's centre into the square to get its nearest point, then compare the squared distance with the squared radius. A whole branch of the tree can be skipped with a single test.
- skipped
- checked, too far
- neighbour
- 46 distance checks instead of 115
Splitting and merging come with their own flickering risk. A crowd hovering around the limit would make a cell split and merge on every tick. The answer is the one we already used for neighbours: two thresholds. For example, split above 32 entities, merge below 16.
Last trick: the top-level cells are stored in a hash map indexed by their coordinates, rather than in a big array. A cell only exists when someone stands in it, so the empty countryside costs nothing at all.
use std::collections::HashMap;
use glam::Vec2;
type EntityId = u32;
enum Cell {
Leaf(Vec<(EntityId, Vec2)>),
Split(Box<[Cell; 4]>),
}
struct World {
cell_size: f32,
// Only populated cells exist: the empty countryside costs nothing.
cells: HashMap<(i32, i32), Cell>,
}
impl World {
fn neighbours(&self, center: Vec2, radius: f32) -> Vec<EntityId> {
let mut found = Vec::new();
let min = ((center - radius) / self.cell_size).floor().as_ivec2();
let max = ((center + radius) / self.cell_size).floor().as_ivec2();
for x in min.x..=max.x {
for y in min.y..=max.y {
if let Some(cell) = self.cells.get(&(x, y)) {
let origin = Vec2::new(x as f32, y as f32) * self.cell_size;
cell.collect(origin, self.cell_size, center, radius, &mut found);
}
}
}
found
}
}
impl Cell {
fn collect(&self, origin: Vec2, size: f32, center: Vec2, radius: f32, found: &mut Vec<EntityId>) {
// Skip the whole cell if it doesn't touch the area of interest.
let nearest = center.clamp(origin, origin + size);
if nearest.distance_squared(center) > radius * radius {
return;
}
match self {
Cell::Leaf(entities) => found.extend(
entities
.iter()
.filter(|(_, position)| position.distance_squared(center) <= radius * radius)
.map(|(id, _)| *id),
),
Cell::Split(children) => {
let half = size / 2.0;
for (i, child) in children.iter().enumerate() {
let offset = Vec2::new((i % 2) as f32, (i / 2) as f32) * half;
child.collect(origin + offset, half, center, radius, found);
}
}
}
}
}Two limits are worth keeping in mind. First, a cell can't split forever: if 300 players stack on the exact same spot, splitting won't separate them, so we need a maximum depth or a minimum cell size. Second, partitioning only removes the objects that are far away. If 500 players stand inside your area of interest, they are 500 neighbours, whatever the data structure. That's no longer a lookup problem, it's the bandwidth constraint, and it calls for capping and prioritising what we send.
Going 3D
Everything above works on the ground plane, in 2D. When the game needs it, the same ideas extend to 3D: the grid becomes a field of cubes indexed by (x, y, z), and a crowded cube splits into eight children instead of four. The quadtree becomes an octree.
It's only worth it when entities really spread vertically: space games, flying mounts, deep dungeons, very tall cities. For most games, the world is mostly flat. Keeping a 2D grid and adding a height check on the few remaining candidates (our cylinder from earlier) is simpler and cheaper.
Handling high-velocity characters
So far, we've quietly assumed that objects move slowly compared to how often we check them. That's rarely true. Rebuilding every neighbour list on every frame would be far too expensive, so the server re-evaluates them at an interval, say every half second. Between two checks, an object travels speed × interval:
| Object | Speed | Distance between two checks |
|---|---|---|
| Walking NPC | 1.5 m/s | 0.75 m |
| Horse | 20 m/s | 10 m |
| Dragon | 60 m/s | 30 m |
| Arrow | 80 m/s | 40 m |
For a walker, the check is basically continuous. For a dragon, the server only sees a series of jumps. This leads to two problems:
- Pop-in. The object jumps over the margin between the spawn radius and the view distance, and appears straight in front of the player.
- Tunnelling. The object crosses the whole area between two checks. It's never spawned, the player never sees it: an arrow hits them out of nowhere, a dragon flies by unnoticed.
Below, a fast rider crosses the map. The server only looks at it on each check (the small circles). Watch the counter: some crossings go completely unnoticed.
- server check
- unknown to the client
- known
- 0 of 0 crossings detected
A margin that follows the speed
The first fix is about pop-in. The gap between the spawn radius and the view distance must be at least as large as the distance an object can travel between two checks. A walker needs a margin of a metre, a dragon needs 30.
Rather than sizing everything for the fastest object, the spawn radius can depend on the object type, which joins one of our constraints: not everything has the same range. A dragon gets a large spawn radius, a rabbit a small one.
Sweeping the path
A larger margin doesn't solve tunnelling, though: if the arrow is fast enough, it will always jump over the area. Instead of testing only the current position, we test the path travelled since the last check: the segment between the previous position and the current one. If the closest point of that segment to the player is inside the area, the object went through it, even if both ends are outside.
The server can then decide what to do with the information: spawn the object late, play its effects, or simply send the event that matters ("you got hit by an arrow coming from there"). Notice the shape we're actually testing. Seen from the object's side, it's no longer a point but a capsule: its path, inflated by the radius. The faster it goes, the longer the capsule. On the partitioning side, a fast object is therefore looked up with the bounding box of that capsule, an oblong rectangle stretched along its movement, and it touches more cells than a slow one.
- server check
- unknown to the client
- known
- 0 of 0 crossings detected
Check fast objects more often
Another option is to stop using the same interval for everyone. Instead of a fixed time, an object is re-evaluated after travelling a given distance, for example a quarter of the margin. A merchant standing behind their stall is almost never re-checked, a horse at full gallop is checked several times per second. The CPU is spent where things actually change.
Projectiles are events, not entities
Arrows, bullets and spells are the fastest objects in the game, and also the shortest-lived. Streaming them like characters (spawn, a few updates, despawn) is a waste. A projectile is better sent as a single event: "an arrow was fired from here, in this direction, at this speed, at this time". The server sends it to every player whose area the trajectory crosses, using the same swept test on the whole path, and each client simulates the flight on its own. Only the impact needs to be confirmed.
When the player is the fast one
Speed also applies to the player. On a mount or a flying creature, a centred circle stops making sense. What matters is what's coming: the player will be there in a second. What's behind is moving away and will soon be out of reach anyway.
So the area of interest can change shape with speed, as we saw earlier: any shape works as long as the test stays cheap. At full gallop, the circle stretches forward into an oval, and the player is no longer at its centre: they slide towards the back, with most of the area ahead of them. Standing still, it's back to a regular circle.
- not a neighbour
- neighbour
- area when standing still
- speed: 19
A capsule works as well: our crossed_area function, called with the player's current position and their predicted position in a second or two, does exactly that. Whatever the shape, two rules stay the same: the despawn shape must be larger than the spawn one, so the Schmitt trigger keeps working, and the shape must not change too abruptly, otherwise every turn of the mount despawns half of the neighbours.
Even stretched, the area keeps moving, entering new cells all the time and producing bursts of spawns: every object of a town arrives at once as the player rides through the gate. Teleports and fast travel are the extreme case: the whole neighbour list is replaced in one go.
Here again, the bandwidth constraint drives the answer. The new neighbours are sent closest first, spread over several ticks, while a fade or a loading screen hides the arrival. The player sees what's in front of them right away, and the far details fill in during the next second.

