## Question

## Transcribed Text

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

function [value, isterminal, direction] = check_billiards_collisions(~,state)% This function determines whether two billiard balls have hit the walls or

% each other. There are nine possible collisions that can occur: each ball

% with each wall, plus the two balls together.

%

% The input t is time in seconds.

% The input state is our state vector. It has eight elements, arranged in a column.

% The state vector's first element, state(1), is the horizontal position of the white ball's center, in meters.

% The state vector's second element, state(2), is the vertical position of the white ball's center, in meters.

% The state vector's third element, state(3), is the horizontal velocity of the white ball's center, in m/s.

% The state vector's fourth element, state(4), is the vertical velocity of the white ball's center, in m/s.

% The state vector's fifth element, state(5), is the horizontal position of the black ball's center, in meters.

% The state vector's sixth element, state(6), is the vertical position of the black ball's center, in meters.

% The state vector's seventh element,state(7), is the horizontal velocity of the black ball's center, in m/s.

% The state vector's eigth element, state(8), is the vertical velocity of the black ball's center, in m/s.

% Declare wall locations as global.

global leftWallX rightWallX lowerWallY upperWallY

global whiteBallRadius blackBallRadius

global whiteLeft whiteRight whiteLower whiteUpper blackLeft blackRight blackLower blackUpper whiteBlack

% Pull our positions out of the state vector. They are all we need.

xw = state(1);

yw = state(2);

xb = state(5);

yb = state(6);

% The value of each test is the distance from the ball in question to the

% object with which we are checking, making sure to subtract out the ball

% radius. These are all positive values that become zero when the

% collision occurs.

value(whiteLeft,1) = xw - leftWallX - whiteBallRadius;

value(whiteRight,1) = rightWallX - xw - whiteBallRadius;

value(whiteLower,1) = yw - lowerWallY - whiteBallRadius;

value(whiteUpper,1) = upperWallY - yw - whiteBallRadius;

value(blackLeft,1) = xb - leftWallX - blackBallRadius;

value(blackRight,1) = rightWallX - xb - blackBallRadius;...