Skip to content

Pool & Snooker Simulator

Pool & Snooker Simulator screenshot 1

A 3D pool and snooker game with rules you can switch on and off and two custom game modes, built for my Games Computing module at Keele University. Balls, cushions and pockets run on the Open Dynamics Engine, with openFrameworks for the graphics and a cue I modelled in Blender. Golden Pocket and Evil Pocket modes hand out random rewards and punishments for potting into a highlighted pocket. Written in C++.

The brief

This was my coursework for the Games Computing module at Keele University in January 2022: design a game, build it in C++ on openFrameworks with the Open Dynamics Engine (ODE) for physics, then evaluate it. I weighed up pinball, air hockey and cue sports, and went with cue sports because they are played with friends, and because pool needs physics accurate enough to really test what ODE can do. My twist was letting players change the game itself: switch individual rules off, tweak the table, and turn on two new modes that hand out random rewards and punishments.

Setting up a game

The start menu, built with openFrameworks' ofxGui, lets the players choose:

  • Pool or snooker. Each game builds its own table and ball layout.
  • Player names. Player 1 always breaks.
  • Table settings: maximum cue power, and offsets for how slippery the cloth is and how bouncy the balls are.
  • Golden Pocket and Evil Pocket, the two new modes (below).
  • Which pool fouls count. Each of these can be switched off: potting the white, potting the wrong colour, hitting the wrong colour first, missing everything, hitting no cushion and potting nothing, and knocking the white off the table. With "potting the white" off, it becomes a tactic: pot it on purpose and your opponent has to play from behind the baulk line.
Start menu with game choice, player names, maximum cue power, Golden Pocket and Evil Pocket toggles, slipperiness and bounciness offsets, six pool foul toggles and the list of key binds
The start menu: game settings, the pool foul toggles and the controls.

The rules

Pool follows the English 8-ball rules I grew up playing. After every shot, once all the balls have stopped, the game checks:

  • Fouls (any that are switched on). A foul gives the other player two visits, or one visit with a free white if they are on the black.
  • The baulk line, drawn across the table a fifth of its length from the end. A free white can be moved anywhere behind it with W, A, S and D, and is pushed back if it strays out.
  • Colours. The first ball potted cleanly after the break decides who is reds and who is yellows.
  • Winning. Pot all your colour, then the black. Pot the black early, or foul while potting it, and your opponent wins.

Fouls and wins are announced on screen, so new players can see why their opponent just got two visits.

Top-down view of the pool table with the announcements Frank WON THE GAME and Foul: Potted the White, and the aiming guideline from the white ball
The end of a game: the winner is announced along with the foul that ended it.

Golden Pocket and Evil Pocket

Turning either mode on highlights a random pocket. Pot a ball into it without fouling and something random happens, then the highlight moves to a different pocket:

Golden Pocket (a reward) Evil Pocket (a punishment)
An extra visit on top of the one for pottingYou lose the visit you just earned by potting
A free white you can move behind the baulk lineRandom cloth friction for the next shot
One fewer of your balls to pot before the blackOne fewer of your opponent's balls to pot before the black

The randomness is the point. It opens up more possible games, so the game is harder to "solve" and takes longer to get boring.

Top-down view mid-game with a gold pocket and a red pocket highlighted, and the announcements Frank is Red and Player 2 is Yellow, Golden Pocket: Free Ball
A pot into the gold pocket has just earned a free ball. The red pocket is the Evil Pocket.

Playing a shot

The cue, which I modelled and UV-textured in Blender, orbits the white ball. Its position is just sine and cosine of the aim angle, and it turns to face the ball. A guideline shows where the white will go. That matters on a screen, because you can't walk round the table to look. There are three cameras: above the table, down the cue, and a free camera.

First-person cue camera looking along the textured cue at the white ball, with the red and yellow balls at the far end of the table
The cue camera, looking down the Blender-modelled cue.
Key Action
← / →Rotate the cue around the white
TabSwitch between coarse and fine aiming
↑ / ↓Increase or decrease shot power
1 / 2 / 3Above view, cue view, free camera
W A S DMove a free white ball
SpaceTake the shot

Taking the shot runs a short animation: the power bar drains as the cue moves forward and strikes the white, and only then is the force applied to the ball.

Physics with ODE

Balls bounce off the cushions using ODE's dContactBounce. A pot is detected with dCollide between a ball and a pocket's geometry, so the ball drops into the pocket instead of vanishing on contact.

  • Friction. ODE has no rolling friction, so a rolling ball would never stop. I damp every ball's velocity by hand each step.
  • Spin. My first version used dContactRolling on the cloth to model spin. Balls then drifted left or right off their line without touching anything, and sped up and slowed down at random. I swapped it for slip contacts on the cloth, locked each ball's angular velocity to zero, and kept the hand-written damping. Making the balls bigger and denser changed the drift a lot, which points to the simulation's scale as the real cause.
  • A stable step. The frame rate varies, so the game tracks the time that has passed and runs as many fixed-size dWorldStep calls as fit into it.
  • Ending a turn. A turn starts when the white is struck and ends when every ball has stopped. A ball counts as stopped only after it has stayed slow for a minimum number of ticks, so a ball that pauses against a cushion doesn't end the turn early.

How the code is organised

Everything hangs off openFrameworks' setup, update and draw. setup builds the menus, camera, lighting and the ODE world. update steps the physics. draw runs the game's drawing and rule logic. Game is an abstract base class that holds the players, fonts, settings and turn logic. PoolGame and SnookerGame inherit from it and set up their own tables and ball positions, so a new variant such as 9-ball only needs a new subclass. When a game ends, the winner's name pulses through the colours on screen, the game's memory is freed, and play returns to the start menu.

What I'd fix next

  • Balls leave the cushions at a slightly shallower angle than they should. I suspect this comes from locking their spin.
  • The cue and the guideline pass through the cushions. The fix is to clip the guideline where it meets a cushion and lift the back of the cue over it.
  • Spin (side, top and screw) is missing, and adding it means scaling the whole simulation up first.

Built with

C++, openFrameworks with ofxGui, the Open Dynamics Engine, and Blender for the cue model. It's about 2,350 lines of my own code across 25 source files, built on Linux.

Visitors: 36280