Skip to content

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.

fix(physics): a layer the project never named keeps the collider's own mask

yhh80e4ab36ec78
diff --git a/sdk/src/physics/PhysicsSystem.ts b/sdk/src/physics/PhysicsSystem.ts
index 25f44bc54..8de545f9c 100644
--- a/sdk/src/physics/PhysicsSystem.ts
+++ b/sdk/src/physics/PhysicsSystem.ts
@@ -74,10 +74,16 @@ export function readPixelsPerUnit(app: App): number {
const MAX_COLLISION_LAYERS = 16;
+/**
+ * What a collider on `categoryBits` may touch: the project's layer table when it
+ * names that layer, the collider's own mask otherwise. A table is saved at the
+ * length a project has named, so reading past its end is a layer nobody set —
+ * not a layer that touches nothing, which is what `undefined` reaches wasm as.
+ */
function resolveCollisionMask(categoryBits: number, maskBits: number, layerMasks?: number[]): number {
if (!layerMasks) return maskBits;
for (let i = 0; i < MAX_COLLISION_LAYERS; i++) {
- if (categoryBits === (1 << i)) return layerMasks[i];
+ if (categoryBits === (1 << i)) return layerMasks[i] ?? maskBits;
}
return maskBits;
}
diff --git a/sdk/tests/collisionLayerMask.test.ts b/sdk/tests/collisionLayerMask.test.ts
new file mode 100644
index 000000000..8e80f2f8d
--- /dev/null
+++ b/sdk/tests/collisionLayerMask.test.ts
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+
+// The project's layer table decides what a collider may touch, and arrives from
+// settings at whatever length the project saved. What a collider on a layer past
+// its end gets is the contract here.
+import { describe, it, expect } from 'vitest';
+import { World } from '../src/ecs/world';
+import { createMockModule } from './mocks/wasm';
+import { BoxCollider } from '../src/physics/PhysicsComponents';
+import { addShapeForEntity } from '../src/physics/PhysicsSystem';
+import type { PhysicsWasmModule } from '../src/physics/PhysicsModuleLoader';
+
+function testWorld(): World {
+ const mod = createMockModule();
+ const world = new World();
+ world.connectCpp(mod.getRegistry(), mod);
+ return world;
+}
+
+/** Records the (category, mask) a box shape was attached with. */
+function maskRecorder(): { filters: Array<[number, number]>; module: PhysicsWasmModule } {
+ const filters: Array<[number, number]> = [];
+ const module = {
+ _malloc: () => 0,
+ _free: () => {},
+ HEAPF32: new Float32Array(64),
+ _physics_addBoxShape: (...args: number[]) => {
+ filters.push([args[10], args[11]]);
+ },
+ } as unknown as PhysicsWasmModule;
+ return { filters, module };
+}
+
+function boxOnLayer(layer: number, maskBits: number, layerMasks?: number[]) {
+ const world = testWorld();
+ const { filters, module } = maskRecorder();
+ const e = world.spawn();
+ world.insert(e, BoxCollider, { categoryBits: 1 << layer, maskBits } as never);
+ addShapeForEntity(world, module, e, layerMasks);
+ return filters[0];
+}
+
+describe('collision layer masks', () => {
+ it('takes the layer table over the collider when the table names the layer', () => {
+ expect(boxOnLayer(2, 0xFFFF, [0x0001, 0x0002, 0x0004, 0x0008])).toEqual([1 << 2, 0x0004]);
+ });
+
+ it('keeps the collider\'s own mask when no table is configured', () => {
+ expect(boxOnLayer(2, 0x00F0)).toEqual([1 << 2, 0x00F0]);
+ });
+
+ // A table of four says nothing about layer five, and reading past its end
+ // yields undefined — which reaches wasm as a filter matching nothing, so the
+ // collider silently stops colliding with no error anywhere.
+ it('keeps the collider\'s own mask for a layer the table does not reach', () => {
+ expect(boxOnLayer(5, 0x00F0, [0x0001, 0x0002, 0x0004, 0x0008])).toEqual([1 << 5, 0x00F0]);
+ });
+
+ it('keeps the collider\'s own mask when the table is empty', () => {
+ expect(boxOnLayer(0, 0x00F0, [])).toEqual([1 << 0, 0x00F0]);
+ });
+});