Yesterday I walked through one shot in my 8-ball pool engine. Carrom on mympgames.com is built the same way: one UMD module (carrom-core.js, about 570 lines) loaded by Node and the browser, a server engine that runs each stroke, and a computer player that tests candidates with the real physics.
The physics was the easy part, because carrom coins slide rather than roll. The rules took longer: a queen that has to be "covered", penalties you can owe before you have anything to pay with, and a foul for finishing too early. This post covers what's different from pool and how each rule maps to code.
The board is 1000 × 1000, and a striker position is one number
The playing surface inside the cushions is 1000 units square. Everything is a circle:
const W = 1000; const COIN_R = 21.5, STRIKER_R = 28; const POCKET_R = 31, POCKET_C = 31; // pocket radius, centre offset from both walls const BASE_OFF = 158, BASE_HALF = 300; // baseline distance from wall, half-length In carrom you shoot from your baseline, between its two end circles. I didn't want the client sending an (x, y) that the server then has to check against a line segment, so a striker position is a single number t in [-1, 1], and each of the four sides maps it onto its own baseline:
function baselinePoint(side, t) { const k = clamp(t, -1, 1) * BASE_HALF; if (side === 0) return { x: W / 2 + k, y: W - BASE_OFF }; // bottom if (side === 2) return { x: W / 2 - k, y: BASE_OFF }; // top if (side === 1) return { x: BASE_OFF, y: W / 2 + k }; // left return { x: W - BASE_OFF, y: W / 2 - k }; // right } Placement can never be off the line. The only check left is overlap with a coin, which prepareShot rejects. The client avoids sending that by snapping your drag to nearestFreeT, which searches outward from t in steps of 0.02.
Physics: sliding discs, nothing more
The 8-ball engine has to track slip and spin, because a cue ball slides and then rolls. A carrom coin just slides on powder until it stops. So each step applies a constant deceleration plus a small damping term that scales with speed:
const DT = 1 / 240; const FRICTION = 640; // units/s², constant sliding deceleration const DRAG = 0.35; // 1/s, velocity-proportional // per body, per step: const sp = Math.hypot(b.vx, b.vy); const ns = sp * (1 - DRAG * DT) - FRICTION * DT; if (ns <= STOP_V) { b.vx = 0; b.vy = 0; } else { b.vx *= ns / sp; b.vy *= ns / sp; } The constant term gives the "slide, then stop dead" feel of a real board. The drag term stops a full-power break from circling the board five times.
The other numbers that matter:
- Mass. The striker is 2.6 times heavier than a coin, so on a straight hit it follows through instead of stopping dead, which is what carrom shots look like.
- Restitution.
E_BALL = 0.9between discs,E_WALL = 0.72off the cushions, plus 3% off the velocity along the wall as a cheap stand-in for cushion friction. - Overlap correction by inverse mass. Overlapping discs are pushed apart in proportion to
im / imSum, always in the same fixedi < jpair order.
The timestep is 240 Hz, lower than pool's 400. At full power (VMAX = 2900) a disc moves about 12 units per step, roughly half a coin radius, so plain overlap tests are enough and nothing tunnels. A stroke is capped at 14 seconds of board time.
Pockets are plain circles with no jaw geometry. Carrom pockets are holes in the corners, not cushioned openings, so this plays fine.
Replays work as in pool: the server broadcasts the stroke's starting numbers, every browser re-runs simulate, and a hash of the end positions catches drift. Carrom adds one thing. If you open the game mid-stroke, the client works out how far through it the server is and replays only the rest, so late joiners still see the coins moving.
The rules as a state machine
applyStroke(state, seat, sim) is the referee. It takes the simulation result and decides what it means. Most of it revolves around three fields:
queen: 'board' | 'pending' | 'covered', queenBy: null, // seat that pocketed it due: [0, 0], // penalties owed, per seat Own coin, then the queen
Pocket a coin of your colour and you shoot again. Pocket only the opponent's coins and they stay down (counting for them) while the turn passes. The queen is harder: pocketing her isn't enough. You have to "cover" her by pocketing one of your own coins, either in the same stroke or on the next one:
if (queenIn) { if (own.length > 0) { state.queen = 'covered'; state.queenBy = seat; } else { state.queen = 'pending'; state.queenBy = seat; again = true; } } A pending queen gives you another shot even if nothing of yours went in. The cover check runs first in the next stroke, before anything else:
if (state.queen === 'pending' && state.queenBy === seat) { if (!foul && own.length > 0) state.queen = 'covered'; else { state.queen = 'board'; returnCoin(state, queen); } // back to the centre } returnCoin uses a deterministic spiral search, freeSpot: the exact centre first, then rings of 6 × ring points, taking the first spot that overlaps nothing. No randomness, so every machine puts the coin in the same place.
Pocketing the striker: a penalty you can owe
If the striker goes into a pocket, it's a foul. Your coins from that stroke come back out, the queen comes back if it went in (unless she was already covered), and one of your previously pocketed coins comes back as a penalty. If you haven't pocketed anything yet, there's nothing to return, so the penalty becomes a debt:
const penalty = () => { const pc = penaltyCoin(state, me); // most recently pocketed own coin if (pc) { returnCoin(state, pc); return true; } state.due[seat] += 1; // owed return false; }; The debt is paid out of your next successful pocket: that coin goes back to the centre, though you still shoot again.
An early version without due let a first-shot foul cost nothing, which felt wrong straight away.
The last-coin foul
You can't finish your colour while the queen is still open. Sinking your ninth coin while queen is 'board' or 'pending' is a last-coin foul: the stroke's coins go back, plus a penalty. The same applies to sinking the opponent's last coin. Without this rule you could ignore the queen entirely.
Winning
There's no points table. A game is a single board, and the win check only runs once the queen is covered:
if (state.queen === 'covered') { if (countPocketed(state, me) === 9) winner = seat; else if (countPocketed(state, opp) === 9) winner = seatOfColor(state, opp); } The second branch catches the case where you knock in your opponent's last coin after the queen is covered. That wins the game for them, not you.
The computer: knowing the rules matters more than aiming
carrom-ai.js uses the ghost-coin geometry from the pool bot. For each target coin and each pocket, it tries 21 baseline positions, discards cut angles with a cosine below 0.3 and paths blocked by other coins, and then simulates the best candidates at three power levels. The interesting part is the scoring, because it has to understand the rules above:
if (sim.strikerPocketed) sc -= 14; if (queenOpen && ownIn >= ownLeft && ownLeft > 0) sc -= 18; // last-coin foul sc += ownIn * 10 - oppIn * 5; if (state.queen === 'pending' && state.queenBy === seat) sc += ownIn ? 14 : -10; Before the last-coin term, the hard bot would sink its final coin with the queen still on the board, foul, and do it again. It also scores the leftover position (own coins near pockets are good, the opponent's are bad). The opening stroke is a fixed full-power break at the centre. Difficulty sets the simulation budget (40 / 90 / 150) and aim noise (2.4° / 0.7° / 0.22°).
Tradeoffs
- No spin. Realistic for carrom and keeps the core small, but rebound shots depend only on angle and power.
- Any direction.
prepareShotaccepts any aim vector. Some house rules restrict shooting at coins behind your baseline; I don't enforce that yet. - One board per game. Club matches score points over several boards. A single board suits a browser session, but it is a simplification.
- Fixed 240 Hz. Cheap enough for the bot to run 150 full simulations per move, which matters more to me than precision at extreme speeds.
Carrom is one of 60+ games on the site. You can try it against the computer or a friend at mympgames.com/play/carrom, or browse the rest at mympgames.com.
