View on GitHub

8 Ball Pool Game with OpenGL & C++

Download this project as a .zip file Download this project as a tar.gz file

Abstract

In this project, I built an interacting billiard game using modern OpenGL API. This game simulates a table with 6 pockets and 16 balls including a cue ball. The cue ball can be hit with a force using the mouse motions such that it gets an initial velocity. And the balls can roll on the table by simulating friction. I also implemented the necessary physics to modeling the collision between a ball and a sidewall, and between two balls. In addition, per-fragment shading is also implemented to achieve better lighting effect.

This report contains five part:

  1. Geometry and rendering setup
  2. Interaction of hitting a ball
  3. Motion of a ball
  4. Collision handling
  5. Summary and discussion

Part 1 Geometry and rendering setup

First, we create a sphere mesh for drawing the balls and also a table mesh. And then we bind 16 textures to all the balls. Figure 1 shows the mesh and textures for pool balls.

Ball Mesh Ball Texture

Figure 1 The sphere mesh and pool balls textures

Initial Setup

Figure 2 The initial setup of our pool game with the table, balls, pocket and lighting

Per-fragment Shading

Figure 3 The shining spots on ball surfaces show per-fragment shading

Part 2 Interaction of hitting a ball

In this part, we implement an interface for hitting a ball. We can press down the left button of the mouse, mouse the cursor to a ball surface and release the mouse. The end position on the ball is the hitting point, and the vector from which the mouse moved towards the hitting point gives the force on the ball at the hitting point.

The implementation includes:

  1. Auto detect the ID of the hitted ball
  2. Calculate the force
  3. Assign an initial velocity to the hitted ball

For detecting the ball ID, we use color picking method by creating another render routine. For calculating hitting force we use windows coordinates to decide the size and direction of the force. And we use the following equations to derive the initial velocity for the hitted ball.

Linear Velocity Angular Velocity

Here, the Δt is the amount of time the cue is in contact with the ball. The k is a constant coefficient. The m is the mass of the cue ball and the vector x is the difference between the starting and end points of the mouse hitting motion. The vector r represents the radius from the center that the cue ball was stuck.

Game Start

Figure 4 Let’s start our game!

Part 3 Motion of a ball

Once the ball has an initial linear and angular velocities, it begins to move across the table. After it leaves the tip of the cue, the only force acting on the cue ball is the force of friction from the table. The motions of our balls include sliding and rolling. We use the following equations to update the linear and angular velocities of the ball when it moves across the table.

Linear Velocity Angular Velocity

Here we have a new vector vp, which is the perimeter velocity of the ball at the point of contact with table. The us is the coefficient of sliding friction and g is the force of gravity.

Game On

Figure 5 The game is going on!

Part 4 Collision handling

The collision between two balls or between a ball and a wall can be detected by calculating the distance between the centers of two ball or between the center of a ball and the wall.

1. Collision between two balls

If a collision is detected, we need to update the velocity vectors of the ball involved. And we use conservation of energy and conservation of momentum principles to derive the updated velocities. The following equations show the final results.

For linear velocity:

Linear Collision

First we calculate the normal to the collision plane. The vectors vn1 and vt1 are the normal and tangential components of the velocity of ball 1. And the vn2 and vt2 are for ball 2. The results show that the new linear velocities of two balls simply come from exchanging the normal components of two old velocities.

For angular velocity:

Angular Collision

The ub is the coefficient of friction between two balls. The vector vpR is the relative perimeter velocity between two balls. The above equation shows the change for ball 2, and ball 1 has exact same change.

2. Collision between a ball and a wall

It is much easy to implement the collision between a ball and a wall. Similar to the reflection principle of light, we use the following equations for updating the velocity.

v’ = vt - vn

w’ = wn - wt

First we calculate the normal of the wall. The vector vn and vt are the normal and tangential component of incident velocity of the ball, and v’ is the reflection velocity. Similarly we apply wn and wt to angular velocity.

Part 5 Summary

In addition to the above implementations, the balls can also go into the pockets. If the ball is cue ball, it will automatically come out again immediately at the center of table. And the future work can be simulating the players and a chalk, which are easy to implement.

Game End

Figure 6 Finally we finish the game. Yep!