Main Content

Model the Dynamics of Moving Billiard Balls

This example shows how to model the opening shot in a billiards game by using continuous-time matrix variables. In the model, a Stateflow® chart simulates the dynamics of a hybrid system that has a large number of discontinuities. For more information, see Continuous-Time Modeling in Stateflow.

When the simulation starts, a MATLAB® user interface (UI) shows a pool table with 15 billiard balls arranged in a triangular rack. The UI then prompts you to select the initial position and velocity of the cue ball. When the cue ball is released, the UI animates the motion of the billiard balls as they undergo a sequence of rapid collisions.

The model consists of:

  • The Stateflow chart Init, which calls the function sf_pool_plotter.m to initialize the position and velocity of the cue ball based on the input from the UI.

  • The Stateflow chart Pool, which calculates the two-dimensional dynamics of each billiard ball.

  • The MATLAB Function block Plot, which calls the function sf_pool_plotter.m to animate the motion of the billiard balls during the simulation.

  • The Scope block Vel, which displays the velocity of each billiard ball during the opening shot.

Calculate Continuous-Time Dynamics

To represent the dynamics of the billiard balls, the Pool chart makes several assumptions.

Continuous-Time Variables

The chart ignores the spin of the balls, so the state of the system is described completely by the positions and velocities of the balls. Each ball is assumed to have unit mass, so its position and velocity are described by the system of differential equations

$$
\left\{ \begin{array}{ll}
\dot{\mathbf{p}} = \mathbf{v} \\
\dot{\mathbf{v}} = \mathbf{F}_\mathrm{friction} +
\mathbf{F}_\mathrm{interaction},
\end{array} \right.
$$

where $\mathbf{F}_\mathrm{friction}$ and $\mathbf{F}_\mathrm{interaction}$ are the forces caused by friction with the pool table and by collisions with other balls.

To track the positions and velocities of the balls, the chart stores a pair of 16-by-2 matrices in the continuous-time variables p and v. In each matrix, the $i^\mathrm{th}$ row represents the two-dimensional position or velocity of the $i^\mathrm{th}$ ball.

Friction Model

To calculate the force of friction acting on each ball, the chart calls the MATLAB function frictionForce. This function implements a simplified friction model. Friction acts on each moving ball with a constant force opposite to the direction of motion. Because friction does not act on stationary balls, the force of friction on each ball is inherently modal:

$$
\mathbf{F}_\mathrm{friction} =
\left\{ \begin{array}{ll}
-\mu g \displaystyle\frac{\mathbf{v}}{\|\mathbf{v}\|}
& \|\mathbf{v}\|>0 \\
0 & \mathrm{otherwise,}
\end{array} \right.
$$

where $\mu$ is the coefficient of friction and $g$ is the acceleration due to gravity.

Collision Dynamics

To determine the interactions caused by collisions between balls, the chart calls the MATLAB function interactionForce. This function implements a simple restoring force model when two balls come in contact with each other. The interaction force between the $i^\mathrm{th}$ and $j^\mathrm{th}$ balls is modal:

$$&#10;\mathbf{F}_{\mathrm{interaction}}(i,j) =&#10;\left\{ \begin{array}{ll}&#10;k_p (2R-\|\Delta \mathbf{p}\|)&#10;\displaystyle\frac{\Delta \mathbf{p}}{\|\Delta \mathbf{p}\|}&#10;- k_v\Delta \mathbf{v} &#38; \|\Delta \mathbf{p}\| < 2R \\&#10;0 &#38; \mathrm{otherwise,}&#10;\end{array} \right.&#10;$$

where:

  • $R$ is the radius of each ball.

  • $k_p$ and $k_v$ are constants of elasticity.

  • $\Delta \mathbf{p} = \mathbf{p}_i - \mathbf{p}_j$ is the relative separation of the centers of the two balls.

  • $\Delta \mathbf{v} = \mathbf{v}_i - \mathbf{v}_j$ is the relative difference in velocity between the two balls.

Because the balls are free to move in two dimensions, the chart uses the 16-by-16 Boolean matrix ball_interaction to account for all potential collisions. For example, when the $i^\mathrm{th}$ and $j^\mathrm{th}$ balls are touching, the value of ball_interaction(i,j) is true. Otherwise, this value is false. Because collisions are symmetric in nature, the chart uses only the upper triangular portion of the matrix.

Perform Matrix Calculations in MATLAB Functions

To compute the two-dimensional dynamics of the billiard balls, the Pool chart calls several MATLAB functions that perform matrix calculations.

  • initBalls initializes the position and velocity of every ball on the pool table.

  • frictionForce calculates the friction force acting on each ball.

  • interactionForce calculates the interaction force acting on each ball.

  • isAnyBallGoingToStop returns a value of 1 if any ball stops moving. Otherwise, the function returns a value of 0.

  • hasBallInteractionChanged returns a value of 1 if any ball interactions change. Otherwise, the function returns a value of 0.

  • isAnyBallNewlyPocketed returns a value of 1 if any ball falls in a pocket. Otherwise, the function returns a value of 0.

  • isAnyBallOutOfBounds returns a value of true if any ball lies outside the boundary of the pool table. Otherwise, the function returns a value of false.

  • nearHole returns a value of true if a ball is near a pocket on the pool table. Otherwise, the function returns a value of false.

  • getBallInteraction returns a Boolean matrix that specifies whether any balls are in contact with each other.

  • updateStopFlags keeps track of which balls have stopped moving and stores the result in the vector stopped.

  • pocketNewBalls sets the velocity of each pocketed ball to 0.

  • resetBallsPosAndVel resets the position and velocity of any ball that lies outside the boundary of the pool table.

View the Simulation Results

When you start the simulation, a UI shows a pool table with 15 billiard balls arranged at one end of the table. To specify the initial position of the cue ball, click anywhere on the pool table.

To specify the initial velocity of the cue ball, click a different spot on the pool table.

The model simulates the dynamics of the system and animates the motion of the billiard balls.

To stop the simulation, close the UI.

Related Topics