HTML5 game publishing
Draw-Your-Own-Road Games: From Pointer Stroke to Physics Colliders
Capture the stroke, simplify it with Ramer-Douglas-Peucker, build thick overlapping colliders, cap the ink and tune the vehicle so any reasonable line works.
"Draw a road and drive on it" games look like magic: the player scribbles a line and a car drives along it as if it were solid. Under the hood it is a pipeline with four stages: capture the stroke, simplify it, build colliders from it and keep the whole thing cheap. Skip any stage and the car either jitters, tunnels through the road or the frame rate collapses after a few lines. This post walks through each stage with a physics engine such as Matter.js in mind, though the ideas apply to any 2D engine.
Stage one: capture the stroke
Use pointer events and record points while the pointer is down. Do not store every event. A fast stroke produces points that are far apart, and a slow stroke produces dozens of points on top of each other.
let stroke = [];
canvas.addEventListener('pointerdown', e => { stroke = [pos(e)]; });
canvas.addEventListener('pointermove', e => {
if (!stroke.length) return;
const p = pos(e), last = stroke[stroke.length - 1];
if (dist(p, last) >= MIN_STEP) stroke.push(p); // e.g. 6 px
});
canvas.addEventListener('pointerup', finishStroke);
A minimum step of a few pixels removes the clustered points. Use getCoalescedEvents() on pointermove if you need every sample from a fast stroke, but check that it exists before relying on it. Convert coordinates from screen to world space the same way you handle clicks. See hit testing for puzzle boards for the conversion.
Stage two: simplify the line
A hand-drawn stroke has hundreds of tiny wobbles. If you turn each segment into a collider, a car rolling over it will hit a thousand little bumps, and the physics will jitter. Simplify first. The Ramer–Douglas–Peucker algorithm removes points that do not change the shape by more than a tolerance:
function simplify(points, eps) {
if (points.length < 3) return points;
let maxD = 0, idx = 0;
const a = points[0], b = points[points.length - 1];
for (let i = 1; i < points.length - 1; i++) {
const d = perpDist(points[i], a, b);
if (d > maxD) { maxD = d; idx = i; }
}
if (maxD <= eps) return [a, b];
const left = simplify(points.slice(0, idx + 1), eps);
const right = simplify(points.slice(idx), eps);
return left.slice(0, -1).concat(right);
}
A tolerance of two or three pixels keeps the shape and cuts the point count by an order of magnitude. A higher tolerance gives a smoother, more forgiving road at the cost of fidelity. For a road that should feel smooth, run a light smoothing pass (averaging each point with its neighbours) before simplifying.
Stage three: build colliders
There are two main ways to make a line solid.
- A chain of thin rectangles. For each segment, create a static rectangle with length equal to the segment and a small thickness, rotated to the segment's angle. It is simple and works in every engine, but the joins between segments leave tiny notches, and fast objects can catch on them.
- A single polyline or chain shape. Engines such as Box2D have chain or edge shapes that handle joins smoothly. Matter.js does not have one built in, so use overlapping rectangles with rounded ends, or a polygon built from the line offset by half the thickness.
Give the road real thickness, such as 8 to 12 world pixels, and overlap the segments slightly. A razor-thin collider lets small, fast bodies tunnel through between physics steps. Enable continuous collision detection if your engine supports it, or run the physics at a smaller fixed step. See 2D collision detection for browser games and fixed timestep loops.
Stage four: keep it cheap
- Cap the ink. A budget, shown as a bar, caps total line length. It is a design feature and a performance guard. Games such as Sketch Road use exactly this.
- Cap the segment count. After simplification a long stroke may still have 50 segments. That is fine. A thousand is not.
- Make roads static. Static bodies cost almost nothing in the broad phase. Only the vehicle and a few props should be dynamic.
- Sleep the vehicle when it has stopped, then wake it on contact.
- Remove old strokes when the player erases or restarts, and destroy the bodies, not just hide them. Leftover colliders are invisible leaks.
The vehicle matters as much as the road
A car that rolls well on any reasonable line is what makes the game feel fair. Tips:
- Use wheels as circles with moderate friction, and a body with low friction so it slides rather than catches.
- Make the center of mass low so the car does not flip on slopes.
- Limit angular velocity. A car that spins uncontrollably after a bump feels broken.
- Apply a small constant forward force, or use a motor on the wheels, so the car moves without input.
Detect success and failure
Success is usually a trigger zone at the flag: when the car's body overlaps it for a moment, the level is solved. Failure has three cases: the car falls below the world, flips and stays still for a few seconds, or runs out of time. Check them each frame and keep them in one function, not scattered through collision handlers. Add a short delay before showing the result so the player sees what happened. A generous restart button, at all times, keeps frustration low.
The same approach powers path-drawing games on the player side. We describe how they feel in draw-your-own-path puzzle games.


