Virtual Pool using Atmel Mega32

Virtual Pool Trainer  Pool in a Box

Danny and Anthony are pool game lovers.  Both claim to have gotten the world pool championship at a certain point in time (though they refused to say exactly when).  A pool cue can be decently cheap, and so are the pool balls.  But the biggest constraint of all is the pool table, for it is not only expensive, but also big and bulky.  As such, most pool players like them have to play pool only at gaming centers, and get ripped off at high rates per hour, especially at the popular weekend hours.

Virtual Pool using Atmel Mega32
Virtual Pool using Atmel Mega32

Sure enough, there are numerous online pool games, of which Yahoo pool is D & A’s personal favorite.  But, these online games are not satisfying, because like most games, they do not have the feel of a real game of pool.  To replace a moving cuestick with a lifeless mouse, and to simulate the force of a whole arm with the click of a finger certainly takes a large part of the fun away.  As such, a PC pool expert, locked away in his room, secretly training remains helpless at the pool table itself.

Hence, their mission:
To build a virtual pool trainer, simulating reality with an LCD, capturing reality with a real cue in hand.
Intro
Introduction to Virtual Pool in a Box

The virtual pool trainer interfaces wth the user through a real cue stick.  The cuestick is attached to a linear potentiometer, which in turn is attached to a rotary potentiometer.  The rotary potentiometer, simulating the position of the white cueball on the table, detects the direction of alignment of the cuestick, and hence determines the direction of initial thrust of the cueball.  The linear potentiometer determines the position of the cuestick relative to the cueball, so that it can be known when the cuestick strikes the cueball.  An accelerometer, attached to the cuestick as well, and measuring up to 10G measures the acceleration of the cuestick on impact with the cueball.  The whole setup involving the cuestick, potentiometers and the simulated pool table is shown in the picture below.

The imagery of the cuestick as it rotates and adjusts is captured on the LCD screen in real time.  In addition, the LCD displays the pool table and balls according to real life proportions.  At the start of the game, the player is able to follow instructions on screen by pressing several buttons attached to the box containing the LCD.  Using these buttons, he is also able to position the cueball at any position on the table upon the cueball entering the pockets.  Seamless integration of the user inputs and output on the LCD screen is controlled by the ATMega 32 MCU.

On startup, the player can choose 1 of 2 modes: Training and Full Game.  Training allows the user to choose from 1 up to 5 object balls for the purpose of practicing.  Full game pits 2 players against each other, in an 8-ball pool game, with a total of 15 object balls on the table, numbered 1 to 15.  Complicated background mathematical computations allow the balls to move as they should in real life, including the effects of friction with the table top, wall collision and ball collision.  In the 8-ball pool game, several rules are followed, such as allowing the opponent to place the cueball freely upon scratch by one of the players.  When 1 player has pocketed 7 of his balls as well as the number 8 black ball, the game ends.

The best feature of our virtual pool trainer, other than its reflection of real life pool, is its portability.  As can be seen, the LCD screen, protoboard, press buttons and switches are all incorporated in a small box.  In addition, the simulated table, cuestick and potentiometers certainly weigh a fraction of a real pool table.  For these reasons, we like to call it Pool In a Box.

Pool for Dummies

To play pool, one of the most popular games around the world, you essentially need 3 things –  a pool table (preferably green, or even the modern day blue), a cue-stick of varying dimensions, a white cueball on the table and at least one other ball, called the object ball for you to aim the cueball at.  There are 6 holes, called pockets, 1 at each corner of the table and 2 located at the middle of the long side of the table.  The objective is simply then, to hit the cueball, such that the object ball will, on collision with the cueball, enter the pocket.   On the other hand, having the cueball enter the pocket is a big no-no, in which case, pool players, for some unknown historical reason, call it a scratch.

The most common pool game, called 8-ball pool, involves 2 players, with not just one, but 15 object balls.  Each player has 7 balls, either all stripes, numbered 1 to 7, or all solids, numbered 9 to 15.  Each player will then attempt to put all his balls in the pockets, before winning the game by finishing off the number 8 black ball.  The objective sure sounds simple, but playing it need not be so.  The rules of playing 8-ball pool may be a little bit more complicated, and more details can be found readily online.

High Level Design

Rationale and sources

We wanted to make the game as real as possible, and decided to draw the ball to the exact proportional specifications, with respect to the table and actual pockets.  A main source was the actual pool table measuring 88 x 44 inches, having 2.2 inch diameter balls, corner pockets that were 6 inches from one end to another and side pockets that were 5.5 inches in width.  To illustrate our “Pool in a box” concept, we decided to make the whole system run on batteries, whereby the Microcontroller (Mega 32) would run on a 9V battery voltage regulated to 5V, the LCD display would run on two 9V batteries connected directly to the LCD display via a simple switch, accelerometers attached to a linear potentiometer that could be easily stuck to a pool cue, and finally a rotary potentiometer attached to a small representation of our pool table.

We wanted our pool game to be completely portable, and the best way to do this was to use a large graphical LCD display instead of a TV screen.  We chose the Seiko G321D LCD panel, partly because it was large (320×200 pixels) and also the fact that groups from previous years had used the panel before.  The graphical system did not require a certain sync timing (unlike the TV) but rather two ports sending commands and data to be written and read.  The ability to operate with less constraints on timing was important as we have a complicated mathematical system to loop through.

Using my personal cue, we attached linear potentiometers, rotary potentiometers and accelerometers to make sure that the structure was firm and would not fall easily.  Since our design is meant to be portable, all the items are to be easily detachable and re-attachable.  We decided to place all the items in a cardboard box (hence our name “Pool in a box”

We searched for many websites for physics on pool and found the following explaining about the intricacies of pool.

1. http://www.jimloy.com/billiard/phys.htm
2. http://aci.mta.ca/TheUmbrella/Physics/P3401/Investigations/BilliardCollREC.html

Background Math and Physics

Moving not 1, but a total of 16 balls around on a table require rather complicated physics.  In fact, certain simplifications have already been made.  For example, we assume that the cuestick always strikes the centre of the cueball, and when calculating velocity, we neglect the effect of ballspin.  From what we observe on the LCD, such simplifications do not affect the results too much.

Where ball physics is concerned, a few scenarios have to be considered.

1) Impact with the cuestick

Upon impact with the cueball, the acceleration of the cue is measured using the accelerometer hooked up to the ADC converter.  Using the formula

v = u + at

where u=0 for a stationary cue ball, we determined that the initial velocity of the cueball is a*dt, where dt is the small period of time that the cuestick is in contact with the cueball.  From various resources such as online websites, as well as through trial and error, we assigned 0.01s to the value of dt.

On impact, we too need to determine the direction the cueball would be traveling in.  For this purpose, we need to define an angle theta, which, like the math you learned in high school increases from 0 to 2 pi anticlockwise.  0 is defined as the angle when the thrust is toward the right direction and pi when the thrust is toward the left.

Since all positions and velocities are ultimately referenced from a common x,y coordinate axes, we need to break down v into its component vx and vy.  Hence,

vx = v * cos theta

vy = -v * sin theta

vy has a minus sign in front of it because the y axis points downwards, not upwards.
2) Slowing down

Like all other things, pool balls ultimately slow down due to a mysterious force known as friction with the table top.  For this, we simply incorporate a drag force into our calculation of velocity using

v = v – drag * v

3) Collision with the wall

When a ball hits the the rail, called a rail or a bank shot, the angle of incidence is the same as the angle of reflection. Hence,

Upper and lower rail hit:

vx = vx

vy = -vy

theta = 2*pi – theta

Side rails hit:

vx = -vx

vy = vy

theta = pi  theta

4) Collision between 2 balls

Here, to illustrate, we use a simple case of ball[1].theta = 90o, ie the aiming line is in the direction of the negative y-axis.  The impact line is a line drawn between the center of the 2 balls when they come into contact.  To calculate the angle between the impact line and the horizontal x-axis, we have

phi = tan-1 [ (-ball[1].y – ball[2].y) / (ball[1].x – ball[2].x)  ]

On impact, the object ball, ball 2 moves in the direction of n (direction of the impact line).

ball[2].theta = PI + phi

Ball 1 moves in the direction of t, perpendicular to the direction of motion of ball 2.  Hence,

 ball[1].theta= PI/2.0 + phi

To determine the angle between the impact line and the original aiming line (cut angle), we have

psi= ball[2].theta – ball[1].original_theta

According to the law of conservation of momentum,

v2n = v1 * cos psi

v1t = v1 * sin psi

Logical Structure

The structure of the program consists of the game, graphical LCD and accelerometers as well as potentiometers and push buttons.

The game

We have the game system containing 10 different states and conditions to simulate events such as the changing of the player, ball entering the pockets, etc.   This handles the main bulk of the game, especially changing the variables using calculations that employ physics and mathematics.  This is displayed to our graphical LCD.

Graphics

The microcontroller is to send commands to the LCD panel to instruct

 The system can also read data from the LCD and see what pixels are there presently before overwriting it.  Finally, the controls are by two potentiometers, two accelerometers and different pushbuttons as shown in the picture below.

We wanted our pool game to be completely portable, and the best way to do this was to use a graphical LCD display instead of a TV screen.  We chose the Seiko G321D LCD panel, partly because it was large (320×200 pixels) and also the fact that groups from previous years had used the panel before.  The graphical system does not require a certain sync timing (unlike the TV), and thus we have less of a worry of code fitting in within a certain amount of space.

The pool table on the screen is created to be similar to one that is in real life, and the pool table takes up 280×140 pixels, corresponding to 88″x44″ on a real pool table.  Also, the pool balls were measured to be 2.2″ in diameter, so we allocated 7 pixels in diameter for each ball.  This was actually the determining factor for the rest of the items, as we were only able to draw numbers recognizably on balls that were at least 7×7 pixels.  The different lines and pockets were drawn pixel by pixel, using excel to help us visualize the actual result.

Control

The control of the pool game was mainly through the different accelerometers, linear and rotary potentiometers, accelerometers and a set of push-buttons.  The accelerometer lets us know the amount of thrust given by the user, the linear potentiometer shows the actual cuestick on the screen moving, and determines when the cueball is touched, and the rotary potentiometer is used to know the angle with which we actually hit the cueball.

Different buttons on the box allow us to move the balls around during the training phase, so we can choose where to place the ball and try different angles of playing the game.  Also, when the cueball enters the hole accidentally or if the player hits an opponent’s balls first instead of his own balls, a scratch is called the other player can play the white ball anywhere on the table.  The up/down/left/right/next and OK buttons allow all these functions to be carried out.

Hardware/software tradeoffs

Both hardware and software are essential to our game, and our LCD screen needed time to display the images or it would flicker uncontrollably.  This meant that our program had to be fast enough to ensure that the flickering would be kept to a minimum.  Also, we wanted to get accurate readings for the acceleration, but when we used a 40g accelerometer, the resolution was very low, and when we used the 2g accelerometer, giving more than 2g of force would result in inaccurate readings.  Finally, we decided on using the 10g accelerometer as it would have the most resolution for the range of accelerations we needed.

Also, we wanted to take in values using interrupts from the ADC inputs, but our program only ran at 100ms intervals, thus updating values too fast (before entering the program loop).  Thus we had to slow down the system to account for such tradeoffs.

For software tradeoffs, we initially wanted the system to be very accurate, and coded the program using floating point calculations.  This however was found to be extremely slow and costly in CPU time.  We had to change almost everything to fixed point calculation, and this made the whole system much faster.  We could not draw too many graphics, as it would take a long time to draw each set of pixels.   Also, for drawing a new pixel, the pixel line had to be read, then the new pixel added in, and written back to it again.  This slowed down the time allowed for drawing new items.

Design Relations

Our project does not require any specific standards, except that we are not to jab anyone with our cuestick or fight in the lab with it.

Existing Patents

A million and one online pool games can be found readily just by a simple Google search.  There are even various leagues and tournaments for those wanting to give their virtual “mouse-clicking” pool skills a challenge.  Certainly, in terms of graphics, our “Pool In a Box” is no match for these professional designs.  After all, none of them are black and white, and some of them even incorporate 3-D graphics.  However, the fact is also that none of them interacts with the user via a real cuestick.

Virtual Pool using Atmel Mega32 diagram

The only exception is the “Automatic Pool Trainer”, a project at IMM/KOM, Aalborg University in Denmark.  Even then, this design requires a real pool table as well, as it doesn’t use an LCD screen for simulated graphical output.  More info of this project can be found from The Automatic PoolTrainer Homepage.  From a certain perspective, this project definitely feels more real than ours, since it is the actual ball that moves on the actual table.  However, portability is a feature that we retain a strong advantage in.  “Pool In a Box” means exactly that.

Moreover, as far as we know, it seems that the “Automatic Pool Trainer” has not filed for a patent.  Substantial searches have also been conducted for patents of pool games very much like ours, and none has been found thus far.

Parts list:

Atmel Mega32 – $8
Custom PC board – $5
SEIKO LCD – $30
Styrofoam Board – $3
Green Paper – $0.25
Potentiometers – free from other projects
Accelerometers – free sample from Analog Devices

For more detail: Virtual Pool


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top