HTML5 game publishing
Knife-Throwing Collision in Angle Space: A Spinning Log in JavaScript
Store each stuck knife as an angle, test collisions by angular distance with wrap-around, account for flight time, vary speed smoothly and add forgiveness players cannot see.
The knife-throwing log is deceptively simple to build, and almost every first version has the same bug: knives that stick when they should have collided, or collide when they clearly missed. The trick is to stop thinking in screen coordinates. A spinning target is a circle, and everything stuck in it is best described by an angle. This post shows how to model it, test collisions in angle space and keep the feel right at different speeds.
Think in angles, not pixels
When a knife sticks in a rotating log, it does not move in a straight line any more. It moves with the log. So store each stuck knife as a single number: its angle relative to the log, measured at the moment it stuck.
const log = { angle: 0, speed: 2.4 }; // radians, radians/second
const stuck = []; // angles relative to the log
function update(dt) {
log.angle += log.speed * dt;
}
function stickKnife() {
// The knife arrives at the bottom of the log (angle PI/2 in screen space).
const impactScreenAngle = Math.PI / 2;
const rel = normalize(impactScreenAngle - log.angle);
stuck.push(rel);
}
To draw a stuck knife, add the log's current angle to its stored angle and convert to coordinates. Nothing else about the knife needs updating each frame, which is both simpler and cheaper than moving each knife separately.
Collision is an angular distance test
A new knife collides if it lands too close to a stuck one. Compare angles on the circle, remembering that angles wrap around:
function normalize(a) {
a = a % (Math.PI * 2);
return a < 0 ? a + Math.PI * 2 : a;
}
function angularDistance(a, b) {
const d = Math.abs(normalize(a) - normalize(b));
return Math.min(d, Math.PI * 2 - d);
}
function hits(rel) {
return stuck.some(s => angularDistance(s, rel) < MIN_GAP);
}
The wrap-around handling matters. Without the Math.min(d, 2π − d), a knife at 359 degrees and one at 1 degree look 358 degrees apart instead of 2, and collisions at the seam never fire.
Choose MIN_GAP from the knife's visual width at the log's radius: gap = knifeWidth / logRadius in radians. A larger log naturally tolerates more knives. Add a small forgiveness factor, about 10 to 15 percent narrower than the visual width, so near misses that look clean do not fail. Players remember unfair collisions far longer than fair ones.
Timing the throw
If the knife flies toward the log, the log keeps rotating while it is in flight. Two ways to handle that:
- Instant hit. Treat the throw as landing at the moment of the tap. Simple and responsive, but it feels floaty if the flight animation plays afterward.
- Flight time. Animate the knife for a fixed time (say 80 ms) and compute the log angle at the end of that time. The player has to lead the gap slightly. This is the more skill-based version, and it is more satisfying when it works.
Whichever you choose, apply the same rule to the collision test and to the picture. If the knife looks like it lands in a gap but is judged against the angle at tap time, the game feels wrong.
const FLIGHT = 0.08; // seconds
function throwKnife() {
const futureAngle = log.angle + log.speed * FLIGHT;
const rel = normalize(Math.PI / 2 - futureAngle);
if (hits(rel)) return fail();
stuck.push(rel);
}
Variable speed and reversals
Many knife games make the log speed up, slow down and reverse. Do this by changing log.speed smoothly over time, not by jumping values. An abrupt reversal makes the gap pattern impossible to read. A simple pattern uses a few phases, each with a target speed, and moves toward the target with easing:
log.speed += (target - log.speed) * Math.min(1, dt * 4);
Avoid speeds near zero when the log is meant to be hard, since a log that nearly stops gives the player free throws. Design the schedule as data per level, so you can tune it without touching code. This is the same idea as the hint ladder in designing trick-puzzle levels: keep pacing in data.
Pre-placed knives and obstacles
A level starts with a few knives or apples already on the log. They are just stored angles. Place them so the starting gaps are playable: a minimum gap of two or three times MIN_GAP keeps early levels fair. Apples are the same kind of angle, but a collision with an apple is a reward, not a failure. Store them as a separate list with their own radius.
For random placement that still guarantees a playable level, generate the pre-placed angles with a seeded random generator, as in a daily puzzle without a server, and verify that at least one gap larger than MIN_GAP * 2 exists.
Frame rate independence
Always multiply speed by dt. On a 144 Hz display, a log that rotates a fixed angle per frame spins more than twice as fast as on a 60 Hz one. If you also need results to be identical across devices, use a fixed timestep loop. See fixed timestep game loops.
Feedback that sells the hit
- A small screen shake and a thud sound when a knife sticks.
- The log wobbles slightly, which you can do by adding a short decaying offset to its angle.
- A clear, visible flash on a collision, showing which knife was hit.
The player-side view of how these games feel is in our guide to knife-throwing games.


