The Character Hangs in the Air Below the Ladder
When a character grabs a ladder and ends up floating below its lowest rung, or above its top, the climb starts from wherever the feet happened to be. A body can touch a ladder with its feet past one of its ends; once it is on the ladder it has to be held between them. TLC found this in our own ladder blueprint by trying every height at which a body can touch the ladder.
What you see
- The character grabs the ladder from the ground and hangs in the air a little below the bottom rung, or climbs up from there.
- Pressing down with the feet above the top of a ladder can, for the same reason, leave it hanging above the top.
- Whether it happens depends on exactly where the body touches the ladder, so it comes and goes.
Why does the character end up below the ladder?
Our ladder climbing blueprint grabs the ladder when the player presses up or down while the body touches it. It then took the body's own height as the start of the climb. But the body touches the ladder over its whole height, so the feet can be below the ladder's foot at that moment. The climb then went on from there, below the ladder's end, and hanging still kept the character there.
TLC checked the rule that a character on the ladder is always between its ends, for every height at which the body may touch it, and found a body touching the ladder 2 units below its foot that climbed on, 1 unit below the ladder's end. Z3 gave an example in numbers: from half a unit below the foot, the first frame of climbing ends at -0.45, still below the end.
How to check your game for it
The first test grabs the ladder from half a unit below its foot, once climbing and once hanging still. The second is the rule TLC checked, written as a plain loop: from every starting height between 2 below the foot and 2 above the top, in every direction, a character that is on the ladder afterwards is between its ends. The third makes sure climbing past an end still steps off at that end.
The test: fix.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { climb } from "./fix.ts";
const ladder = { bottom: 0, top: 6 };
const speed = 3;
const dt = 1 / 60;
test("a grab with the feet below the ladder's foot starts at the foot", () => {
const up = climb(-0.5, ladder, 1, speed, dt);
assert.ok(up.on && up.y >= ladder.bottom, `pressing up from half a unit below the foot: on the ladder at ${up.y}`);
const still = climb(-0.5, ladder, 0, speed, dt);
assert.equal(still.y, ladder.bottom, "hanging still from half a unit below the foot");
});
test("while on the ladder, the character is always between its ends", () => {
// every starting height from 2 below the foot to 2 above the top, every direction
for (let y = ladder.bottom - 2; y <= ladder.top + 2; y += 0.25) {
for (const axis of [-1, 0, 1]) {
const after = climb(y, ladder, axis, speed, dt);
if (after.on) assert.ok(after.y >= ladder.bottom && after.y <= ladder.top, `from ${y} with axis ${axis}: on the ladder at ${after.y}`);
}
}
});
test("climbing past an end steps off at that end", () => {
assert.deepEqual(climb(5.99, ladder, 1, speed, dt), { y: 6, on: false });
assert.deepEqual(climb(0.01, ladder, -1, speed, dt), { y: 0, on: false });
});
The catch:
- a grab with the feet below the ladder's foot starts at the foot
- while on the ladder, the character is always between its ends
What it said on the code with the bug put back
✖ a grab with the feet below the ladder's foot starts at the foot
pressing up from half a unit below the foot: on the ladder at -0.45
✖ while on the ladder, the character is always between its ends
from -2 with axis 0: on the ladder at -2
✔ climbing past an end steps off at that end
3 tests: 1 passed, 2 failedWhat it said on the fixed code
✔ a grab with the feet below the ladder's foot starts at the foot
✔ while on the ladder, the character is always between its ends
✔ climbing past an end steps off at that end
3 tests: 3 passed, 0 failedThe fix
Before the climb moves the character, hold the starting height between the ladder's bottom and its top. Then a grab from below starts at the foot and a grab from above starts at the top, and the character can never hang off the ends.
What changed
where the body is now, `axis` is 1 for up, -1 for down and 0 to hang still. */
export function climb(y: number, ladder: Ladder, axis: number, speed: number, dt: number): { y: number; on: boolean } {
- const from = y;
+ // Start from the body's height held between the ladder's ends, not from wherever the feet are.
+ const from = Math.min(Math.max(y, ladder.bottom), ladder.top);
if (axis === 0) return { y: from, on: true };
const next = from + Math.sign(axis) * speed * dt;The fixed code: fix.ts
/* One frame on a ladder. The body may touch the ladder with its feet past one of its ends; on the
ladder it is held between them, and past an end it steps off. From the ladder climbing blueprint:
https://spielerando.com/en/mechanics/ladder-climbing */
export interface Ladder {
/** The height of the lowest rung, where the character steps onto the ground. */
bottom: number;
/** The height of the top, where the character steps onto the platform. */
top: number;
}
/** Where the character is after one frame on the ladder, and whether it is still on it. `y` is
where the body is now, `axis` is 1 for up, -1 for down and 0 to hang still. */
export function climb(y: number, ladder: Ladder, axis: number, speed: number, dt: number): { y: number; on: boolean } {
// Start from the body's height held between the ladder's ends, not from wherever the feet are.
const from = Math.min(Math.max(y, ladder.bottom), ladder.top);
if (axis === 0) return { y: from, on: true };
const next = from + Math.sign(axis) * speed * dt;
// Past an end the character steps off and stands right at that end.
if (axis > 0 && next >= ladder.top) return { y: ladder.top, on: false };
if (axis < 0 && next <= ladder.bottom) return { y: ladder.bottom, on: false };
return { y: next, on: true };
}
Paste this to your AI
In my game the character can grab a ladder and end up hanging in the air below its lowest rung, or above its top. I think the climb starts from wherever the body touched the ladder instead of from a point on the ladder.
1. Before you change anything, write down the states of my ladder code (free, hanging, climbing, jumping off) and what moves the character between them. Write next to it the rule it must keep: while on the ladder, the character is between the ladder's bottom and its top.
2. Write tests like the ones below for my code: a grab with the feet half a unit below the ladder's foot, and a loop over many starting heights below, on and above the ladder, in every direction, that checks the rule. Run them and paste me the real output.
3. If they fail, fix it the way the code below does: hold the starting height between the ladder's ends before the climb moves it. Run the tests again and paste the output.
4. Start the climb from the body's own height again for a moment, show me the rule's test failing, and undo it.
Do not tell me it is fixed unless the test output shows it.
The code, from https://spielerando.com/en/fixes/character-hangs-in-the-air-below-the-ladder (TypeScript, tested):
```ts
/* One frame on a ladder. The body may touch the ladder with its feet past one of its ends; on the
ladder it is held between them, and past an end it steps off. From the ladder climbing blueprint:
https://spielerando.com/en/mechanics/ladder-climbing */
export interface Ladder {
/** The height of the lowest rung, where the character steps onto the ground. */
bottom: number;
/** The height of the top, where the character steps onto the platform. */
top: number;
}
/** Where the character is after one frame on the ladder, and whether it is still on it. `y` is
where the body is now, `axis` is 1 for up, -1 for down and 0 to hang still. */
export function climb(y: number, ladder: Ladder, axis: number, speed: number, dt: number): { y: number; on: boolean } {
// Start from the body's height held between the ladder's ends, not from wherever the feet are.
const from = Math.min(Math.max(y, ladder.bottom), ladder.top);
if (axis === 0) return { y: from, on: true };
const next = from + Math.sign(axis) * speed * dt;
// Past an end the character steps off and stands right at that end.
if (axis > 0 && next >= ladder.top) return { y: ladder.top, on: false };
if (axis < 0 && next <= ladder.bottom) return { y: ladder.bottom, on: false };
return { y: next, on: true };
}
```
The test that catches it:
```ts
import assert from "node:assert/strict";
import test from "node:test";
import { climb } from "./fix.ts";
const ladder = { bottom: 0, top: 6 };
const speed = 3;
const dt = 1 / 60;
test("a grab with the feet below the ladder's foot starts at the foot", () => {
const up = climb(-0.5, ladder, 1, speed, dt);
assert.ok(up.on && up.y >= ladder.bottom, `pressing up from half a unit below the foot: on the ladder at ${up.y}`);
const still = climb(-0.5, ladder, 0, speed, dt);
assert.equal(still.y, ladder.bottom, "hanging still from half a unit below the foot");
});
test("while on the ladder, the character is always between its ends", () => {
// every starting height from 2 below the foot to 2 above the top, every direction
for (let y = ladder.bottom - 2; y <= ladder.top + 2; y += 0.25) {
for (const axis of [-1, 0, 1]) {
const after = climb(y, ladder, axis, speed, dt);
if (after.on) assert.ok(after.y >= ladder.bottom && after.y <= ladder.top, `from ${y} with axis ${axis}: on the ladder at ${after.y}`);
}
}
});
test("climbing past an end steps off at that end", () => {
assert.deepEqual(climb(5.99, ladder, 1, speed, dt), { y: 6, on: false });
assert.deepEqual(climb(0.01, ladder, -1, speed, dt), { y: 0, on: false });
});
```
In my game the character can grab a ladder and end up hanging in the air below its lowest rung, or above its top. I think the climb starts from wherever the body touched the ladder instead of from a point on the ladder.
1. Before you change anything, write down the states of my ladder code (free, hanging, climbing, jumping off) and what moves the character between them. Write next to it the rule it must keep: while on the ladder, the character is between the ladder's bottom and its top.
2. Write tests like the ones below for my code: a grab with the feet half a unit below the ladder's foot, and a loop over many starting heights below, on and above the ladder, in every direction, that checks the rule. Run them and paste me the real output.
3. If they fail, fix it the way the code below does: hold the starting height between the ladder's ends before the climb moves it. Run the tests again and paste the output.
4. Start the climb from the body's own height again for a moment, show me the rule's test failing, and undo it.
Do not tell me it is fixed unless the test output shows it.Where we found it
The blueprint the bug was in, and the test that guards it there:
- Ladder Climbing:
grabbing-from-below-the-bottom-starts-at-the-bottom
How we found them: the bugs our proofs found in our own code
Was this page helpful?
Next step
Fix by symptom