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(ai): hearing, world-pose perception, nav bakes implicit statics; rays can meet characters
yhhd9fbd7e9c79a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bf3c4e1d8..ccee5142e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,11 +25,15 @@ 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.+- **Perceivers hear.** `Perceiver.hearingRange` and a `Noise` event (where, how loud, who made it): a perceiver hears a noise within `hearingRange * loudness`, through walls, as Unreal's hearing sense does, and `Perception` keeps `heard`, where the last noise came from and `sinceHeard`. Saltline's machines come to a gunshot within 45 m.+- **A ray can meet characters.** `Physics3D.raycast(origin, direction, layers, { characters: true })` stops on a `CharacterController3D`'s capsule where it is now, on Jolt and on PhysX alike; a plain ray still passes them, which line of sight and a camera's boom rely on. Before, nothing could shoot a character that walks.- **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+- **Navigation bakes an imported level.** The bake took only bodies with a static RigidBody3D, so a level of mesh colliders (static by themselves) baked to no ground; and it placed every collider at its LOCAL pose and unscaled, so one under a parent landed elsewhere. It now takes what the solver builds, where the solver builds it.+- **Perception senses from where things are in the world.** It read each entity's own position, so a perceiver on a head bone or a target on a player's eye camera sensed from near the origin; sight measured feet to feet was also cut by every kerb.- **A first-person view model no longer hides the world in a linear or tone-mapped project.** A camera that keeps the colour under it (clears depth only, as the held-gun camera of `FirstPersonViewModel` does) still draws through a capture of its own when the frame is linear or has an output transform; that capture started black and was copied over the camera beneath, so the world went black around the gun. It is now laid over by coverage. Gate: overlay-camera-linear.- **Post-process volumes skip a camera that draws over another one.** Such a camera was given the scene's global stack too, which ran fog, sky and the look over the whole finished picture again with its own cleared depth — the world under a held gun went to fog. Local volumes now also weigh a camera by where it is in the world, not by its position under its parent.- **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.diff --git a/sdk/src/ai/nav/navGeometry.ts b/sdk/src/ai/nav/navGeometry.tsindex 2649ad45a..ce22ea35d 100644--- a/sdk/src/ai/nav/navGeometry.ts+++ b/sdk/src/ai/nav/navGeometry.ts@@ -20,7 +20,7 @@ import type { Entity, Quat, Vec3 } from '../../types';import type { World } from '../../ecs/world';import { Transform } from '../../ecs/component';import {- RigidBody3D, MeshCollider3D, ConvexCollider3D,+ RigidBody3D, MeshCollider3D, ConvexCollider3D, BoxCollider3D, SphereCollider3D, CapsuleCollider3D,type RigidBody3DData, type MeshCollider3DData, type ConvexCollider3DData,} from '../../physics3d/Physics3DComponents';import {@@ -28,6 +28,7 @@ import {type Collider3DInstance,} from '../../physics3d/ColliderShape3D';import { getMeshCollision } from '../../asset/meshCollision';+import { isImplicitStatic } from '../../physics3d/implicitStatic';/** World-space triangle soup: `verts` is `vertexCount * 3` floats. */export interface NavGeometry {@@ -53,8 +54,7 @@ export interface CollectNavGeometryOptions {* produces no shape at all, so the projection would always report nothing amiss.*/export function navGeometryReady(world: World, layers = 0): boolean {- for (const entity of world.getEntitiesWithComponents([RigidBody3D, Transform])) {- if (!isBakeable(world, entity, layers)) continue;+ for (const entity of bakeableEntities(world, layers)) {const handle = namedMesh(world, entity);if (handle !== 0 && getMeshCollision(handle) === null) return false;}@@ -77,23 +77,23 @@ export function collectNavGeometry(world: World, opts: CollectNavGeometryOptionsconst indices: number[] = [];let bodyCount = 0;- for (const entity of world.getEntitiesWithComponents([RigidBody3D, Transform])) {- if (!isBakeable(world, entity, layers)) continue;+ for (const entity of bakeableEntities(world, layers)) {const local = localTriangles(world, entity);if (!local) continue;- // The transform the 3D SOLVER builds its bodies from. A mesh baked from- // anywhere else would describe a world the agent does not collide with.+ // The transform the 3D SOLVER builds its bodies from — the composed WORLD pose, and for a mesh its world+ // scale. The local pose put every collider under a parent (a prefab's meshes, a level's) somewhere else.const tf = world.get(entity, Transform);- const at = tf.position as Vec3;- const rot = tf.rotation as Quat;+ const at = (tf.worldPosition ?? tf.position) as Vec3;+ const rot = (tf.worldRotation ?? tf.rotation) as Quat;+ const sc = local.scaled ? ((tf.worldScale ?? tf.scale) as Vec3 | undefined) : undefined;const base = verts.length / 3;let minX = Infinity, minY = Infinity, minZ = Infinity;let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;for (let i = 0; i + 2 < local.positions.length; i += 3) {const p = rotateVec3ByQuat(rot, {- x: local.positions[i]!, y: local.positions[i + 1]!, z: local.positions[i + 2]!,+ x: local.positions[i]! * (sc?.x ?? 1), y: local.positions[i + 1]! * (sc?.y ?? 1), z: local.positions[i + 2]! * (sc?.z ?? 1),});const x = at.x + p.x;const y = at.y + p.y;@@ -126,6 +126,27 @@ function isBakeable(world: World, entity: Entity, layers: number): boolean {return layers === 0 || (layers & (1 << body.layer)) !== 0;}+/**+ * The ground: every static body, the authored ones and the colliders that are static for having no RigidBody3D+ * (implicitStatic.ts) — an imported level is a MeshCollider3D on each mesh and nothing else, and baked to no ground.+ */+function* bakeableEntities(world: World, layers: number): Iterable<Entity> {+ // World poses are read below, so they are made current first, as the solver does before it builds a body.+ world.ensureTransformsComposed?.();+ for (const entity of world.getEntitiesWithComponents([RigidBody3D, Transform])) {+ if (isBakeable(world, entity, layers)) yield entity;+ }+ const seen = new Set<Entity>();+ for (const def of IMPLICIT_GROUND) {+ for (const entity of world.getEntitiesWithComponents([def, Transform])) {+ if (seen.has(entity) || !isImplicitStatic(world, entity)) continue;+ seen.add(entity);+ const layer = (world.get(entity, def) as { layer?: number }).layer ?? 0;+ if (layers === 0 || (layers & (1 << layer)) !== 0) yield entity;+ }+ }+}+/** The one shape the solver builds for this entity, if it is part of the ground.* A character is an agent walking the mesh rather than part of what it walks* on, and a sensor is a trigger nobody bumps into. */@@ -140,7 +161,7 @@ function activeCollider(world: World, entity: Entity): Collider3DInstance | nullfunction localTriangles(world: World, entity: Entity,-): { positions: Float32Array; indices: ArrayLike<number> } | null {+): { positions: Float32Array; indices: ArrayLike<number>; scaled?: boolean } | null {const active = activeCollider(world, entity);if (!active) return null;if (active.component === 'MeshCollider3D' || active.component === 'ConvexCollider3D') {@@ -148,9 +169,11 @@ function localTriangles(// The hull a convex collider builds is not the mesh, but the mesh's own// triangles are what describe where it is; the difference is a dent the// voxeliser would have rounded away in any case.- return data ? { positions: data.positions, indices: data.indices } : null;+ return data ? { positions: data.positions, indices: data.indices, scaled: true } : null;}return collider3DTriangles(active.shape);}const STATIC = 0;+/** Colliders that are ground on their own, with no RigidBody3D. */+const IMPLICIT_GROUND = [MeshCollider3D, ConvexCollider3D, BoxCollider3D, SphereCollider3D, CapsuleCollider3D] as const;diff --git a/sdk/src/ai/perception/PerceptionPlugin.ts b/sdk/src/ai/perception/PerceptionPlugin.tsindex 4514ff1f8..a9b844adf 100644--- a/sdk/src/ai/perception/PerceptionPlugin.ts+++ b/sdk/src/ai/perception/PerceptionPlugin.ts@@ -23,7 +23,9 @@ import { Physics3D } from '../../physics3d/Physics3DPlugin';import type { Physics3DQueries } from '../../physics3d/Physics3DQueries';import { senseTarget, facingFromRotation } from './sense';import type { Vec3 } from '../../types';-import { Perceiver, Perception, PerceptionTarget, type PerceptionData } from './components';+import { Perceiver, Perception, PerceptionTarget, Noise, type PerceptionData, type NoisePayload } from './components';+import { EventReader } from '../../ecs/event';+import { Res, Time, type TimeData } from '../../ecs/resource';/** The slice of `World` the perception step needs — lets tests inject a fake. */export interface PerceptionWorldView {@@ -32,6 +34,8 @@ export interface PerceptionWorldView {set<C extends AnyComponentDef>(entity: Entity, component: C, data: ComponentData<C>): void;has(entity: Entity, component: AnyComponentDef): boolean;insert<C extends AnyComponentDef>(entity: Entity, component: C, data?: Partial<ComponentData<C>>): unknown;+ /** Make composed (world) poses current; a fake world without them leaves this out. */+ ensureTransformsComposed?(): void;}/**@@ -46,18 +50,28 @@ export type LosCheck = (layers: number,) => boolean;-/** Update every Perceiver's Perception with the nearest visible target. */-export function stepPerception(world: PerceptionWorldView, isBlocked?: LosCheck): void {+/** Where an entity is in the world: its composed pose where the world keeps one, its own otherwise (a fake world). */+const worldAt = (tf: { position: unknown; worldPosition?: unknown }) => (tf.worldPosition ?? tf.position) as Vec3;++/**+ * Update every Perceiver's Perception: the nearest visible target, and the nearest noise it heard.+ * Positions are WORLD positions, so a perceiver can sit on an eye (a head bone, a sensor) and a target on what is+ * looked at (a player's eye camera); sight measured from one character's feet to another's was cut by every kerb.+ * `noises` are this step's {@link Noise} events; `dt` ages what was heard before.+ */+export function stepPerception(world: PerceptionWorldView, isBlocked?: LosCheck,+ noises: readonly NoisePayload[] = [], dt = 0): void {+ world.ensureTransformsComposed?.();const targets = world.getEntitiesWithComponents([PerceptionTarget, Transform]).map(e => {const t = world.get(e, Transform);- return { e, at: t.position as Vec3 };+ return { e, at: worldAt(t) };});for (const e of world.getEntitiesWithComponents([Perceiver, Transform])) {const cfg = world.get(e, Perceiver);const tf = world.get(e, Transform);- const at = tf.position as Vec3;- const facing = facingFromRotation(tf.rotation, cfg.facingAxis);+ const at = worldAt(tf);+ const facing = facingFromRotation((tf.worldRotation ?? tf.rotation) as typeof tf.rotation, cfg.facingAxis);const halfFov = ((cfg.fovDegrees * Math.PI) / 180) / 2;let best: { at: Vec3; distance: number; dir: Vec3 } | null = null;@@ -70,13 +84,29 @@ export function stepPerception(world: PerceptionWorldView, isBlocked?: LosCheck)}}+ // Hearing: the nearest noise within reach this step, else what was heard before, a step older.+ const before = world.has(e, Perception) ? world.get(e, Perception) as Partial<PerceptionData> : null;+ let heard: NoisePayload | null = null, heardAt = Infinity;+ const hearing = cfg.hearingRange ?? 0;+ if (hearing > 0) {+ for (const n of noises) {+ if (n.source === e) continue;+ const d = Math.hypot(n.x - at.x, n.y - at.y, n.z - at.z);+ if (d <= hearing * (n.loudness ?? 1) && d < heardAt) { heard = n; heardAt = d; }+ }+ }+ const since = before?.sinceHeard ?? -1;+ const ears = heard+ ? { heard: true, noiseX: heard.x, noiseY: heard.y, noiseZ: heard.z, sinceHeard: 0 }+ : { heard: false, noiseX: before?.noiseX ?? 0, noiseY: before?.noiseY ?? 0, noiseZ: before?.noiseZ ?? 0, sinceHeard: since < 0 ? -1 : since + dt };+const p: PerceptionData = best? {visible: true, distance: best.distance,targetX: best.at.x, targetY: best.at.y, targetZ: best.at.z,- dirX: best.dir.x, dirY: best.dir.y, dirZ: best.dir.z,+ dirX: best.dir.x, dirY: best.dir.y, dirZ: best.dir.z, ...ears,}- : { visible: false, distance: 0, targetX: 0, targetY: 0, targetZ: 0, dirX: 0, dirY: 0, dirZ: 0 };+ : { visible: false, distance: 0, targetX: 0, targetY: 0, targetZ: 0, dirX: 0, dirY: 0, dirZ: 0, ...ears };if (world.has(e, Perception)) world.set(e, Perception, p);else world.insert(e, Perception, p);@@ -125,15 +155,15 @@ export class PerceptionPlugin implements Plugin {app.addSystemToSchedule(Schedule.PreUpdate,defineSystem(- [GetWorld()],- world => {+ [GetWorld(), EventReader(Noise), Res(Time)],+ (world, noises: Iterable<NoisePayload>, time: TimeData) => {// The 3D solver first where a scene has one: its bodies are the// walls of a 3D scene, and the 2D one knows nothing about them.const q3d = app.hasResource(Physics3D)? app.getResource<Physics3DQueries | null>(Physics3D) : null;const physics = app.hasResource(Physics2D) ? app.getResource<Physics2DAPI>(Physics2D) : null;const los = q3d ? makeLosCheck3D(q3d) : physics ? makeLosCheck(physics) : undefined;- stepPerception(world as PerceptionWorldView, los);+ stepPerception(world as PerceptionWorldView, los, [...noises], time.delta);},{name: 'PerceptionSystem',diff --git a/sdk/src/ai/perception/components.ts b/sdk/src/ai/perception/components.tsindex 9473ad1a2..4c055fb83 100644--- a/sdk/src/ai/perception/components.ts+++ b/sdk/src/ai/perception/components.ts@@ -9,6 +9,8 @@*/import { defineComponent, defineTag } from '../../ecs/component';+import { defineEvent, type EventDef } from '../../ecs/event';+import type { Entity } from '../../types';import type { FacingAxis } from './sense';export interface PerceiverData {@@ -25,6 +27,11 @@ export interface PerceiverData {* against the layers that actually block sight.*/losLayers: number;+ /**+ * How far a {@link Noise} of loudness 1 is heard, in world units; 0 = deaf. A noise is heard within+ * `hearingRange * loudness`, through walls, as Unreal's hearing sense is.+ */+ hearingRange: number;}export const Perceiver = defineComponent<PerceiverData>('Perceiver', {@@ -32,12 +39,14 @@ export const Perceiver = defineComponent<PerceiverData>('Perceiver', {fovDegrees: 360,facingAxis: 'x',losLayers: 0,+ hearingRange: 0,}, {fields: {range: { min: 0, unit: 'px', category: 'Perception' },fovDegrees: { min: 0, max: 360, unit: '°', category: 'Perception' },facingAxis: { category: 'Perception', advanced: true, tooltip: "'x' is screen-right (a flat scene); '-z' is where an imported model faces." },losLayers: { min: 0, category: 'Perception', advanced: true, tooltip: '3D layer mask the sight ray may hit; 0 = every layer.' },+ hearingRange: { min: 0, category: 'Perception', tooltip: 'How far a noise of loudness 1 is heard, through walls; 0 = deaf.' },},});@@ -54,6 +63,14 @@ export interface PerceptionData {dirX: number;dirY: number;dirZ: number;+ /** A noise was heard this step. */+ heard: boolean;+ /** Where the last noise heard came from. */+ noiseX: number;+ noiseY: number;+ noiseZ: number;+ /** Seconds since that noise; -1 = nothing heard yet. */+ sinceHeard: number;}export const Perception = defineComponent<PerceptionData>('Perception', {@@ -65,6 +82,11 @@ export const Perception = defineComponent<PerceptionData>('Perception', {dirX: 0,dirY: 0,dirZ: 0,+ heard: false,+ noiseX: 0,+ noiseY: 0,+ noiseZ: 0,+ sinceHeard: -1,}, {// Runtime-only: the system rewrites it every frame.transient: true,@@ -75,3 +97,19 @@ export const Perception = defineComponent<PerceptionData>('Perception', {/** Tags an entity as sensable by Perceivers. */export const PerceptionTarget = defineTag('PerceptionTarget');++/** What a noise carries: where it was made, how loud (1 = heard out to a Perceiver's `hearingRange`), and who made it. */+export interface NoisePayload {+ x: number;+ y: number;+ z: number;+ loudness: number;+ /** The entity that made it; it does not hear itself. 0 = no one in particular. */+ source: Entity;+}++/**+ * Something made a sound perceivers may hear (a gunshot, a door, a footfall). Send it with `EventWriter(Noise)`; the+ * perception step reads it the frame after, as every event is read.+ */+export const Noise: EventDef<NoisePayload> = defineEvent<NoisePayload>('Noise', { x: 0, y: 0, z: 0, loudness: 1, source: 0 as Entity });diff --git a/sdk/src/ai/perception/index.ts b/sdk/src/ai/perception/index.tsindex eb03f8204..fc90cf24d 100644--- a/sdk/src/ai/perception/index.ts+++ b/sdk/src/ai/perception/index.ts@@ -7,8 +7,8 @@export { senseTarget, facingFromRotation, normalizeAngle, type SenseResult, type FacingAxis } from './sense';export {- Perceiver, Perception, PerceptionTarget,- type PerceiverData, type PerceptionData,+ Perceiver, Perception, PerceptionTarget, Noise,+ type PerceiverData, type PerceptionData, type NoisePayload,} from './components';export {PerceptionPlugin, perceptionPlugin, stepPerception, makeLosCheck,diff --git a/sdk/src/physics3d/Physics3DModule.ts b/sdk/src/physics3d/Physics3DModule.tsindex ed14d56d2..be355ae19 100644--- a/sdk/src/physics3d/Physics3DModule.ts+++ b/sdk/src/physics3d/Physics3DModule.ts@@ -87,6 +87,9 @@ export interface Physics3DWasmModule {_physics3d_setCharacterPosition(characterId: number, px: number, py: number,pz: number): void;+ /** `_physics3d_raycast` with flags: bit 0 also meets characters. */+ _physics3d_raycastWith(ox: number, oy: number, oz: number, dx: number, dy: number, dz: number,+ layerMask: number, flags: number): number;_physics3d_raycast(ox: number, oy: number, oz: number,dx: number, dy: number, dz: number, layerMask: number): number;_physics3d_sphereCast(px: number, py: number, pz: number, radius: number,@@ -163,7 +166,7 @@ export const PHYSICS3D_TRANSFORM_STRIDE = 8;const REQUIRED_EXPORTS = ['_physics3d_init', '_physics3d_shutdown', '_physics3d_isReady', '_physics3d_step', '_physics3d_setSolver','_physics3d_addBox', '_physics3d_addSphere', '_physics3d_addCapsule',- '_physics3d_removeBody', '_physics3d_setTransform', '_physics3d_raycast',+ '_physics3d_removeBody', '_physics3d_setTransform', '_physics3d_raycast', '_physics3d_raycastWith','_physics3d_transforms', '_physics3d_transformsBytes','_physics3d_addCharacter', '_physics3d_moveCharacter', '_physics3d_removeCharacter','_physics3d_setCharacterPosition',diff --git a/sdk/src/physics3d/Physics3DQueries.ts b/sdk/src/physics3d/Physics3DQueries.tsindex f179dfa44..456b3e928 100644--- a/sdk/src/physics3d/Physics3DQueries.ts+++ b/sdk/src/physics3d/Physics3DQueries.ts@@ -59,14 +59,22 @@ export class Physics3DQueries {return true;}- /** The nearest body along a ray, or null. `direction` carries its length. */+ /**+ * The nearest body along a ray, or null. `direction` carries its length.+ *+ * Characters (CharacterController3D) are passed through unless `options.characters` asks for them: line of sight and+ * a camera's boom ask about the world, a shot asks about who is in the way. A ray that starts inside a character+ * meets it at once, so a shooter leaves its own out by its layer.+ */raycast(origin: { x: number; y: number; z: number },direction: { x: number; y: number; z: number },- layerMask = 0): Cast3DHit | null {+ layerMask = 0, options?: { characters?: boolean }): Cast3DHit | null {const p = this.ppu_;- const hit = this.module_._physics3d_raycast(- origin.x / p, origin.y / p, origin.z / p,- direction.x / p, direction.y / p, direction.z / p, layerMask);+ const args = [origin.x / p, origin.y / p, origin.z / p,+ direction.x / p, direction.y / p, direction.z / p, layerMask] as const;+ const hit = options?.characters+ ? this.module_._physics3d_raycastWith(...args, 1)+ : this.module_._physics3d_raycast(...args);return hit ? this.readCast_() : null;}diff --git a/sdk/tests/nav-geometry.test.ts b/sdk/tests/nav-geometry.test.tsindex 5e9ad593b..d7853b443 100644--- a/sdk/tests/nav-geometry.test.ts+++ b/sdk/tests/nav-geometry.test.ts@@ -30,7 +30,8 @@ const BOX = { min: { x: -500, y: -500, z: -500 }, max: { x: 500, y: 500, z: 500/** A static box collider at `at`, plus whatever else the caller wants on it. */function addFloor(world: World, at = { x: 0, y: 0, z: 0 }, body = {}, collider = {}): number {const e = world.spawn();- world.insert(e, Transform, { position: at });+ // The mock composes nothing, so the world pose a running game composes is written beside the local one.+ world.insert(e, Transform, { position: at, worldPosition: at });world.insert(e, RigidBody3D, { bodyType: 0, ...body });world.insert(e, BoxCollider3D, { halfExtents: { x: 100, y: 10, z: 100 }, ...collider });return e;@@ -50,6 +51,29 @@ function boundsOf(geo: { verts: Float32Array }): { min: number[]; max: number[]}describe('collectNavGeometry', () => {+ // An imported level is a MeshCollider3D on each mesh and no RigidBody3D: static, as in Unity and as the solver+ // builds it. It baked to no ground, and an agent on Saltline's yard had nowhere to walk.+ it('takes a collider with no RigidBody3D as ground, at its world pose', () => {+ const world = testWorld();+ const e = world.spawn();+ world.insert(e, Transform, { position: { x: 0, y: 0, z: 0 }, worldPosition: { x: 50, y: 20, z: -30 } });+ world.insert(e, BoxCollider3D, { halfExtents: { x: 100, y: 10, z: 100 } });+ const geo = collectNavGeometry(world, BOX);+ expect(geo.bodyCount).toBe(1);+ const { min, max } = boundsOf(geo);+ expect(min).toEqual([-50, 10, -130]);+ expect(max).toEqual([150, 30, 70]);+ });++ it('leaves a collider-only character out of the ground', () => {+ const world = testWorld();+ const e = world.spawn();+ world.insert(e, Transform, { position: { x: 0, y: 0, z: 0 }, worldPosition: { x: 0, y: 0, z: 0 } });+ world.insert(e, BoxCollider3D, { halfExtents: { x: 100, y: 10, z: 100 } });+ world.insert(e, CharacterController3D);+ expect(collectNavGeometry(world, BOX).bodyCount).toBe(0);+ });+it('takes a static collider, at the size and place the scene put it', () => {const world = testWorld();addFloor(world, { x: 50, y: 20, z: -30 });diff --git a/sdk/tests/perception-step.test.ts b/sdk/tests/perception-step.test.tsindex 2e21e8d83..fb123063b 100644--- a/sdk/tests/perception-step.test.ts+++ b/sdk/tests/perception-step.test.ts@@ -129,3 +129,59 @@ describe('stepPerception', () => {expect(w.get(p, Perception).targetX).toBe(60);});});++// Hearing: Saltline's machines come to a gunshot within 45 m, through the containers between. Unreal's hearing sense is+// the same shape: a noise of some loudness, heard within hearingRange * loudness, walls or not.+describe('stepPerception hearing', () => {+ const noise = (x: number, y: number, loudness = 1, source = 0 as Entity) => ({ x, y, z: 0, loudness, source });+ const listener = (w: FakeWorld, x: number, hearingRange: number) =>+ w.spawn([[Perceiver, Perceiver.create({ range: 10, hearingRange })], [Transform, tf(x, 0)]]);++ it('hears a noise within reach, where it was made, through anything', () => {+ const w = new FakeWorld();+ const p = listener(w, 0, 45);+ stepPerception(w, () => true, [noise(40, 0)], 0.016);+ const per = w.get(p, Perception);+ expect(per.heard).toBe(true);+ expect(per.visible).toBe(false);+ expect([per.noiseX, per.noiseY, per.sinceHeard]).toEqual([40, 0, 0]);+ });++ it('does not hear past its range scaled by loudness, nor a noise of its own, and a deaf one hears nothing', () => {+ const w = new FakeWorld();+ const p = listener(w, 0, 45);+ const deaf = listener(w, 0, 0);+ stepPerception(w, undefined, [noise(50, 0), noise(20, 0, 0.4), noise(1, 0, 1, p)], 0.016);+ expect(w.get(p, Perception).heard).toBe(false);+ expect(w.get(deaf, Perception).heard).toBe(false);+ stepPerception(w, undefined, [noise(80, 0, 2)], 0.016);+ expect(w.get(p, Perception).heard).toBe(true);+ });++ it('keeps where it last heard something, older each step, until it hears again', () => {+ const w = new FakeWorld();+ const p = listener(w, 0, 45);+ stepPerception(w, undefined, [], 0.5);+ expect(w.get(p, Perception).sinceHeard).toBe(-1);+ stepPerception(w, undefined, [noise(10, 5)], 0.5);+ stepPerception(w, undefined, [], 0.5);+ stepPerception(w, undefined, [], 0.25);+ const per = w.get(p, Perception);+ expect([per.heard, per.noiseX, per.noiseY, per.sinceHeard]).toEqual([false, 10, 5, 0.75]);+ });+});++// A perceiver on an eye and a target on an eye: both are children, and their own positions are near the origin.+describe('stepPerception in the world', () => {+ it('senses from and to WORLD positions where the world keeps them', () => {+ const w = new FakeWorld();+ const eye = w.spawn([[Perceiver, Perceiver.create({ range: 100 })],+ [Transform, { position: { x: 0, y: 0.6, z: 0 }, worldPosition: { x: 10, y: 1.6, z: 0 }, rotation: { x: 0, y: 0, z: 0, w: 1 }, worldRotation: { x: 0, y: 0, z: 0, w: 1 } }]]);+ w.spawn([[PerceptionTarget, {}], [Transform, { position: { x: 0, y: 0.7, z: 0 }, worldPosition: { x: 40, y: 1.6, z: 0 }, rotation: { x: 0, y: 0, z: 0, w: 1 } }]]);+ stepPerception(w);+ const per = w.get(eye, Perception);+ expect(per.visible).toBe(true);+ expect(per.distance).toBeCloseTo(30);+ expect(per.targetX).toBe(40);+ });+});diff --git a/sdk/tests/physics3d-smoke.mjs b/sdk/tests/physics3d-smoke.mjsindex 08f26ef35..f2e4c0db3 100644--- a/sdk/tests/physics3d-smoke.mjs+++ b/sdk/tests/physics3d-smoke.mjs@@ -198,6 +198,20 @@ check('a character walks where it is sent', walked.x > landed.x + 0.8,`x=${walked.x.toFixed(4)} from ${landed.x.toFixed(4)}`);check('and stays on the ground while walking', walked.ground === ON_GROUND);+// A shot has to meet a character; line of sight and a camera's boom must not. A ray across the hero's capsule at+// chest height: the plain one passes it, the one that asks for characters stops on its surface, 0.3 short of its axis.+{+ const ox = walked.x, ray = (flags, mask = 0) => (m._physics3d_raycastWith(ox, 0.8, -5, 0, 0, 10, mask, flags) ? readRay() : null);+ const plain = ray(0);+ check('a plain ray passes through a character', !plain || plain.entity !== 9, JSON.stringify(plain));+ const shot = ray(1);+ check('a ray asking for characters meets one', shot && shot.entity === 9, JSON.stringify(shot));+ check('on its capsule, 0.3 before its axis', shot && near(shot.z, -0.3, 0.02) && near(shot.fraction, 0.47, 0.01),+ `z=${shot?.z?.toFixed(4)} fraction=${shot?.fraction?.toFixed(4)}`);+ check('with the normal facing the ray', shot && near(shot.nz, -1, 0.05), `nz=${shot?.nz?.toFixed(4)}`);+ check('and not when its layer is masked out', !ray(1, 1 << 1));+}+// A 0.2m step is climbed rather than stopped at: without WalkStairs the character// stops dead against it, so the y AFTER is what tells the two apart.m._physics3d_addBox(10, 0.5, 0.1, 5, walked.x + 1.3, 0.1, 0, ...IDENTITY,diff --git a/src/esengine/bindings/modules/physics3d/Physics3DBindings.hpp b/src/esengine/bindings/modules/physics3d/Physics3DBindings.hppindex 05cd060d9..289932d3c 100644--- a/src/esengine/bindings/modules/physics3d/Physics3DBindings.hpp+++ b/src/esengine/bindings/modules/physics3d/Physics3DBindings.hpp@@ -36,6 +36,10 @@ int physics3d_isReady();void physics3d_step(float dt, int collisionSteps);void physics3d_optimize();void physics3d_setLayerMask(uint32_t layer, uint32_t mask);+/// physics3d_raycast with flags: bit 0 also meets characters (a shot, which must hit an enemy that walks).+/// Plain physics3d_raycast stays blind to them: line of sight and a camera's boom ask about the world.+int physics3d_raycastWith(float ox, float oy, float oz, float dx, float dy, float dz,+ uint32_t layerMask, uint32_t flags);/// The project's solver settings. Each value <= 0 keeps the backend's own default/// (Jolt's for Jolt, Unity's for PhysX); allowSleep is 0 or 1. After init, before bodies.void physics3d_setSolver(int velocityIterations, int positionIterations, int allowSleep,diff --git a/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp b/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cppindex dcd4722b0..666518b8b 100644--- a/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp+++ b/src/esengine/bindings/modules/physics3d/Physics3DModuleEntry.cpp@@ -848,6 +848,48 @@ int physics3d_raycast(float ox, float oy, float oz, float dx, float dy, float dzreturn 1;}+/**+ * @brief The world's nearest hit, or a character's when one is nearer and `flags` bit 0 asks for them.+ * @details A CharacterVirtual is not a body, so the narrow phase cannot meet it; each capsule is cast against in its+ * own space instead, which is what Jolt's own CharacterVirtual queries do. A ray that starts inside a capsule+ * meets it at 0 — a shooter's own character is left out by its layer.+ */+EMSCRIPTEN_KEEPALIVE+int physics3d_raycastWith(float ox, float oy, float oz, float dx, float dy, float dz,+ uint32_t layerMask, uint32_t flags) {+ const int worldHit = physics3d_raycast(ox, oy, oz, dx, dy, dz, layerMask);+ if ((flags & 1u) == 0 || !g().isValid()) return worldHit;+ float best = worldHit ? g().queryBuffer[1] : 1.0f;+ const RRayCast ray{RVec3(ox, oy, oz), Vec3(dx, dy, dz)};+ uint32_t entity = 0;+ Vec3 normal = Vec3::sZero();+ bool found = false;+ for (const auto& entry : g().characters) {+ const auto layer = g().characterLayers.find(entry.first);+ if (!layerPasses(layerMask, layer == g().characterLayers.end() ? 0u : layer->second)) continue;+ const auto owner = g().entityOf.find(entry.first | CHARACTER_ID_TAG);+ if (owner == g().entityOf.end()) continue;+ const RMat44 com = entry.second->GetCenterOfMassTransform();+ const RayCast local(ray.Transformed(com.InversedRotationTranslation()));+ RayCastResult hit;+ hit.mFraction = best;+ const Shape* shape = entry.second->GetShape();+ if (!shape->CastRay(local, SubShapeIDCreator(), hit) || hit.mFraction >= best) continue;+ best = hit.mFraction;+ entity = owner->second;+ normal = com.Multiply3x3(shape->GetSurfaceNormal(hit.mSubShapeID2, local.GetPointOnRay(best))).NormalizedOr(Vec3::sAxisY());+ found = true;+ }+ if (!found) return worldHit;+ const RVec3 point = ray.GetPointOnRay(best);+ g().queryBuffer = {+ static_cast<float>(entity), best,+ static_cast<float>(point.GetX()), static_cast<float>(point.GetY()), static_cast<float>(point.GetZ()),+ normal.GetX(), normal.GetY(), normal.GetZ(),+ };+ return 1;+}+// ReadbackEMSCRIPTEN_KEEPALIVEdiff --git a/src/esengine/bindings/modules/physics3d_physx/PhysXQueries.cpp b/src/esengine/bindings/modules/physics3d_physx/PhysXQueries.cppindex eb2d56585..0fb32b580 100644--- a/src/esengine/bindings/modules/physics3d_physx/PhysXQueries.cpp+++ b/src/esengine/bindings/modules/physics3d_physx/PhysXQueries.cpp@@ -386,21 +386,55 @@ int physics3d_sphereCast(float px, float py, float pz, float radius,EMSCRIPTEN_KEEPALIVEint physics3d_raycast(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask) {+ return physics3d_raycastWith(ox, oy, oz, dx, dy, dz, layerMask, 0);+}++EMSCRIPTEN_KEEPALIVE+int physics3d_raycastWith(float ox, float oy, float oz, float dx, float dy, float dz, uint32_t layerMask,+ uint32_t flags) {g().queryBuffer.clear();if (!g().isValid()) return 0;PxVec3 dir(dx, dy, dz);const float length = dir.normalizeSafe();if (!(length > 0.0f)) return 0;+ const PxVec3 origin(ox, oy, oz);+ float best = length;+ PxVec3 point(0.0f), normal(0.0f);+ uint32_t entity = 0;+ bool found = false;PxRaycastBuffer hit;LayerFilter filter(layerMask, false);- const PxVec3 origin(ox, oy, oz);- if (!g().scene->raycast(origin, dir, length, hit, PxHitFlag::ePOSITION | PxHitFlag::eNORMAL,- queryData(), &filter)- || !hit.hasBlock) return 0;- const PxRaycastHit& b = hit.block;- const PxVec3 point = origin + dir * b.distance;- g().queryBuffer = {static_cast<float>(px3::entityOf(b.actor)), b.distance / length,- point.x, point.y, point.z, b.normal.x, b.normal.y, b.normal.z};+ if (g().scene->raycast(origin, dir, length, hit, PxHitFlag::ePOSITION | PxHitFlag::eNORMAL, queryData(), &filter)+ && hit.hasBlock) {+ best = hit.block.distance;+ point = origin + dir * best;+ normal = hit.block.normal;+ entity = px3::entityOf(hit.block.actor);+ found = true;+ }+ // Characters where they are NOW: a controller's actor reaches the pose a move gave it only at the next simulate, so+ // the scene would answer with last step's capsule. Cast against each one directly, as the Jolt backend does.+ if ((flags & 1u) != 0) {+ const PxQuat upright(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f));+ const uint32_t mask = layerMask == 0 ? 0xFFFFFFFFu : layerMask;+ for (const auto& entry : g().characters) {+ const Character& c = *entry.second;+ if ((mask & (1u << c.layer)) == 0) continue;+ auto& capsule = static_cast<PxCapsuleController&>(*c.controller);+ const PxCapsuleGeometry geometry(capsule.getRadius(), 0.5f * capsule.getHeight());+ const PxTransform pose(toVec(c.controller->getPosition()), upright);+ PxRaycastHit h;+ if (PxGeometryQuery::raycast(origin, dir, geometry, pose, best, PxHitFlag::ePOSITION | PxHitFlag::eNORMAL, 1, &h) == 0) continue;+ if (h.distance >= best && found) continue;+ best = h.distance;+ point = origin + dir * best;+ normal = h.normal;+ entity = c.entity;+ found = true;+ }+ }+ if (!found) return 0;+ g().queryBuffer = {static_cast<float>(entity), best / length, point.x, point.y, point.z, normal.x, normal.y, normal.z};return 1;}