A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(net): interest is a sphere, not a column
esenginee39d2b667527
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 093e88389..2c22b9dd0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -54,6 +54,12 @@ published separately; it ships inside the editor.### Fixed+- **Network interest is a sphere.** `radiusInterest` measured the distance to a+ connection's pawn in x and y, so two floors of a building were one place and+ every connection streamed both — the saving the policy exists to make is the one+ it stopped making. The reader hook's return type is now `InterestPoint`, whose+ `z` is optional, so a flat game's reader keeps answering in two.+- **Spatial audio and camera follow are three-dimensional.** Both measured theworld in x and y only, so in a 3D scene a source across the room was as loud asone in your ear, and a camera lost its target the moment it walked in depth.diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 568f42fd6..593c0ee3c 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -5991,7 +5991,7 @@ ReadonlySet<string>## RadiusInterestOptions — interface @experimental```-position: ((world: World, entity: Entity) => { x: number; y: number; } | null) | undefined+position: ((world: World, entity: Entity) => InterestPoint | null) | undefined```## ReadableWorld — type @experimentaldiff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 70211e99a..6373f2577 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -6184,7 +6184,7 @@ ReadonlySet<string>## RadiusInterestOptions — interface @experimental```-position: ((world: World, entity: Entity) => { x: number; y: number; } | null) | undefined+position: ((world: World, entity: Entity) => InterestPoint | null) | undefined```## ReadableWorld — type @experimentaldiff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 917e92eea..27e63ff90 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -5986,7 +5986,7 @@ ReadonlySet<string>## RadiusInterestOptions — interface @experimental```-position: ((world: World, entity: Entity) => { x: number; y: number; } | null) | undefined+position: ((world: World, entity: Entity) => InterestPoint | null) | undefined```## ReadableWorld — type @experimentaldiff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 2b4a54253..eba6925c8 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -6231,7 +6231,7 @@ ReadonlySet<string>## RadiusInterestOptions — interface @experimental```-position: ((world: World, entity: Entity) => { x: number; y: number; } | null) | undefined+position: ((world: World, entity: Entity) => InterestPoint | null) | undefined```## ReadableWorld — type @experimentaldiff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex 9f1dc439f..517e98633 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -5993,7 +5993,7 @@ ReadonlySet<string>## RadiusInterestOptions — interface @experimental```-position: ((world: World, entity: Entity) => { x: number; y: number; } | null) | undefined+position: ((world: World, entity: Entity) => InterestPoint | null) | undefined```## ReadableWorld — type @experimentaldiff --git a/sdk/src/net/replication/interest.ts b/sdk/src/net/replication/interest.tsindex 18dd39c9f..254ed2dd1 100644--- a/sdk/src/net/replication/interest.ts+++ b/sdk/src/net/replication/interest.ts@@ -34,14 +34,21 @@ export interface InterestView {*/export type InterestPolicy = (view: InterestView) => ReadonlySet<Entity> | 'all';+/** Where an entity is, for the distance a policy measures. `z` is optional so a+ * flat game's reader can keep answering in two. */+export interface InterestPoint {+ x: number;+ y: number;+ z?: number;+}+export interface RadiusInterestOptions {/**- * How to read an entity's 2D position. Defaults to the `Transform`- * component's world-space `position`. Return null for "this entity has no- * place" — such entities are always relevant (they can't be culled by- * distance).+ * How to read an entity's position. Defaults to the `Transform` component's+ * world-space `position`. Return null for "this entity has no place" — such+ * entities are always relevant (they can't be culled by distance).*/- position?: (world: World, entity: Entity) => { x: number; y: number } | null;+ position?: (world: World, entity: Entity) => InterestPoint | null;}/**@@ -54,7 +61,7 @@ export function radiusInterest(radius: number, options: RadiusInterestOptions =const r2 = radius * radius;const positionOf = options.position ?? defaultPosition;return ({ connectionId, world, candidates }) => {- const anchors: { x: number; y: number }[] = [];+ const anchors: InterestPoint[] = [];for (const e of candidates) {const repl = world.tryGet(e, Replicated) as ReplicatedData | null;if (repl?.owner !== connectionId) continue;@@ -71,9 +78,12 @@ export function radiusInterest(radius: number, options: RadiusInterestOptions =continue;}for (const a of anchors) {+ // A sphere, not a column: two floors of a building are one place to+ // a radius that drops z, and every connection then streams both.const dx = p.x - a.x;const dy = p.y - a.y;- if (dx * dx + dy * dy <= r2) {+ const dz = (p.z ?? 0) - (a.z ?? 0);+ if (dx * dx + dy * dy + dz * dz <= r2) {visible.add(e);break;}@@ -83,9 +93,9 @@ export function radiusInterest(radius: number, options: RadiusInterestOptions =};}-function defaultPosition(world: World, entity: Entity): { x: number; y: number } | null {+function defaultPosition(world: World, entity: Entity): InterestPoint | null {const Transform = getComponent('Transform');if (!Transform || !world.has(entity, Transform)) return null;- const t = world.tryGet(entity, Transform) as { position?: { x: number; y: number } } | null;- return t?.position ? { x: t.position.x, y: t.position.y } : null;+ const t = world.tryGet(entity, Transform) as { position?: { x: number; y: number; z?: number } } | null;+ return t?.position ? { x: t.position.x, y: t.position.y, z: t.position.z ?? 0 } : null;}diff --git a/sdk/tests/replication-interest.test.ts b/sdk/tests/replication-interest.test.tsindex 0b4403ab1..939886e09 100644--- a/sdk/tests/replication-interest.test.ts+++ b/sdk/tests/replication-interest.test.ts@@ -20,12 +20,12 @@ import type { World } from '../src/ecs/world';const STEP = 1 / 60;-let NetPos: ReturnType<typeof defineComponent<{ x: number; y: number }>>;+let NetPos: ReturnType<typeof defineComponent<{ x: number; y: number; z: number }>>;beforeEach(() => {clearUserComponents();- NetPos = defineComponent('NetPos', { x: 0, y: 0 }, {- replicatedFields: ['x', 'y'],+ NetPos = defineComponent('NetPos', { x: 0, y: 0, z: 0 }, {+ replicatedFields: ['x', 'y', 'z'],});});@@ -35,9 +35,9 @@ function makeApp(): App {return app;}-const posOf = (world: World, e: Entity): { x: number; y: number } | null => {- const p = world.tryGet(e, NetPos) as { x: number; y: number } | null;- return p ? { x: p.x, y: p.y } : null;+const posOf = (world: World, e: Entity): { x: number; y: number; z: number } | null => {+ const p = world.tryGet(e, NetPos) as { x: number; y: number; z: number } | null;+ return p ? { x: p.x, y: p.y, z: p.z } : null;};async function makePair(policy?: InterestPolicy) {@@ -51,10 +51,10 @@ async function makePair(policy?: InterestPolicy) {return { serverApp, clientApp, server, client, connId };}-function spawnAt(app: App, name: string, x: number, y: number, owner = 0): Entity {+function spawnAt(app: App, name: string, x: number, y: number, owner = 0, z = 0): Entity {const e = app.world.spawn(name);app.world.insert(e, Replicated, { owner });- app.world.insert(e, NetPos, { x, y });+ app.world.insert(e, NetPos, { x, y, z });return e;}@@ -162,6 +162,21 @@ describe('interest management (radius policy)', () => {expect(ghostNames(laterApp)).toEqual(['near', 'pawn']);});+ // Interest is a sphere, not a column. Two floors of a building sit at the same+ // x/y, and a radius that drops z sends every connection both of them — the one+ // saving it means is the one that stops being saved.+ it('culls by depth as it culls by width', async () => {+ const { serverApp, clientApp, server, connId } = await makePair();+ server.setInterestPolicy(radiusInterest(100, { position: posOf }));++ spawnAt(serverApp, 'pawn', 0, 0, connId);+ spawnAt(serverApp, 'sameFloor', 50, 0);+ spawnAt(serverApp, 'upstairs', 0, 0, 0, 500);+ await step(serverApp, clientApp);++ expect(ghostNames(clientApp)).toEqual(['pawn', 'sameFloor']);+ });+it('radius policy fails open while the connection owns no positioned entity', async () => {const { serverApp, clientApp, server } = await makePair();server.setInterestPolicy(radiusInterest(100, { position: posOf }));