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
feat(physics3d): a collider with no RigidBody3D is a static body
yhh8978e1ee9860
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex db0edd321..48085631c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,10 +25,12 @@ published separately; it ships inside the editor.- **The sky lights the scene.** Sky Atmosphere's **Light the Scene** makes the sky the scene's environment: every Ambient light that names no environment of its own lights with it, its diffuse light from the dome and the clouds and its reflection on metal and wet ground, scaled by the light's colour and intensity as an imported one is. The sky is worked out again as it changes (the sun moving, a weather coming in, clouds drifting), at most once a second; with clouds that takes about 20 ms on a desktop. The sun's disc is left out, since the directional light the sky follows is its direct light. Off by default, so a scene already lit by hand looks as it did.- **Rooms stay dark under a sky that changes (sky occlusion).** With **Sky occlusion** on in the Lighting settings (`BakedLighting.skyOcclusion`), a probe bake keeps how much of the sky each probe sees instead of the sky itself: the probes hold the lights and their bounces, and the sky the frame has now (a live Sky Atmosphere, a weather, an environment) is added through what each probe sees of it, to the diffuse light and to the sky's reflection. A ball inside a roofed room measured 119 where the baked sky gave it 150, and a ball outside kept its light. A `.esprobes` baked this way carries `skyVisibility`; one without it lights as before. Probes are read per object, as Unity's light probes are: one large mesh spanning indoors and out takes one value.+- **A 3D collider with no RigidBody3D is a static body, as in Unity.** An imported level now collides with a `MeshCollider3D` on each mesh and nothing else; before, a collider without a body built nothing and a character fell through the floor. A collider under an entity with a RigidBody3D is still left to that body, and the collider wireframe marks the same shapes the world builds. Removing an entity's RigidBody3D while its collider stays now leaves a static body rather than none.- **The 3D physics world is set in Project Settings, not in code.** **Physics → 3D Physics** sets what a scene unit is to the solver (**Units per metre**: 100, a centimetre, as before; 1 for a metre scene such as imported glTF), gravity in m/s², collision steps, velocity and position iterations, the bounce threshold, sleep and the body budget. Iterations and the bounce threshold left at 0 keep each solver's own (Jolt's, or Unity's for PhysX). The 3D world also takes the project's fixed step and collision-layer matrix, which only 2D did. A metre scene used to get gravity 100 times too strong, since everything was read as centimetres. Settings left at their defaults write nothing, so an existing project and its builds are unchanged. In `project.esproject` they are `features.physics.world3d`.### Fixed+- **The editor's MCP server starts its editor again after the editor goes away.** When a call found the editor unreachable before the editor's exit was seen, every later call in the session failed at once with "the editor host is not running" and no editor was started.- **A scene whose only 3D physics is a compound collider or a ragdoll gets a physics world.** The scene loader did not count `CompoundCollider3D` or `Ragdoll` as 3D physics, so a scene with nothing else ran with neither moving.- **Geometry with no lightmap UV set still stands in a bake's light.** It was left out of the bake altogether, so it cast no shadow on the baked surfaces and the probes saw straight through it. It now shadows them and hides the sky from the probes, while receiving no baked light itself (the import still says to generate lightmap UVs for that).- **An opaque surface no longer lets the sky through where its map's alpha is below 1.** An unblended draw wrote its base colour's alpha into the frame, and Sky Atmosphere reads a pixel's alpha as how much of it geometry covers, so wherever a texture used its alpha for something else (glTF's OPAQUE ignores it) the clouds showed through the wall. An opaque Model surface now writes 1, except a cut-out's coverage and the subsurface mark.diff --git a/desktop b/desktopindex 8424e1c32..8235baeb9 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 8424e1c322aad957f60249da46c00a3dd15ed0d1+Subproject commit 8235baeb992b87c58ee0fdced1d7a5ad1f06ce7cdiff --git a/sdk/src/physics3d/ColliderShape3D.ts b/sdk/src/physics3d/ColliderShape3D.tsindex 356209df5..8f7c27ae6 100644--- a/sdk/src/physics3d/ColliderShape3D.ts+++ b/sdk/src/physics3d/ColliderShape3D.ts@@ -19,7 +19,8 @@* Units: 3D collider fields are world units already (the system divides by* pixels-per-unit on its way into the solver), so nothing here scales.*/-import type { Vec3, Quat } from '../types';+import type { Entity, Vec3, Quat } from '../types';+import { isImplicitStatic } from './implicitStatic';import { q } from '../math/quat';import type { World } from '../ecs/world';import {@@ -97,7 +98,8 @@ export function readCollider3DShapes(world: World, entity: number): Collider3DInconst on = (c: { enabled?: boolean }): boolean => c.enabled !== false;const body = world.has(entity, RigidBody3D)? world.get(entity, RigidBody3D) as RigidBody3DData : null;- const bodyLive = body !== null && on(body);+ // With no RigidBody3D a collider is a static body of its own (implicitStatic.ts), as the world builds it.+ const bodyLive = body !== null ? on(body) : isImplicitStatic(world, entity as Entity);let taken = false;if (world.has(entity, CompoundCollider3D)) {const c = world.get(entity, CompoundCollider3D), scale = world.has(entity, Transform)diff --git a/sdk/src/physics3d/Physics3DSystem.ts b/sdk/src/physics3d/Physics3DSystem.tsindex eadc48941..3380def2d 100644--- a/sdk/src/physics3d/Physics3DSystem.ts+++ b/sdk/src/physics3d/Physics3DSystem.ts@@ -25,6 +25,7 @@ import {type ConvexCollider3DData,} from './Physics3DComponents';import { getMeshCollision } from '../asset/meshCollision';+import { isImplicitStatic } from './implicitStatic';import { CompoundCollider3D, encodeCompoundParts } from './CompoundCollider3D';import { syncJoints3D, type Joint3DMap } from './Physics3DJoints';import type { Entity } from '../types';@@ -376,6 +377,15 @@ function addConvexBody(module: Physics3DWasmModule, entity: Entity,}}+/** The colliders that make a static body on their own, with no RigidBody3D. */+const IMPLICIT_STATIC_COLLIDERS = [MeshCollider3D, BoxCollider3D, SphereCollider3D, CapsuleCollider3D, ConvexCollider3D, CompoundCollider3D];+/** Per body map: which of its bodies are static for having no RigidBody3D. */+const implicitBodies = new WeakMap<BodyMap, Set<Entity>>();+const IMPLICIT_STATIC: RigidBody3DData = {+ bodyType: 0, layer: 0, gravityScale: 0, linearDamping: 0, angularDamping: 0,+ fixedRotation: false, continuousCollision: false, enabled: true,+};+/** Bring the world's population in line with the ECS, then step and read back. */export function stepPhysics3D(app: App, module: Physics3DWasmModule,bodies: BodyMap, config: Physics3DConfig,@@ -395,10 +405,32 @@ export function stepPhysics3D(app: App, module: Physics3DWasmModule,authoredPoses.set(bodies, poses);app.world.ensureTransformsComposed();const live = new Set<Entity>();+ // A collider with no RigidBody3D is a static body, as in Unity: an imported level is collided against with a+ // MeshCollider3D on each mesh and nothing else. Its own `layer` (a mesh collider has one) is the body's.+ const bodied: Array<[Entity, RigidBody3DData]> = [];for (const entity of app.world.queryEntities([RigidBody3D])) {- const body = app.world.get(entity, RigidBody3D) as RigidBody3DData;- if (!body.enabled) continue;+ bodied.push([entity, app.world.get(entity, RigidBody3D) as RigidBody3DData]);+ }+ for (const collider of IMPLICIT_STATIC_COLLIDERS) {+ for (const entity of app.world.queryEntities([collider])) {+ if (!isImplicitStatic(app.world, entity)) continue;+ const layer = (app.world.get(entity, collider) as { layer?: number }).layer ?? 0;+ bodied.push([entity, { ...IMPLICIT_STATIC, layer }]);+ }+ }+ const implicit = implicitBodies.get(bodies) ?? new Set<Entity>();+ implicitBodies.set(bodies, implicit);+ for (const [entity, body] of bodied) {+ if (!body.enabled || live.has(entity)) continue;live.add(entity);+ // A body that changed kind (its RigidBody3D came or went, the collider stayed) is made again as the new kind.+ const isImplicit = !app.world.has(entity, RigidBody3D);+ if (bodies.has(entity) && isImplicit !== implicit.has(entity)) {+ module._physics3d_removeBody(bodies.get(entity)!);+ bodies.delete(entity);+ poses.delete(entity);+ }+ if (isImplicit) implicit.add(entity); else implicit.delete(entity);const created = !bodies.has(entity);if (created) {const id = createBody(app, module, entity, body, ppu);diff --git a/sdk/src/physics3d/implicitStatic.ts b/sdk/src/physics3d/implicitStatic.tsnew file mode 100644index 000000000..cb6d1546e--- /dev/null+++ b/sdk/src/physics3d/implicitStatic.ts@@ -0,0 +1,28 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file implicitStatic.ts+ * @brief Which entities are a static body with no RigidBody3D: one answer for the world that builds them and the+ * wireframe that draws them, so the two cannot disagree.+ */+import { Parent } from '../ecs/component';+import type { ParentData } from '../ecs/component.generated';+import type { World } from '../ecs/world';+import type { Entity } from '../types';+import { RigidBody3D, CharacterController3D } from './Physics3DComponents';++/**+ * A collider with no RigidBody3D is a static body, as in Unity: an imported level collides with a MeshCollider3D on+ * each mesh and nothing else. Not when a character controller owns the entity, and not under a RigidBody3D, whose+ * child colliders belong to that body.+ */+export function isImplicitStatic(world: World, entity: Entity): boolean {+ if (world.has(entity, RigidBody3D) || world.has(entity, CharacterController3D)) return false;+ let at = entity;+ for (let depth = 0; depth < 64 && world.has(at, Parent); depth++) {+ at = (world.get(at, Parent) as ParentData).entity;+ if (!world.valid(at)) return true;+ if (world.has(at, RigidBody3D)) return false;+ }+ return true;+}diff --git a/sdk/tests/physics3d-ecs.test.ts b/sdk/tests/physics3d-ecs.test.tsindex 33b097afe..f7c5b2452 100644--- a/sdk/tests/physics3d-ecs.test.ts+++ b/sdk/tests/physics3d-ecs.test.ts@@ -10,7 +10,7 @@* behaves.*/import { describe, it, expect, beforeEach, vi } from 'vitest';-import { Transform } from '../src/ecs/component';+import { Parent, Transform } from '../src/ecs/component';import type { TransformData } from '../src/ecs/component.generated';import {RigidBody3D, BoxCollider3D, SphereCollider3D, CapsuleCollider3D, CharacterController3D,@@ -294,12 +294,33 @@ describe('the 3D world and the ECS', () => {stepPhysics3D(app, module, bodies, DEFAULT_PHYSICS3D_CONFIG);expect(bodies.size).toBe(1);+ // The collider stays, so the entity is a static body now: the dynamic one goes and a static one is made.world.remove(e, RigidBody3D);+ module.calls.length = 0;+ stepPhysics3D(app, module, bodies, DEFAULT_PHYSICS3D_CONFIG);+ expect(called('removeBody')).toHaveLength(1);+ expect(called('addBox')).toHaveLength(1);+ expect(bodies.size).toBe(1);++ world.remove(e, BoxCollider3D);+ module.calls.length = 0;stepPhysics3D(app, module, bodies, DEFAULT_PHYSICS3D_CONFIG);expect(called('removeBody')).toHaveLength(1);expect(bodies.size).toBe(0);});+ it('builds a collider under a rigid body as nothing of its own', () => {+ const parent = spawn();+ world.insert(parent, BoxCollider3D);+ const child = world.spawn();+ world.insert<TransformData>(child, Transform, {});+ world.insert(child, Parent, { entity: parent });+ world.insert(child, SphereCollider3D);+ stepPhysics3D(app, module, bodies, DEFAULT_PHYSICS3D_CONFIG);+ expect(called('addSphere')).toHaveLength(0);+ expect(bodies.size).toBe(1);+ });+it('sweeps a character in metres and reads its ground state back', () => {const e = world.spawn();world.insert<TransformData>(e, Transform, {@@ -625,6 +646,7 @@ describe('the 3D world and the ECS', () => {stepJoints();module.calls.length = 0;world.remove(b, RigidBody3D);+ world.remove(b, BoxCollider3D);stepJoints();const removeJoint = module.calls.findIndex((c) => c.name === 'removeJoint');diff --git a/sdk/tests/physics3d-wireframe.test.ts b/sdk/tests/physics3d-wireframe.test.tsindex f576bb557..4e34434b3 100644--- a/sdk/tests/physics3d-wireframe.test.ts+++ b/sdk/tests/physics3d-wireframe.test.ts@@ -209,13 +209,14 @@ describe('what a 3D collider looks like', () => {releaseMeshCollision(555);});- it('reports no active shape without a rigid body, and the world builds none', () => {+ // Unity's rule, and how an imported level collides: a MeshCollider3D on each mesh and nothing else.+ it('reports a collider with no rigid body as a static body, and the world builds it', () => {const e = spawn(false);world.insert(e, BoxCollider3D);- expect(read(e)).toHaveLength(1); // still authorable, still drawn- expect(read(e)[0]!.active).toBe(false);- expect(built()).toBeNull();+ expect(read(e)).toHaveLength(1);+ expect(read(e)[0]!.active).toBe(true);+ expect(built()).toBe('addBox');});it('reports no active shape for a disabled body', () => {