WIP: feat(modapi): world shapes: sphere, capsule and box casts and overlaps through the game's own shape query #2438

Closed
ChloeNow wants to merge 6 commits from ChloeNow/Longshot:feat/modapi-world-shapes into mod-api
Contributor

Depends on #2422 (world.ray and rayAll). This branch is stacked on it: review only the commit after
76da61dd (ab936bff). Once #2422 merges, this rebases onto mod-api with git rebase --onto origin/mod-api 76da61dd
and the base stays mod-api.

Summary

#2422 gives mods rays. This adds what other engines offer next to them: shapes swept through the world and shapes
placed in it, against the game's own collision. Unity's SphereCast/CapsuleCast/BoxCast, their Alls and
OverlapSphere/Capsule/Box; Unreal's sweeps and overlaps; Godot's cast_motion and intersect_shape.

  • Casts: sphereCast(from, to, radius, options?), capsuleCast(from, to, radius, height, options?) and
    boxCast(from, to, halfExtents, options?): the nearest thing the shape touches as its centre goes from from to
    to. Each has an All (sphereCastAll and so on): everything it touches on the way, nearest first, each thing
    once, at most 16.
  • Overlaps: overlapSphere(center, radius, options?), overlapCapsule(center, radius, height, options?),
    overlapBox(center, halfExtents, options?): what the shape touches where it is now. Empty means the place is clear.
  • The rays' options (RayOptions): the same kinds (ground, objects, characters, water on; Link off) and ignore.
  • What it touched (ShapeHit, new): the point on the surface, its normal, the shape's centre at that moment (where
    it can go and no further), the distance its centre went, the kind, the actor's key, and inside (touching at the
    start; every hit of an overlap).
  • Capsules stand upright (along the world's up, as Link's body does); boxes lie along the world's axes. Rotated shapes
    are not offered (the game's query has a rotation; a later change can expose it).

How it's built, in short: the game has its own shape query. Its AI builds one on the stack for checks like
QueryPhysicsCastShapeEntitySphere (f_82ac68d0), and about 90 other places do the same. The host builds it the same
way: the game's constructor, the game's class for each shape (its destructor checked against the build's), and the
game's own builder for the shape (sphere f_803bd550, capsule f_80d1eba0, box f_8047e6f0), which puts a Havok
convex shape in the query's own arena, so nothing goes on the heap. It runs with f_805efbc0 (Havok's castShape, or
its closest points for an overlap) under the world's lock, taken exactly as for a ray (#2422). Every hit comes from the
one query's collector: no extra queries per hit. A hit's kind is the hit body's own collision layer, so shapes need no
second query to classify, unlike rays. An actor with several bodies (Link has two) is one hit, by the collision group
its bodies share.

game_call_more gains float arguments (s0 to s3) for the builders' radius.

Read first: the shapes block of host/game/world.h (what was read of the game), then shape_query and shape_cast in
host/game/world.c, then the new "Shapes" section of docs/dev/reference/world.md.

Testing

Tested: Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU, driver 621.24, the save kokiri-start, through
dev.py game-tests, on #2422's branch with this change (and the other open mod-api PRs, which loading a save needs):

  • A validation mod in Link's house in Kokiri Forest, 26 of 26 checks passing:
    • A sphere (0.3 m) swept down stops with its centre 0.30 m above the floor, normal straight up. A capsule (0.35 m,
      1.8 m tall) stops 0.90 m above it, a box (half height 0.2 m) 0.20 m above it.
    • With player: true, Link is hit first, once, named by his key; with ignore set to his key the sweep passes
      through him to the floor.
    • The Alls give Link, the floor and the ground under the house, each at its own distance, nearest first.
    • Overlaps: at Link's feet, the floor; at his chest, only Link (with player: true); in the air, nothing; a capsule
      where he stands, nothing; the same capsule sunk 0.3 m, the floor; a box at his feet, the floor.
    • A Keese spawned in front of him: a sphere onto it hits it as character with its key; an overlap on it gives it;
      characters: false leaves it out.
    • Swept out in eight directions at chest height, a sphere (0.4 m) always stopped short of a ray along the same line.
    • A sweep of no length in the air: a miss.
    • Then 20 sphere casts a frame for 30 frames: no stall, no crash, no busy lock.
  • The repository's tests/game-features/world: the new fake tests pass (a sphere onto the ground, an overlap at
    Link's feet, has() false without the shapes); the new game test (a sphere onto the floor at Link's feet) is in the
    file.
  • Game-free: tests/game-features/ in the fake game (19 areas); tests/test_game_call.py (the new float arguments);
    tests/test_mod_host_features.py (the nine features answer); modapi/generate.py --check.

The validation mod ran on this code with a temporary trace of the raw query (removed before the commit, along with a
per-call timer). The final code was built, but the game wasn't run again: the removed lines only logged.

Still to test:

  • The new game test in tests/game-features/world through dev.py game-tests (not yet run on its own).
  • Linux: mise run test, including tests/test_modrt.py (its stand-in's game_world_shape_cast over the floor,
    water and character planes; new checks for each shape onto the floor, a miss, the Alls, the overlaps, sizes out of
    range refused, and a build without the shapes). Not run here: the fixture links the POSIX way (by hand, or CI).
  • Other GPUs and Linux in the game: the query is the game's own CPU code, the same on every PC.

Measurements

Verdict: a shape costs about 140 µs on the main thread on average, a little more than a ray; nothing runs when no
mod casts.

Setup: Windows 11, RTX 4060 Laptop GPU, driver 621.24; HOS_RAY_TRACE=1's counts over the validation run (600
requests, most of them the 20-a-frame sphere casts), and a per-call timer in the run before the commit.

a shape, on average about 140 µs
20 sphere casts in one frame about 3 ms of the mod's frame (the runtime warns past 2 ms, faults past 8 ms)
the first calls of a run, and a few boxes 1 to 8 ms (the slowest, a box, 7.8 ms)
busy at the first try 0 in 600

The schema's doc for sphereCast tells mod authors the cost (about 0.15 ms each; ten a frame is about 1.5 ms).

Risks

  • The query's layout is read from the game's code, not a header: the constructor's fields, the classes, the
    builders, the collector's contact. Each was confirmed in the runs (a raw dump of the query and its contacts, since
    removed), and the build-specific functions are checked before use (game_world_shape_ready: the ray's checks, plus
    the shape query's functions in the lift; each class's destructor against the build's). Any other build keeps
    has("world.sphereCast") and the rest false. HOS_WORLD_RAY=0 turns them off with the rays.
  • The lock is #2422's, unchanged: the shape query's own shared lock (inlined in f_804ea3e0) nests on the host's
    hold the same way the ray executor's does.
  • The query lives below the main thread's guest stack (0x1310 bytes), as the ray's does (0x180), and the calls run
    below it. The game's own callers keep the same object on their stacks.
  • A contact's other side (the flag's bit 0 clear) wasn't seen in the runs; the host follows the game's own reader
    (f_80aeb9e0, f_805fd650) for it.
  • Merging an actor's bodies by collision group leaves the ground and water alone (their bodies may share one).
    Two actors sharing a group would show as one hit; none was seen.
  • Pre-existing, not from this change: as in #2422 (files.*, content.change in test_mod_host_features.py), and
    on Windows tests/test_mod_tools.py (5 path and symlink tests) and the link-hud example's input test, which fails
    the same on 76da61dd.

Checklist

Ready for review

  • One topic, on current mod-api (stacked on #2422); the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on mod-api is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch has its row in gpu/vk/switches.def; its default ships on. (no new switch: HOS_WORLD_RAY and HOS_RAY_TRACE cover the shapes)
  • A check under tests/ that reads changed source is updated, not weakened. (tests/modrt_test.c's stand-in, test_mod_host_features.py, game_call_test.c, the fake game)
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • #2422 merged, and this rebased onto mod-api.
  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

> **Depends on #2422** (`world.ray` and `rayAll`). This branch is stacked on it: review only the commit after > `76da61dd` (ab936bff). Once #2422 merges, this rebases onto `mod-api` with `git rebase --onto origin/mod-api 76da61dd` > and the base stays `mod-api`. ## Summary #2422 gives mods rays. This adds what other engines offer next to them: shapes swept through the world and shapes placed in it, against the game's own collision. Unity's `SphereCast`/`CapsuleCast`/`BoxCast`, their `All`s and `OverlapSphere`/`Capsule`/`Box`; Unreal's sweeps and overlaps; Godot's `cast_motion` and `intersect_shape`. - **Casts:** `sphereCast(from, to, radius, options?)`, `capsuleCast(from, to, radius, height, options?)` and `boxCast(from, to, halfExtents, options?)`: the nearest thing the shape touches as its centre goes from `from` to `to`. Each has an **`All`** (`sphereCastAll` and so on): everything it touches on the way, nearest first, each thing once, at most 16. - **Overlaps:** `overlapSphere(center, radius, options?)`, `overlapCapsule(center, radius, height, options?)`, `overlapBox(center, halfExtents, options?)`: what the shape touches where it is now. Empty means the place is clear. - **The rays' options** (`RayOptions`): the same kinds (ground, objects, characters, water on; Link off) and `ignore`. - **What it touched** (`ShapeHit`, new): the point on the surface, its normal, the shape's centre at that moment (where it can go and no further), the distance its centre went, the `kind`, the actor's key, and `inside` (touching at the start; every hit of an overlap). - Capsules stand upright (along the world's up, as Link's body does); boxes lie along the world's axes. Rotated shapes are not offered (the game's query has a rotation; a later change can expose it). How it's built, in short: the game has its own shape query. Its AI builds one on the stack for checks like `QueryPhysicsCastShapeEntitySphere` (`f_82ac68d0`), and about 90 other places do the same. The host builds it the same way: the game's constructor, the game's class for each shape (its destructor checked against the build's), and the game's own builder for the shape (sphere `f_803bd550`, capsule `f_80d1eba0`, box `f_8047e6f0`), which puts a Havok convex shape in the query's own arena, so nothing goes on the heap. It runs with `f_805efbc0` (Havok's `castShape`, or its closest points for an overlap) under the world's lock, taken exactly as for a ray (#2422). Every hit comes from the one query's collector: no extra queries per hit. A hit's kind is the hit body's own collision layer, so shapes need no second query to classify, unlike rays. An actor with several bodies (Link has two) is one hit, by the collision group its bodies share. `game_call_more` gains float arguments (`s0` to `s3`) for the builders' radius. Read first: the shapes block of `host/game/world.h` (what was read of the game), then `shape_query` and `shape_cast` in `host/game/world.c`, then the new "Shapes" section of docs/dev/reference/world.md. ## Testing **Tested:** Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU, driver 621.24, the save `kokiri-start`, through `dev.py game-tests`, on #2422's branch with this change (and the other open mod-api PRs, which loading a save needs): - A validation mod in Link's house in Kokiri Forest, 26 of 26 checks passing: - A sphere (0.3 m) swept down stops with its centre 0.30 m above the floor, normal straight up. A capsule (0.35 m, 1.8 m tall) stops 0.90 m above it, a box (half height 0.2 m) 0.20 m above it. - With `player: true`, Link is hit first, once, named by his key; with `ignore` set to his key the sweep passes through him to the floor. - The `All`s give Link, the floor and the ground under the house, each at its own distance, nearest first. - Overlaps: at Link's feet, the floor; at his chest, only Link (with `player: true`); in the air, nothing; a capsule where he stands, nothing; the same capsule sunk 0.3 m, the floor; a box at his feet, the floor. - A Keese spawned in front of him: a sphere onto it hits it as `character` with its key; an overlap on it gives it; `characters: false` leaves it out. - Swept out in eight directions at chest height, a sphere (0.4 m) always stopped short of a ray along the same line. - A sweep of no length in the air: a miss. - Then 20 sphere casts a frame for 30 frames: no stall, no crash, no busy lock. - The repository's `tests/game-features/world`: the new fake tests pass (a sphere onto the ground, an overlap at Link's feet, `has()` false without the shapes); the new game test (a sphere onto the floor at Link's feet) is in the file. - Game-free: `tests/game-features/` in the fake game (19 areas); `tests/test_game_call.py` (the new float arguments); `tests/test_mod_host_features.py` (the nine features answer); `modapi/generate.py --check`. The validation mod ran on this code with a temporary trace of the raw query (removed before the commit, along with a per-call timer). The final code was built, but the game wasn't run again: the removed lines only logged. **Still to test:** - The new game test in `tests/game-features/world` through `dev.py game-tests` (not yet run on its own). - Linux: `mise run test`, including `tests/test_modrt.py` (its stand-in's `game_world_shape_cast` over the floor, water and character planes; new checks for each shape onto the floor, a miss, the `All`s, the overlaps, sizes out of range refused, and a build without the shapes). Not run here: the fixture links the POSIX way (by hand, or CI). - Other GPUs and Linux in the game: the query is the game's own CPU code, the same on every PC. ## Measurements **Verdict:** a shape costs about 140 µs on the main thread on average, a little more than a ray; nothing runs when no mod casts. Setup: Windows 11, RTX 4060 Laptop GPU, driver 621.24; `HOS_RAY_TRACE=1`'s counts over the validation run (600 requests, most of them the 20-a-frame sphere casts), and a per-call timer in the run before the commit. | | | |---|---| | a shape, on average | about 140 µs | | 20 sphere casts in one frame | about 3 ms of the mod's frame (the runtime warns past 2 ms, faults past 8 ms) | | the first calls of a run, and a few boxes | 1 to 8 ms (the slowest, a box, 7.8 ms) | | busy at the first try | 0 in 600 | The schema's doc for `sphereCast` tells mod authors the cost (about 0.15 ms each; ten a frame is about 1.5 ms). ## Risks - **The query's layout is read from the game's code**, not a header: the constructor's fields, the classes, the builders, the collector's contact. Each was confirmed in the runs (a raw dump of the query and its contacts, since removed), and the build-specific functions are checked before use (`game_world_shape_ready`: the ray's checks, plus the shape query's functions in the lift; each class's destructor against the build's). Any other build keeps `has("world.sphereCast")` and the rest false. `HOS_WORLD_RAY=0` turns them off with the rays. - **The lock** is #2422's, unchanged: the shape query's own shared lock (inlined in `f_804ea3e0`) nests on the host's hold the same way the ray executor's does. - **The query lives below the main thread's guest stack** (0x1310 bytes), as the ray's does (0x180), and the calls run below it. The game's own callers keep the same object on their stacks. - **A contact's other side** (the flag's bit 0 clear) wasn't seen in the runs; the host follows the game's own reader (`f_80aeb9e0`, `f_805fd650`) for it. - **Merging an actor's bodies by collision group** leaves the ground and water alone (their bodies may share one). Two actors sharing a group would show as one hit; none was seen. - **Pre-existing, not from this change**: as in #2422 (`files.*`, `content.change` in `test_mod_host_features.py`), and on Windows `tests/test_mod_tools.py` (5 path and symlink tests) and the `link-hud` example's input test, which fails the same on `76da61dd`. ## Checklist ### Ready for review - [x] One topic, on current `mod-api` (stacked on #2422); the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `mod-api` is named). <!-- Windows parts run; the Linux half is CI's --> - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A switch has its row in `gpu/vk/switches.def`; its default ships on. (no new switch: `HOS_WORLD_RAY` and `HOS_RAY_TRACE` cover the shapes) - [x] A check under `tests/` that reads changed source is updated, not weakened. (`tests/modrt_test.c`'s stand-in, `test_mod_host_features.py`, `game_call_test.c`, the fake game) - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. <!-- Windows built and played; Linux: CI --> - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] #2422 merged, and this rebased onto `mod-api`. - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(modapi): world.ray and rayAll: the game's own ray query, what it hits by kind, which actor it hit
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 5s
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled
76da61dd95
longshot/world's ray was in the schema with no game build answering it. It now casts through the game's own
synchronous ray query (f_80e86730), built as the game builds its own inline (f_80e865e0's layout), under the physics
world's reader/writer lock taken as the game's own reader would take it but never with a wait that could hang: a
compare-and-swap while others read, or under nn::os TryLockMutex from 0; busy, 50 us of retries, then once a frame a
1 ms sleep with the core given up (no CPU of the mod's), then null. The game's own unlock lets the hold go.

What a ray hits is chosen by kind (RayOptions: ground, objects, characters, player, water), the game's collision layers
read from the binary and checked one bit at a time in runs; by default everything but Link, so a ray cast from him does
not stop on him. ignore passes through an actor's collision group (f_803f2dd0, f_80e86ac0). A hit says its kind (the
same ray against one kind at a time), the key of the actor whose body it is (a rigid body's handle, [[body+0x78]+8],
matched to the listed actors' bodies; for a mod with actors.read) and whether the ray started inside it. rayAll gives
every hit along the ray, each body once. The game's invisible walls are no kind: no ray query meets them (a run swept
every layer, sub-layer and ray layer in both of the manager's worlds).

has("world.ray") and has("world.rayAll") are true in build e0f3ccfa; HOS_WORLD_RAY=0 turns them off. In the game
(more than 100,000 casts across map changes): no stall; about 60 us a cast, 80 to 100 us a ray with its kind; busy
about once in 500 requests, slept on 4 times in 8,400, one null. The checks: the ground at Link's feet, Link hit and
named, passed through with ignore, a spawned Keese hit and named, passed with characters off, rayAll through it, a
door an object with its key, Lake Hylia's surface water and the bed under it with water off.
docs/dev/reference/world.md has the lock, the layers, the runs and what is not offered (shape casts, the walls).

The fake game (mod-tools) filters a test's hits by the options as the game does, and has rayAll. The generator now
emits the list type for a nullable list result (list<RayHit>?). Tests: test_modrt.py's world checks over the stand-in's
floor, water and a character (kinds, keys, ignore, rayAll, a mod without actors.read); test_mod_host_features.py no
longer lists world.ray as absent and gets the hos_game_is_mkw stub services_game.c already called; the world feature
suite's game test checks the floor at Link's feet, its fake tests the kinds and rayAll.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
feat(modapi): world shapes: sphere, capsule and box casts and overlaps through the game's own shape query
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 9s
Website / Website on Cloudflare (pull_request_target) Has been skipped
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled
ab936bff00
longshot/world gains sphereCast, capsuleCast and boxCast (each with an All giving every hit, nearest first) and
overlapSphere, overlapCapsule and overlapBox: a sphere, an upright capsule or a box along the world's axes, swept from
one place to another or put at one, with the rays' RayOptions (kinds, ignore). A hit (ShapeHit) says where it touched,
the normal, where the shape's centre was then, how far it went, its kind, its actor's key and inside.

They run the game's own shape query, built as its AI's sphere query builds one on its stack (f_82ac68d0): constructed by
f_80a65640, given the shape's class (its destructor checked against the build's), the shape built in place by the
game's builder for it (sphere f_803bd550, capsule f_80d1eba0, box f_8047e6f0: Havok convex shapes in the query's own
arena), the same collision filter as the ray's, run by f_805efbc0 (Havok's castShape, or its closest points for an
overlap) under the world's lock taken as for a ray. Every hit comes from the one query's collector: each contact's
point, normal and fraction of the sweep; each body's nearest kept, an actor's several bodies once (their collision
group); a hit's kind its body's own layer, with no second query. game_call_more gains float arguments (s0 to s3) for
the builders' radius.

In the game (build e0f3ccfa, Kokiri Forest, two probe runs): 26 of 26 checks: each shape onto the floor stops its
extent above it; Link hit once with player: true, named, passed with ignore; the Alls give each hit its own distance;
overlaps at the feet, the chest, in the air and on a spawned Keese; a sphere always stops short of a ray along the same
line. About 140 us a shape on average. The fake game gains allowShapes; the feature suite, modrt's stand-in and
docs/dev/reference/world.md cover them.

Depends on the world.ray change (feat/modapi-world-ray).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Superseded by #2663: sphere, capsule and box casts and overlaps, made again on the Rust mod API's step-thread rays (stacked on #2662). Closing this C one.

Superseded by #2663: sphere, capsule and box casts and overlaps, made again on the Rust mod API's step-thread rays (stacked on #2662). Closing this C one.
ChloeNow closed this pull request 2026-11-27 11:44:25 +00:00
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 9s
Website / Website on Cloudflare (pull_request_target) Has been skipped
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2438
No description provided.