The Grappling Hook Grabs the Wrong Point When the Game Stutters
When a grappling hook catches the anchor you aimed at on a smooth frame rate, but now and then grabs a different one close by after a stutter, it is choosing between anchors by the wrong measure. Our own blueprint ranked anchors by where the hook passed closest to them, not by which one it reached first. Z3 found a pair of anchors where those two answers differ.
What you see
- On a smooth frame rate the hook catches the anchor you aimed at. After a stutter, a loading hitch or on a slow computer it sometimes latches onto another one close by.
- It happens where two anchors sit near each other along the hook's path, one right on the line and one a little off it and a little nearer.
- Because it depends on the length of a frame, it is hard to make happen on purpose on a fast computer.
Why does a long frame change the anchor?
A fast hook can fly past a small anchor between two frames, so our grappling hook looks at the whole stretch the hook flew in a frame and at every anchor within reach of it. When several are within reach it should take the one the hook reached first. It took the one whose closest point came first along the stretch.
In many short frames that makes no difference: the hook comes within reach of the near anchor in an early frame and catches it. In one long frame both anchors are within reach of the same stretch, and an anchor slightly off the line can have its closest point earlier, though the hook came within reach of the other one first. Z3 found exactly that case: anchor A on the path at (5, 0), anchor B at (4.8, 1.99), a reach of 2. One long step caught B, many short steps caught A.
How to check your game for it
Fly the same path twice, once in fifteen short frames and once in a single long one, with Z3's two anchors in the way. Both must catch the same anchor, the one on the path. Two more tests make sure the nearer of two anchors on the path still wins and that an anchor out of reach is never caught.
The test: fix.test.ts
import assert from "node:assert/strict";
import test from "node:test";
import { catchAnchor, type Point } from "./fix.ts";
/** The hook flies from x = 0 to x = 10 along the ground in `frames` equal steps; what does it catch? */
function fly(frames: number, anchors: Point[], radius: number): Point | null {
const step = 10 / frames;
for (let i = 0; i < frames; i++) {
const caught = catchAnchor({ x: i * step, y: 0 }, { x: (i + 1) * step, y: 0 }, anchors, radius);
if (caught) return caught;
}
return null;
}
test("one long frame catches the same anchor as many short ones", () => {
// A lies on the path, B a little above it and a little nearer. With a reach of 2 the hook
// comes within reach of A first and of B only later, but it passes closest to B first.
const a = { x: 5, y: 0 };
const b = { x: 4.8, y: 1.99 };
assert.deepEqual(fly(15, [a, b], 2), a, "fifteen short frames");
assert.deepEqual(fly(1, [a, b], 2), a, "one long frame");
});
test("the nearer of two anchors on the path wins", () => {
assert.deepEqual(fly(1, [{ x: 6, y: 0 }, { x: 3, y: 0 }], 0.5), { x: 3, y: 0 });
});
test("an anchor out of reach is not caught", () => {
assert.equal(fly(1, [{ x: 5, y: 3 }], 2), null);
});
The catch:
- one long frame catches the same anchor as many short ones
What it said on the code with the bug put back
✖ one long frame catches the same anchor as many short ones
one long frame
got { x: 4.8, y: 1.99 }, expected { x: 5, y: 0 }
✔ the nearer of two anchors on the path wins
✔ an anchor out of reach is not caught
3 tests: 2 passed, 1 failedWhat it said on the fixed code
✔ one long frame catches the same anchor as many short ones
✔ the nearer of two anchors on the path wins
✔ an anchor out of reach is not caught
3 tests: 3 passed, 0 failedThe fix
For each anchor, work out where along this frame's stretch the hook first comes within reach of it, and take the anchor with the earliest such point. Then one long frame and many short ones catch the same anchor. The code below does that with a little geometry: from the closest point, step back along the stretch by as much as the reach allows.
The same question comes up wherever something fast picks one of several targets in a frame: a bullet and two enemies, a dash and two walls.
What changed
const EPSILON = 1e-9;
- /** Where on the stretch a to b the point p comes closest: 0 at a, 1 at b, and how far away it is then. */
- function closestOnStretch(a: Point, b: Point, p: Point): { t: number; distance: number } {
+ /** Where on the stretch a to b the hook first comes within `radius` of p: 0 at a, 1 at b, or null
+ if it never does. */
+ function firstTouch(a: Point, b: Point, p: Point, radius: number): number | null {
const dx = b.x - a.x;
const dy = b.y - a.y;
const length2 = dx * dx + dy * dy;
- const t = length2 < EPSILON ? 0 : Math.min(1, Math.max(0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2));
- return { t, distance: Math.hypot(a.x + dx * t - p.x, a.y + dy * t - p.y) };
+ if (length2 < EPSILON) return Math.hypot(a.x - p.x, a.y - p.y) <= radius ? 0 : null;
+ const closest = ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2;
+ const t = Math.min(1, Math.max(0, closest));
+ if (Math.hypot(a.x + dx * t - p.x, a.y + dy * t - p.y) > radius) return null;
+ // Along the line, the points within reach of p run from closest - half to closest + half.
+ const away2 = (a.x + dx * closest - p.x) ** 2 + (a.y + dy * closest - p.y) ** 2;
+ const half = Math.sqrt(Math.max(0, radius * radius - away2) / length2);
+ return Math.max(0, closest - half);
}
…
let earliest = Infinity;
for (const anchor of anchors) {
- const { t, distance } = closestOnStretch(from, to, anchor);
- if (distance <= radius && t < earliest) {
+ const t = firstTouch(from, to, anchor, radius);
+ if (t !== null && t < earliest) {
earliest = t;
caught = anchor;The fixed code: fix.ts
/* Which anchor does a flying hook catch in one frame? The one it comes within reach of first along
the stretch it flew, not the one it passes closest to. Then one long frame (a stutter, a slow
computer) catches the same anchor as many short ones. From the grappling hook blueprint:
https://spielerando.com/en/mechanics/grappling-hook */
export interface Point {
x: number;
y: number;
}
/** Below this a stretch has no length. */
const EPSILON = 1e-9;
/** Where on the stretch a to b the hook first comes within `radius` of p: 0 at a, 1 at b, or null
if it never does. */
function firstTouch(a: Point, b: Point, p: Point, radius: number): number | null {
const dx = b.x - a.x;
const dy = b.y - a.y;
const length2 = dx * dx + dy * dy;
if (length2 < EPSILON) return Math.hypot(a.x - p.x, a.y - p.y) <= radius ? 0 : null;
const closest = ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2;
const t = Math.min(1, Math.max(0, closest));
if (Math.hypot(a.x + dx * t - p.x, a.y + dy * t - p.y) > radius) return null;
// Along the line, the points within reach of p run from closest - half to closest + half.
const away2 = (a.x + dx * closest - p.x) ** 2 + (a.y + dy * closest - p.y) ** 2;
const half = Math.sqrt(Math.max(0, radius * radius - away2) / length2);
return Math.max(0, closest - half);
}
/** The anchor the hook catches flying from `from` to `to` in this frame, or null. */
export function catchAnchor(from: Point, to: Point, anchors: Point[], radius: number): Point | null {
let caught: Point | null = null;
let earliest = Infinity;
for (const anchor of anchors) {
const t = firstTouch(from, to, anchor, radius);
if (t !== null && t < earliest) {
earliest = t;
caught = anchor;
}
}
return caught;
}
Paste this to your AI
In my game the grappling hook usually catches the anchor I aim at, but after a stutter or on a slow computer it sometimes grabs a different one nearby. I think it picks between anchors by where it passes closest to them, not by which one it reaches first.
1. Before you change anything, find the code that decides which anchor (or which target, wall or enemy) a fast-moving thing hits in one frame. Write down how it chooses when more than one is within reach of the stretch it moved this frame.
2. Write a test like the one below for my code: fly the same path once in one long frame and once in many short ones, with one anchor on the path and one a little off it, and check that both catch the same anchor. Run it and paste me the real output.
3. If it fails, fix it the way the code below does: for each anchor, work out where along the frame's stretch the hook first comes within reach, and take the earliest. Run the test again and paste the output.
4. Put the old choice back for a moment, show me that the test fails, and undo it.
Do not tell me it is fixed unless the test output shows it.
The code, from https://spielerando.com/en/fixes/grappling-hook-grabs-the-wrong-anchor (TypeScript, tested):
```ts
/* Which anchor does a flying hook catch in one frame? The one it comes within reach of first along
the stretch it flew, not the one it passes closest to. Then one long frame (a stutter, a slow
computer) catches the same anchor as many short ones. From the grappling hook blueprint:
https://spielerando.com/en/mechanics/grappling-hook */
export interface Point {
x: number;
y: number;
}
/** Below this a stretch has no length. */
const EPSILON = 1e-9;
/** Where on the stretch a to b the hook first comes within `radius` of p: 0 at a, 1 at b, or null
if it never does. */
function firstTouch(a: Point, b: Point, p: Point, radius: number): number | null {
const dx = b.x - a.x;
const dy = b.y - a.y;
const length2 = dx * dx + dy * dy;
if (length2 < EPSILON) return Math.hypot(a.x - p.x, a.y - p.y) <= radius ? 0 : null;
const closest = ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2;
const t = Math.min(1, Math.max(0, closest));
if (Math.hypot(a.x + dx * t - p.x, a.y + dy * t - p.y) > radius) return null;
// Along the line, the points within reach of p run from closest - half to closest + half.
const away2 = (a.x + dx * closest - p.x) ** 2 + (a.y + dy * closest - p.y) ** 2;
const half = Math.sqrt(Math.max(0, radius * radius - away2) / length2);
return Math.max(0, closest - half);
}
/** The anchor the hook catches flying from `from` to `to` in this frame, or null. */
export function catchAnchor(from: Point, to: Point, anchors: Point[], radius: number): Point | null {
let caught: Point | null = null;
let earliest = Infinity;
for (const anchor of anchors) {
const t = firstTouch(from, to, anchor, radius);
if (t !== null && t < earliest) {
earliest = t;
caught = anchor;
}
}
return caught;
}
```
The test that catches it:
```ts
import assert from "node:assert/strict";
import test from "node:test";
import { catchAnchor, type Point } from "./fix.ts";
/** The hook flies from x = 0 to x = 10 along the ground in `frames` equal steps; what does it catch? */
function fly(frames: number, anchors: Point[], radius: number): Point | null {
const step = 10 / frames;
for (let i = 0; i < frames; i++) {
const caught = catchAnchor({ x: i * step, y: 0 }, { x: (i + 1) * step, y: 0 }, anchors, radius);
if (caught) return caught;
}
return null;
}
test("one long frame catches the same anchor as many short ones", () => {
// A lies on the path, B a little above it and a little nearer. With a reach of 2 the hook
// comes within reach of A first and of B only later, but it passes closest to B first.
const a = { x: 5, y: 0 };
const b = { x: 4.8, y: 1.99 };
assert.deepEqual(fly(15, [a, b], 2), a, "fifteen short frames");
assert.deepEqual(fly(1, [a, b], 2), a, "one long frame");
});
test("the nearer of two anchors on the path wins", () => {
assert.deepEqual(fly(1, [{ x: 6, y: 0 }, { x: 3, y: 0 }], 0.5), { x: 3, y: 0 });
});
test("an anchor out of reach is not caught", () => {
assert.equal(fly(1, [{ x: 5, y: 3 }], 2), null);
});
```
In my game the grappling hook usually catches the anchor I aim at, but after a stutter or on a slow computer it sometimes grabs a different one nearby. I think it picks between anchors by where it passes closest to them, not by which one it reaches first.
1. Before you change anything, find the code that decides which anchor (or which target, wall or enemy) a fast-moving thing hits in one frame. Write down how it chooses when more than one is within reach of the stretch it moved this frame.
2. Write a test like the one below for my code: fly the same path once in one long frame and once in many short ones, with one anchor on the path and one a little off it, and check that both catch the same anchor. Run it and paste me the real output.
3. If it fails, fix it the way the code below does: for each anchor, work out where along the frame's stretch the hook first comes within reach, and take the earliest. Run the test again and paste the output.
4. Put the old choice back for a moment, show me that the test fails, 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:
- Grappling Hook:
the-anchor-touched-first-wins-at-any-frame-rate
How we found them: the bugs our proofs found in our own code
Was this page helpful?
Next step
Fix by symptom