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.

feat(terrain): a ray meets the ground's triangles anywhere, with no physics and no collision tile there

yhh4d0f87382e98
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9a6116c81..396fce8ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ published separately; it ships inside the editor.
- **Generated worlds.** The new `esengine/worldgen` lays a world out by a game's rules as it is explored: biomes weighed from fields (the terrain's height and slope, the water's depth, seeded moisture, temperature and rock, and a game's own), mixed at their edges and painting the terrain; recipes an artist composes (an anchor and members round it, with footprint apart from canopy) placed in layers, big things first, each keeping clear of what was placed before it; and `WorldZone`s (a home, a road, a building) that keep out what they name and hold a biome, fading at their edge. The same seed lays out the same world in whatever order its chunks are made, every placement has an id that outlives rules added later, interactive ones are entities with a `PlacedObject`, and what players change (`diffs().remove(id)`) is kept apart and laid over before anything is shown. `plan().explain(x, z)` says why anything stands or does not. A new example, Generated World, walks through one.
- **Which water a place is.** The sea fills only low ground joined to the open sea past the water zone's edge: a hollow below its level walled off by higher ground stays dry (or holds a lake of its own). A lake or a river is water only where its surface is over its ground, so a hill inside its outline is land, for what is drawn and what `Waters.sample` answers, whatever order the bodies were added in. `WaterBody.bodyId` names a body the same in every session; `Waters.sample` carries it and `Waters.bodyAt(x, z)` says which body a place belongs to. `findBasins` gives a game the ground's basins (each filled to the lowest point of its rim, with its outlet), the drainage down to the edge and the sea's reach, for deciding where its lakes and rivers go; `drainPath` follows the water from a point.
- **An area without a heightmap is a window on its provider's ground.** Its noise is read from `Terrain.provider` where the area stands in the world, over the provider's height range, instead of being made in the area's own space and stretched to fill it: two areas of the same size side by side join, an area is the same ground its provider gives without end, and a game's own provider can make an area. Ground read from a provider is read again when its terrain is moved, so it stays the world's ground.
+- **Rays against the terrain, anywhere.** `Terrains.raycast(origin, direction, maxDistance)` meets the ground's triangles exactly (the ones collision and `heightAt` use) wherever the ray goes, with no physics world and no collision tile there, for a line of sight, a pick or a landing spot.
- **A vertex hook can hand the fragment a colour.** What a material's vertex hook leaves in `es_vertexColor` is now the colour its fragment receives (times the instance's tint), on both backends.
- **Bakes run over frames, with progress.** Work that used to hold a frame until it was done (a terrain worn by rain, a water zone baked, terrain chunks built, plants grown) is now spread over frames within a budget, and listed with its progress in the new `Bakes` resource (`bakesOf(app).active`). The editor's status bar shows it as a bar, for the edit world and for the game running in Play, and a game can show it on a loading screen. Plants wait for the ground and the water they stand on instead of being grown twice.
- **Eroded and warped terrain.** `Terrain.erosion` wears the ground with rain (a heightmap's as well as the noise's): gullies down the slopes, sharper ridges, silt in the valleys, and dirt painted where the water ran; the same seed wears the same ground. `Terrain.warp` bends the noise by noise of its own for winding valleys and bent ridges. `erodeHeightfield` and `erosionJob` do the same for a tool.
diff --git a/docs/WORLD_GENERATION_PLAN.md b/docs/WORLD_GENERATION_PLAN.md
index a4e980a83..ab929320b 100644
--- a/docs/WORLD_GENERATION_PLAN.md
+++ b/docs/WORLD_GENERATION_PLAN.md
@@ -27,7 +27,7 @@
- 需要:地形碰撞(先桥接静态 `MeshCollider3D`,再评估专用高度场碰撞);碰撞精度有自己稳定的规则,不随摄像机 LOD 变化;高度/法线查询、射线、碰撞与三角面在约定精度内吻合。
- 已知差异:`sampleHeight` 双线性插值,渲染为两片三角形(3×3 合成例中对角线中点 0.5 对 1.0)。新 CDLOD 渲染按页样本插值并在细节级之间形变,同样需要与查询统一定义。
-- 当前状态:已完成第一步。统一定义见 `sdk/src/terrain/surface.ts`:`cellSize` 格点、节点高度取数据源、每格沿 (1,0)–(0,1) 对角线切分,与最细一级渲染补丁同一三角面(差异仅 GPU 16 位高度编码);`heightAt`/`normalAt`(三角面法线)读它;碰撞按 32 格分块,在每个运动刚体与角色周围 `collisionDistance` 内构建,经通用的 `CollisionGeometry` 资源(`sdk/src/runtime/collisionGeometry.ts`)交给 3D 物理,每块一个静态网格刚体;重建期间旧块保留到新块覆盖;`Terrains.collisionReadyAt` 供游戏等待。测试 `sdk/tests/terrain-surface.test.ts`(含真实 Jolt:射线跨块缝与 `heightAt` 吻合到 1e-3、落球静止在地面上)。仍欠:专用高度场形状(Jolt HeightFieldShape,省内存)评估;远处没有运动物体的位置无碰撞(远程射线需走 `heightAt`);分块驻留尚未归 T-03 的统一预算。
+- 当前状态:已完成第一步。统一定义见 `sdk/src/terrain/surface.ts`:`cellSize` 格点、节点高度取数据源、每格沿 (1,0)–(0,1) 对角线切分,与最细一级渲染补丁同一三角面(差异仅 GPU 16 位高度编码);`heightAt`/`normalAt`(三角面法线)读它;碰撞按 32 格分块,在每个运动刚体与角色周围 `collisionDistance` 内构建,经通用的 `CollisionGeometry` 资源(`sdk/src/runtime/collisionGeometry.ts`)交给 3D 物理,每块一个静态网格刚体;重建期间旧块保留到新块覆盖;`Terrains.collisionReadyAt` 供游戏等待。测试 `sdk/tests/terrain-surface.test.ts`(含真实 Jolt:射线跨块缝与 `heightAt` 吻合到 1e-3、落球静止在地面上)。后续已定:不用 Jolt HeightFieldShape(按块 8 位量化样本、对角线方向固定,会破坏"查询 = 碰撞 = 所画三角面";网格分块每块约 37KB,内存不是瓶颈);远处无运动物体时用 `Terrains.raycast` 对同一组三角面精确求交(测试中与 Jolt 射线逐点一致);分块驻留已归 T-03 的 `ProceduralChunks`。
- 验收:机器人离地高度稳定;建筑四角支撑不悬空;LOD 切换、跨块、重载不掉落;掉落物、障碍、设备有真实碰撞。
### T-03 程序化区块的驻留与统一预算
diff --git a/docs/astro/src/content/docs/graphics/terrain.mdx b/docs/astro/src/content/docs/graphics/terrain.mdx
index ddbef62c2..664ce78fd 100644
--- a/docs/astro/src/content/docs/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/graphics/terrain.mdx
@@ -143,8 +143,12 @@ that moves and every character: the tile under one at once, the rest a few a fra
Each tile is a static mesh body owned by the terrain entity, so a ray or a contact that hits the ground reports the terrain. While a
terrain is rebuilt its old tiles stay until the new ones replace them, so nothing standing on it falls.
`terrains.collisionReadyAt(x, z)` says whether the ground under a point is built, for a game to wait on before it drops something
-there (a provider that answers later builds a tile once its samples are in). Far from anything that moves there is nothing to hit:
-ask `heightAt` there instead.
+there (a provider that answers later builds a tile once its samples are in). Far from anything that moves there is nothing in the
+physics world to hit; `terrains.raycast(origin, direction, maxDistance)` meets the same triangles anywhere, with no physics, for a
+long line of sight, a pick or a landing spot.
+
+The tiles are meshes rather than a compressed heightfield shape on purpose: a heightfield shape quantizes its samples and cuts its
+cells its own way, and the ground that is collided with would no longer be the ground that is drawn and queried.
The tiles go through the `CollisionGeometry` resource (`esengine`), which any system that makes static geometry as the game runs can
use; the 3D physics world keeps one static body for each piece in it.
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
index 8c8f37cf0..57c9a2f8d 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
@@ -98,7 +98,9 @@ const n = terrains.normalAt(x, z); // 那里的单位法线,或 null
### 碰撞
-打开 `collision` 后,地面以 32 格为一块,在每个运动的 3D 刚体和每个角色周围 `collisionDistance` 内为 3D 物理世界构建:它脚下那块立刻构建,其余每帧几块、由近及远,没有东西靠近后释放。每块是属于地形实体的静态网格刚体,所以射线或接触打到地面时报告的是地形实体。地形重建期间旧块保留到新块替换它们,站在上面的东西不会掉下去。`terrains.collisionReadyAt(x, z)` 说明某点下的地面是否已构建好,游戏可以等它再往那里放东西(稍后才应答的生成器在样本到达后才构建那块)。离任何运动物体都远的地方没有可碰的东西,那里请用 `heightAt`。
+打开 `collision` 后,地面以 32 格为一块,在每个运动的 3D 刚体和每个角色周围 `collisionDistance` 内为 3D 物理世界构建:它脚下那块立刻构建,其余每帧几块、由近及远,没有东西靠近后释放。每块是属于地形实体的静态网格刚体,所以射线或接触打到地面时报告的是地形实体。地形重建期间旧块保留到新块替换它们,站在上面的东西不会掉下去。`terrains.collisionReadyAt(x, z)` 说明某点下的地面是否已构建好,游戏可以等它再往那里放东西(稍后才应答的生成器在样本到达后才构建那块)。离任何运动物体都远的地方,物理世界里没有可碰的东西;`terrains.raycast(origin, direction, maxDistance)` 不经物理、在任何地方都与同一组三角面求交,可用于远距离视线、拾取或寻找落点。
+
+碰撞块用网格而不用压缩的高度场形状是有意的:高度场形状会量化样本、按自己的方式切分格子,碰撞的地面就不再是画出来、查询到的那片地面。
这些块经由 `CollisionGeometry` 资源(`esengine`)交出,任何在游戏运行中生成静态几何的系统都可以用它;3D 物理世界为其中每一片保留一个静态刚体。
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 2072466b6..444bfbd63 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -10087,6 +10087,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 87baa0230..cf12abf8a 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -10300,6 +10300,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index dba18518b..fae35eac4 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -10082,6 +10082,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 0c38b8292..4fd385477 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -10375,6 +10375,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/etc/terrain.api.md b/sdk/etc/terrain.api.md
index 9c852ecd7..76c2d8c89 100644
--- a/sdk/etc/terrain.api.md
+++ b/sdk/etc/terrain.api.md
@@ -284,6 +284,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index bda408a5c..b4492e306 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -10089,6 +10089,7 @@ heightAt: (x: number, z: number) => number | null
loading: boolean
normalAt: (x: number, z: number) => [number, number, number] | null
pending: number
+raycast: (origin: { x: number; y: number; z: number; }, direction: { x: number; y: number; z: number; }, maxDistance: number) => { distance: number; point: { x: number; y: number; z: number; }; normal: { x: number; y: number; z: number; }; entity: Entity; } | null
repaint: () => void
setPaint: (paint: Repaint | null) => void
status: (entity: Entity) => TerrainStatus | null
diff --git a/sdk/src/terrain/surface.ts b/sdk/src/terrain/surface.ts
index 6967ba819..253b0c98a 100644
--- a/sdk/src/terrain/surface.ts
+++ b/sdk/src/terrain/surface.ts
@@ -80,6 +80,41 @@ export class TerrainSurface {
return { x0: tx * n * this.cell, z0: tz * n * this.cell, positions, indices };
}
+ /**
+ * Where the ray from (ox, oy, oz) along (dx, dy, dz) first meets the ground, within `maxDistance` (along the direction, normalised):
+ * the distance, the point and the triangle's normal; null where it meets none (or crosses ground not known yet). Exact against the
+ * triangles: it walks the lattice cell by cell under the ray and meets each cell's two.
+ */
+ raycast(ox: number, oy: number, oz: number, dx: number, dy: number, dz: number, maxDistance: number):
+ { distance: number; point: [number, number, number]; normal: [number, number, number] } | null {
+ const len = Math.hypot(dx, dy, dz);
+ if (len <= 0) return null;
+ dx /= len; dy /= len; dz /= len;
+ const c = this.cell;
+ // Over the cells the ray's shadow on the ground crosses, in order (Amanatides and Woo).
+ let i = Math.floor(ox / c), k = Math.floor(oz / c);
+ const si = dx > 0 ? 1 : -1, sk = dz > 0 ? 1 : -1;
+ const tdi = dx !== 0 ? Math.abs(c / dx) : Infinity, tdk = dz !== 0 ? Math.abs(c / dz) : Infinity;
+ let ti = dx !== 0 ? ((dx > 0 ? (i + 1) * c : i * c) - ox) / dx : Infinity;
+ let tk = dz !== 0 ? ((dz > 0 ? (k + 1) * c : k * c) - oz) / dz : Infinity;
+ let t = 0;
+ for (let steps = 0; steps < 1_000_000 && t <= maxDistance; steps++) {
+ const a = this.node(i, k), b = this.node(i + 1, k), cc = this.node(i, k + 1), d = this.node(i + 1, k + 1);
+ if (a === null || b === null || cc === null || d === null) return null;
+ const x0 = i * c, z0 = k * c;
+ // The cell's two triangles, cut from its (1, 0) corner to its (0, 1) corner, as the patch is.
+ for (const tri of [[[x0, a, z0], [x0, cc, z0 + c], [x0 + c, b, z0]], [[x0 + c, b, z0], [x0, cc, z0 + c], [x0 + c, d, z0 + c]]] as const) {
+ const hit = rayTriangle(ox, oy, oz, dx, dy, dz, tri[0], tri[1], tri[2]);
+ if (hit && hit.t <= maxDistance && this.covers(ox + dx * hit.t, oz + dz * hit.t)) {
+ return { distance: hit.t, point: [ox + dx * hit.t, oy + dy * hit.t, oz + dz * hit.t], normal: hit.n };
+ }
+ }
+ if (ti < tk) { t = ti; ti += tdi; i += si; } else { t = tk; tk += tdk; k += sk; }
+ if (dx === 0 && dz === 0) break;
+ }
+ return null;
+ }
+
/** The four corners of the cell under (x, z) and where in it the point is, or null. */
private corners(x: number, z: number): [number, number, number, number, number, number] | null {
if (!this.covers(x, z)) return null;
@@ -90,3 +125,26 @@ export class TerrainSurface {
return [a, b, c, d, u - i, v - k];
}
}
+
+/** Where a ray (unit direction) meets a triangle from either side, and the triangle's upward normal; null where it misses. */
+function rayTriangle(ox: number, oy: number, oz: number, dx: number, dy: number, dz: number,
+ p0: readonly number[], p1: readonly number[], p2: readonly number[]): { t: number; n: [number, number, number] } | null {
+ const e1x = p1[0]! - p0[0]!, e1y = p1[1]! - p0[1]!, e1z = p1[2]! - p0[2]!;
+ const e2x = p2[0]! - p0[0]!, e2y = p2[1]! - p0[1]!, e2z = p2[2]! - p0[2]!;
+ const px = dy * e2z - dz * e2y, py = dz * e2x - dx * e2z, pz = dx * e2y - dy * e2x;
+ const det = e1x * px + e1y * py + e1z * pz;
+ if (Math.abs(det) < 1e-12) return null;
+ const inv = 1 / det;
+ const sx = ox - p0[0]!, sy = oy - p0[1]!, sz = oz - p0[2]!;
+ const u = (sx * px + sy * py + sz * pz) * inv;
+ if (u < -1e-9 || u > 1 + 1e-9) return null;
+ const qx = sy * e1z - sz * e1y, qy = sz * e1x - sx * e1z, qz = sx * e1y - sy * e1x;
+ const v = (dx * qx + dy * qy + dz * qz) * inv;
+ if (v < -1e-9 || u + v > 1 + 1e-9) return null;
+ const t = (e2x * qx + e2y * qy + e2z * qz) * inv;
+ if (t < 0) return null;
+ let nx = e1y * e2z - e1z * e2y, ny = e1z * e2x - e1x * e2z, nz = e1x * e2y - e1y * e2x;
+ if (ny < 0) { nx = -nx; ny = -ny; nz = -nz; }
+ const l = Math.hypot(nx, ny, nz) || 1;
+ return { t, n: [nx / l, ny / l, nz / l] };
+}
diff --git a/sdk/src/terrain/terrainPlugin.ts b/sdk/src/terrain/terrainPlugin.ts
index b53c1cdd3..9055cf18b 100644
--- a/sdk/src/terrain/terrainPlugin.ts
+++ b/sdk/src/terrain/terrainPlugin.ts
@@ -272,6 +272,32 @@ export class Terrains {
return true;
}
+ /**
+ * Where a ray meets the ground, anywhere and whether or not anything collides there: exact against the same triangles collision and
+ * `heightAt` use. `maxDistance` is along the direction. Null where it meets no terrain within it.
+ */
+ raycast(origin: { x: number; y: number; z: number }, direction: { x: number; y: number; z: number }, maxDistance: number):
+ { distance: number; point: { x: number; y: number; z: number }; normal: { x: number; y: number; z: number }; entity: Entity } | null {
+ const world = this.app_?.world;
+ if (!world) return null;
+ let best: { distance: number; point: { x: number; y: number; z: number }; normal: { x: number; y: number; z: number }; entity: Entity } | null = null;
+ for (const [entity, built] of this.built_) {
+ if (!built.surface || !world.valid(entity)) continue;
+ const t = world.tryGet(entity, Transform) as TransformData | null;
+ if (!t) continue;
+ const at = t.worldPosition;
+ const hit = built.surface.raycast(origin.x - at.x, origin.y - at.y, origin.z - at.z, direction.x, direction.y, direction.z,
+ best ? best.distance : maxDistance);
+ if (!hit) continue;
+ best = {
+ distance: hit.distance, entity,
+ point: { x: hit.point[0] + at.x, y: hit.point[1] + at.y, z: hit.point[2] + at.z },
+ normal: { x: hit.normal[0], y: hit.normal[1], z: hit.normal[2] },
+ };
+ }
+ return best;
+ }
+
/** Whether the ground under (x, z) is built for collision: what a game waits on before it drops something there. */
collisionReadyAt(x: number, z: number): boolean {
const app = this.app_;
diff --git a/sdk/tests/terrain-surface.test.ts b/sdk/tests/terrain-surface.test.ts
index 3a8f9c596..ee00887a1 100644
--- a/sdk/tests/terrain-surface.test.ts
+++ b/sdk/tests/terrain-surface.test.ts
@@ -93,6 +93,30 @@ describe('the surface', () => {
});
});
+describe('a ray against the surface', () => {
+ const surface = new TerrainSurface(new NoiseSource(params), 2);
+
+ it('meets the ground where heightAt says it is, straight down or slanting, and faces the way normalAt does', () => {
+ for (const [x, z] of [[3.3, 7.9], [-120.5, 44.25], [999.1, -12.6]] as const) {
+ const down = surface.raycast(x, 500, z, 0, -1, 0, 1000)!;
+ expect(down.point[1]).toBeCloseTo(surface.height(x, z)!, 6);
+ const slant = surface.raycast(x, 300, z, 0.4, -1, 0.3, 2000)!;
+ expect(slant.point[1]).toBeCloseTo(surface.height(slant.point[0], slant.point[2])!, 5);
+ const n = surface.normal(slant.point[0], slant.point[2])!;
+ for (let c = 0; c < 3; c++) expect(slant.normal[c]).toBeCloseTo(n[c]!, 3);
+ }
+ });
+
+ it('finds a hillside far along a low ray, and nothing past its reach or pointing at the sky', () => {
+ const ground = surface.height(0, 0)!;
+ const far = surface.raycast(0, ground + 1, 0, 1, 0, 0.2, 20000);
+ expect(far).not.toBeNull();
+ expect(far!.distance).toBeGreaterThan(1);
+ expect(surface.raycast(0, ground + 1, 0, 1, 0, 0.2, 0.5)).toBeNull();
+ expect(surface.raycast(0, ground + 1, 0, 0, 1, 0, 1000)).toBeNull();
+ });
+});
+
describe('collision tiles', () => {
const boot = (): App => {
setDefaultContext(new AppContext());
@@ -219,6 +243,23 @@ describe.skipIf(!HAS_WASM || !hasSideModule('physics3d'))('the ground in the 3D
}
});
+ it('a ray through the physics world and a ray against the surface meet the ground at the same point', async () => {
+ const { app } = await world();
+ const e = app.world.spawn();
+ app.world.insert(e, Transform, { position: { x: 30, y: 80, z: 30 } });
+ app.world.insert(e, RigidBody3D, { bodyType: 1 });
+ await app.stepFrames(6, 1 / 60);
+ const physics = app.getResource(Physics3D)!, terrains = app.getResource(TerrainsRes);
+ for (const [x, z] of [[30, 30], [41.3, 17.8]] as const) {
+ const origin = { x, y: 200, z }, dir = { x: 0.2, y: -1, z: 0.1 };
+ const viaPhysics = physics.raycast(origin, { x: dir.x * 400, y: dir.y * 400, z: dir.z * 400 })!;
+ const viaSurface = terrains.raycast(origin, dir, 1000)!;
+ expect(viaSurface.point.x).toBeCloseTo(viaPhysics.x, 2);
+ expect(viaSurface.point.y).toBeCloseTo(viaPhysics.y, 2);
+ expect(viaSurface.point.z).toBeCloseTo(viaPhysics.z, 2);
+ }
+ });
+
it('a ball dropped on it comes to rest on it, not through it', async () => {
const { app } = await world();
const terrains = app.getResource(TerrainsRes);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 867c00e92..5a683507c 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2788,4 +2788,6 @@ export const NOTED = {
{"note":"Rain and storms on water."},
"fix(water): water casts no shadow, a lake holds a river that runs into it, and the surf's slope fades on the ridge between shores":
{"note":"A river runs into a lake cleanly, and water casts no shadow."},
+ "feat(terrain): a ray meets the ground's triangles anywhere, with no physics and no collision tile there":
+ {"note":"Rays against the terrain, anywhere."},
};