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.

perf(physics): reuse static parent transform snapshots

yhhacdc5ded8cc9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62a0358cb..6724f7796 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Static siblings reuse their parent transform snapshot.** Unchanged static bodies sharing a parent avoid repeated child-world-pose reads while dynamic bodies keep composition active. Parent movement and direct child edits still update their poses in place, and cached parent snapshots are removed when no static body references them.
+
- **Transformless hierarchy groups preserve child world poses.** A grouping entity without its own Transform no longer stops composition of its descendants. Root groups start from identity, intermediate groups carry the ancestor matrix through, and dirty propagation and traversal guards continue through those nodes.
- **Tilemap activity preserves painted data and owns generated collision.** Disabling a layer or its ancestor retains its chunks and derived layers. Runtime colliders inherit the map's movement and activity, remain outside saved scenes, and resume with their existing identities; inactive tile animations stop advancing.
diff --git a/sdk/src/physics/PhysicsSystem.ts b/sdk/src/physics/PhysicsSystem.ts
index 2772468fd..045ff7d70 100644
--- a/sdk/src/physics/PhysicsSystem.ts
+++ b/sdk/src/physics/PhysicsSystem.ts
@@ -699,7 +699,9 @@ export function registerPhysics2DSystem(
const physicsComponents = [RigidBody2D, ...COLLIDER_TYPES, ...JOINT_TYPES, OneWayPlatform2D];
for (const c of physicsComponents) app.world.enableChangeTracking(c);
const kinematicEntities = new Set<Entity>();
- const staticPoses = new Map<Entity, { x: number; y: number; angle: number; parented: boolean }>();
+ const staticPoses = new Map<Entity, { x: number; y: number; angle: number; parent: Entity | null }>();
+ const staticParentPoses = new Map<Entity, { values: number[]; checked: number; changed: boolean }>();
+ let staticSyncSequence = 0;
let lastStaticEpoch = -1;
let lastStaticTick = -1;
let lastStaticPpu = -1;
@@ -807,7 +809,7 @@ export function registerPhysics2DSystem(
if (rb.bodyType === BodyType.Kinematic) kinematicEntities.add(entity);
if (rb.bodyType === BodyType.Static) {
staticPoses.set(entity, { x: posX * invPpu, y: posY * invPpu,
- angle: quatToAngleZ(rot), parented: hasParent });
+ angle: quatToAngleZ(rot), parent: hasParent ? world.get(entity, Parent).entity : null });
}
continue;
}
@@ -857,10 +859,10 @@ export function registerPhysics2DSystem(
if (cached.bodyType === BodyType.Kinematic) kinematicEntities.add(entity);
else kinematicEntities.delete(entity);
if (cached.bodyType === BodyType.Static) {
- const parented = world.has(entity, Parent);
+ const parent = world.has(entity, Parent) ? world.get(entity, Parent).entity : null;
const pose = staticPoses.get(entity);
- if (pose) pose.parented = parented;
- else staticPoses.set(entity, { x: NaN, y: NaN, angle: NaN, parented });
+ if (pose) pose.parent = parent;
+ else staticPoses.set(entity, { x: NaN, y: NaN, angle: NaN, parent });
parentedBodies.delete(entity);
} else staticPoses.delete(entity);
}
@@ -902,17 +904,48 @@ export function registerPhysics2DSystem(
lastEntitySyncTick = world.getWorldTick();
lastStructuralVersion = structuralVersion;
+ const staticParents = new Set<Entity>();
+ for (const pose of staticPoses.values()) if (pose.parent !== null) staticParents.add(pose.parent);
+ for (const parent of staticParentPoses.keys()) {
+ if (!staticParents.has(parent)) staticParentPoses.delete(parent);
+ }
} // end if (needReconcile)
const staticEpoch = world.transformEpoch();
if (staticPoses.size > 0 && (needReconcile || staticEpoch < 0
|| staticEpoch !== lastStaticEpoch || ppu !== lastStaticPpu)) {
world.ensureTransformsComposed();
+ const sequence = ++staticSyncSequence;
for (const [entity, pose] of staticPoses) {
if (!cachedProps.get(entity)?.enabled) continue;
- const hasParent = pose.parented;
- if (!hasParent && !needReconcile && ppu === lastStaticPpu
- && !world.isChangedSince(entity, Transform, lastStaticTick)) continue;
+ const hasParent = pose.parent !== null;
+ if (!needReconcile && ppu === lastStaticPpu
+ && !world.isChangedSince(entity, Transform, lastStaticTick)) {
+ if (pose.parent === null) continue;
+ let parentPose = staticParentPoses.get(pose.parent);
+ if (parentPose?.checked !== sequence) {
+ if (!world.has(pose.parent, Transform)) {
+ if (parentPose) {
+ parentPose.checked = sequence; parentPose.changed = true;
+ } else {
+ parentPose = { values: [], checked: sequence, changed: true };
+ staticParentPoses.set(pose.parent, parentPose);
+ }
+ } else {
+ const tf = world.get(pose.parent, Transform) as TransformData;
+ const p = tf.worldPosition, r = tf.worldRotation, s = tf.worldScale;
+ const values = [p.x, p.y, p.z, r.x, r.y, r.z, r.w, s.x, s.y, s.z];
+ const changed = !parentPose || values.some((value, index) => value !== parentPose!.values[index]);
+ if (parentPose) {
+ parentPose.values = values; parentPose.checked = sequence; parentPose.changed = changed;
+ } else {
+ parentPose = { values, checked: sequence, changed };
+ staticParentPoses.set(pose.parent, parentPose);
+ }
+ }
+ }
+ if (!parentPose.changed) continue;
+ }
if (!world.isEntityActive(entity)) continue;
const tf = world.get(entity, Transform) as TransformData;
const position = hasParent ? tf.worldPosition : tf.position;
diff --git a/sdk/tests/physics-static-transform-wasm.test.ts b/sdk/tests/physics-static-transform-wasm.test.ts
index 9f13f1322..d27d8b668 100644
--- a/sdk/tests/physics-static-transform-wasm.test.ts
+++ b/sdk/tests/physics-static-transform-wasm.test.ts
@@ -43,6 +43,10 @@ describe.skipIf(!HAS_WASM || !hasSideModule('physics'))('static Box2D transform
await app.stepFrames(20, 1 / 30);
expect(move).not.toHaveBeenCalled();
expect(module._physics_hasBody(body)).toBe(1);
+ world.update(body, Transform, value => { value.position.x += 20; });
+ await app.stepFrames(2, 1 / 30);
+ expect(hit().point.x).toBeCloseTo(before + 50, 3);
+ expect(move.mock.calls).toHaveLength(1);
});
it('keeps batched dynamic writeback when static bodies have parents', async () => {
const app = App.new(); apps.push(app);
@@ -62,4 +66,47 @@ describe.skipIf(!HAS_WASM || !hasSideModule('physics'))('static Box2D transform
await app.stepFrames(2, 1 / 30);
expect(batch).toHaveBeenCalled();
});
+ it('skips unchanged child pose reads while a dynamic body keeps composition active', async () => {
+ const app = App.new(); apps.push(app);
+ app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ const module = await loadSideModule<PhysicsWasmModule>('physics');
+ app.addPlugin(new Physics2DPlugin('', { gravity: { x: 0, y: 0 } }, async () => module));
+ await app.getResource(Physics2DRuntime).initPromise;
+ const world = app.world, parent = world.spawn(), bodies = new Set<number>();
+ world.insert(parent, Transform);
+ for (let i = 0; i < 10; i++) {
+ const body = world.spawn(); bodies.add(body);
+ world.insert(body, Transform, { position: { x: i * 30, y: 0, z: 0 } });
+ world.insert(body, RigidBody2D, { bodyType: BodyType.Static });
+ world.insert(body, BoxCollider2D, {}); world.setParent(body, parent);
+ }
+ const dynamic = world.spawn();
+ world.insert(dynamic, Transform, { position: { x: 1000, y: 1000, z: 0 } });
+ world.insert(dynamic, RigidBody2D, { bodyType: BodyType.Dynamic });
+ world.insert(dynamic, BoxCollider2D, {});
+ await app.stepFrames(3, 1 / 30);
+ module._physics_setLinearVelocity(dynamic, 1, 0);
+ const get = vi.spyOn(world, 'get');
+ await app.stepFrames(20, 1 / 30);
+ expect(get.mock.calls.filter(([entity, component]) => component === Transform && bodies.has(entity)))
+ .toHaveLength(0);
+ });
+ it('keeps moving static children when their grouping parent has no Transform', async () => {
+ const app = App.new(); apps.push(app);
+ app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ const module = await loadSideModule<PhysicsWasmModule>('physics');
+ app.addPlugin(new Physics2DPlugin('', { gravity: { x: 0, y: 0 } }, async () => module));
+ await app.getResource(Physics2DRuntime).initPromise;
+ const parent = app.world.spawn(), body = app.world.spawn();
+ app.world.insert(body, Transform, { position: { x: 5, y: 0, z: 0 } });
+ app.world.insert(body, RigidBody2D, { bodyType: BodyType.Static });
+ app.world.insert(body, BoxCollider2D, { halfExtents: { x: .1, y: .1 } });
+ app.world.setParent(body, parent);
+ await app.stepFrames(3, 1 / 30);
+ app.world.update(body, Transform, value => { value.position.x += 30; });
+ await app.stepFrames(2, 1 / 30);
+ const hit = app.getResource(Physics2D).raycast({ x: -50, y: 0 }, { x: 1, y: 0 }, 200)
+ .find(value => value.entity === body);
+ expect(hit?.point.x).toBeCloseTo(25, 3);
+ });
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index b0e3e1849..2ce67a0f2 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'perf(physics): reuse static parent transform snapshots':
+ { note: 'Static siblings reuse their parent transform snapshot.' },
'fix(ecs): compose transforms through transformless groups':
{ note: 'Transformless hierarchy groups preserve child world poses.' },
'fix(tilemap): preserve inactive layers and own runtime colliders':