Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Camera clip treats a non-finite ground height as 0, document rig limits (#772)

Nguyen Xuan An682578b90371
diff --git a/changes/772-camera-clip-nan.md b/changes/772-camera-clip-nan.md
new file mode 100644
index 00000000..d9c78297
--- /dev/null
+++ b/changes/772-camera-clip-nan.md
@@ -0,0 +1 @@
+- `createCameraClip` treats a ground function that returns a non-finite height as ground height 0, so the camera still stays above the floor instead of clipping being switched off (#772).
diff --git a/docs/player.md b/docs/player.md
index bc299f0b..380ec679 100644
--- a/docs/player.md
+++ b/docs/player.md
@@ -83,7 +83,7 @@ Both read the same input names as the character controller, use the same `mover`
### Camera collision
-`createCameraClip({ obstacles, ground, padding })` returns a `clipEye(target, eye)` for any orbiting rig. It pulls the eye along the line to the target until it is `padding` (0.25) outside every obstacle box and above the ground, so the camera never ends up inside a wall or under the floor. A body pressed within `padding` of a wall leaves the camera at the target.
+`createCameraClip({ obstacles, ground, padding })` returns a `clipEye(target, eye)` for any orbiting rig. It pulls the eye along the line to the target until it is `padding` (0.25) outside every obstacle box and above the ground, so the camera never ends up inside a wall or under the floor. A body pressed within `padding` of a wall leaves the camera at the target. When the target itself is inside a padded box, the eye collapses onto the target. A ground function that returns a non-finite height counts as height 0.
```js
const clip = createCameraClip({ obstacles, ground });
@@ -94,7 +94,7 @@ const camera = createChaseCamera({ subject: car.state, clipEye: clip });
`registerController(kind, factory)` and `registerCameraRig(kind, factory)` add a kind (up to 64 names, built-ins cannot be replaced). A controller factory returns a runtime system with `state.position` and a `fixedUpdate` or `update` function; a rig factory returns a system with `subject` and `pose(context) => { eye, target, up }`. `createController(kind, options)` and `createCameraRigOfKind(kind, options)` build one by name; `listControllerKinds()` and `listCameraRigKinds()` list them (`character`, `vehicle`, `flight`; `follow`, `firstPerson`, `topDown`, `orbit`, `chase`, `sideScroller`).
-`replaceController(runtime, name, kind, options)` swaps the controller system for another kind at the same position and yaw. `replaceCameraRig(runtime, name, kind, options)` swaps the rig and keeps its subject and yaw. After a controller swap, point the rig at the new `state`:
+`replaceController(runtime, name, kind, options)` swaps the controller system for another kind at the same position and yaw. `replaceCameraRig(runtime, name, kind, options)` swaps the rig and keeps its subject and yaw but not its input: pass `input` again in `options` if the new rig reads it. After a controller swap, point the rig at the new `state`:
```js
const car = replaceController(runtime, 'player', 'vehicle', { input });
diff --git a/src/player.js b/src/player.js
index eaf34591..3c98a85d 100644
--- a/src/player.js
+++ b/src/player.js
@@ -449,6 +449,8 @@ export function createSideScrollerCamera(options = {}) {
// A clipEye for any orbiting rig: pulls the camera in along the line from its target so the eye stays `padding` outside every
// obstacle box and above the ground, and so never shows the inside of a wall.
+const FALLBACK_GROUND_HEIGHT = 0;
+
export function createCameraClip(options = {}) {
const owner = 'createCameraClip';
const padding = numberOption(owner, 'padding', options.padding, 0.25, 0, 10);
@@ -460,7 +462,11 @@ export function createCameraClip(options = {}) {
if (typeof ground !== 'function' && !Number.isFinite(ground)) {
throw new TypeError(`${owner} ground must be a height number or a function (x, z) => height, got ${describe(ground)}.`);
}
- const groundHeight = (x, z) => (typeof ground === 'function' ? ground(x, z) : ground);
+ // A ground function that returns NaN or Infinity would make every comparison false and silently turn clipping off.
+ const groundHeight = (x, z) => {
+ const height = typeof ground === 'function' ? ground(x, z) : ground;
+ return Number.isFinite(height) ? height : FALLBACK_GROUND_HEIGHT;
+ };
// Fraction of the segment target -> eye that is free of the padded box (1 when it never enters it).
function freeFraction(target, offset, box) {
diff --git a/tests/player-rigs.test.js b/tests/player-rigs.test.js
index 673fc01b..4d68976b 100644
--- a/tests/player-rigs.test.js
+++ b/tests/player-rigs.test.js
@@ -112,6 +112,13 @@ test('the clip keeps the camera above sloped ground', () => {
assert.ok(eye[1] >= 3.5 - 0.05, `eye y was ${eye[1]}`);
});
+test('the clip treats a non-finite ground height as ground height 0', () => {
+ const clip = createCameraClip({ ground: () => NaN, padding: 0.5 });
+ const eye = clip([0, 5, 0], [0, -4, 6]);
+ assert.ok(eye.every(Number.isFinite), `eye was ${eye}`);
+ assert.ok(eye[1] >= 0.5 - 0.05, `eye y was ${eye[1]}`);
+});
+
test('the car drives the course on its own controller and chase camera and stops at the wall', () => {
const clip = createCameraClip({ obstacles: OBSTACLES, ground: courseGround });
const game = createGame(