**Notes:**

- As a final project for one of my Computer Science courses I made a Google App Engine game of billiards where you can interact with the mathematical derivations from this post. It requires two players, so you’ll need to navigate to the web page in two separate browser tabs if you don’t have an opponent. Visit http://our-mediator-166519.appspot.com/ to play.
- Additionally, all of the images from this post were rendered using some of the components I developed for this project. You can view the full source on my Github repository.

I’ve been allowed some creative freedom with a final project for one of my computer science courses. Naturally, I’m going to make a video game. This has led me to think lately about some of the ensuing technical hurdles and mathematical tools I’ll need to develop in order to get the project underway.

**Matrix determinant and parallelogram area
**

I’ll begin by defining a vector operation, , where

.

The operation is just a 2×2 matrix determinant, but without getting too technical, I’ll just think of it as an operation on two vectors that gives a number, the absolute value of which is the area of the parallelogram that they subtend.

Note that the operation is not commutative, it is anti-commutative.

This is a useful property of the operation. if is positive, then I know the area of the parallelogram that they subtend, but also that vector b is oriented counter-clockwise with respect to vector a. On the other hand, if is negative, then I know that vector b is oriented clockwise with respect to vector a.

Also useful to know, the operation is left and right distributive over addition/subtraction:

also,

I will be using a b notation throughout this post to save typing. In addition to this, I will use more familiar notation for other common operations, such as:

Dot Product

and Euclidean norm

**What is the height of the blue vector over the red vector?**

To find the length of the yellow line, I’ll begin by finding the area of the parallelogram subtended by the blue and red vectors.

Half of this area gives the area of the triangle defined by the blue and red vectors.

The length of the yellow line is the height of this triangle, with base equal to the length of the red vector. I can find the height of the yellow line with the familiar formula,

Substituting for and the base of the triangle gives

Note that this operation is related to the formula for vector projection,

When paired together, the two operations satisfy the Pythagorean theorem:

Together they give the base and height, respectively, of the generalized right triangle formed by two arbitrary vectors.

**When will the circle run into the edge?**

The circle will run into the edge when a line (yellow) drawn perpendicular from the edge to the center of the circle has a length that is equal to the circle’s radius.

I’ll use the operation defined earlier to get the height of the circle, c, above the edge, e

As circle, c, travels along its velocity vector, v, through time,

I’ll make this substitution for and and solve for time t, when the height of the yellow line is equal to the radius of the circle, .

With a little algebra, the time at which the circle runs into the edge,

This can be written more compactly:

The plus or minus symbol in the formula arises because the circle can be in contact with the line segment from both above and below, or more accurately, when the circle is oriented counter-clockwise and when it is oriented clockwise with respect to the line segment.

**In what direction should the circle be deflected off of the edge?**

The circle should be deflected in the direction of a new velocity vector whose component that is perpendicular to the edge has been negated, but whose component that is parallel to the edge has remained unchanged.

With respect to the edge, e, the perpendicular and parallel components of the velocity vector before deflection, , are respectively

And so the logic above gives the following system of equations for the velocity after deflection, :

i.e.

Because it will appear in other calculations, for the sake of reuse I’ll state that the coefficient matrix of this system has inverse,

Multiplying both sides of the system gives the solution,

i.e.

**When will the two circles collide with one another?**

The two circles will collide with one another when the distance between the centers of the two circles is equal to the sum of their radii.

As circles A and B move in accordance of their velocity vectors,

Then

gives

let

Solving for t with the quadratic formula gives two solutions.

The solution that is the smallest positive value gives the time at which the soonest collision occurs.

The solution that is the largest positive value gives the time at which another collision would occur should the circles pass through one another, rather than bounce away from each other.

If the two circles are not going to collide, t will either be a complex number or infinity.

**How should the two circles be deflected away from one another?**

The component of the circle’s velocity vector that is perpendicular to an edge connecting the center of the two circles should remain constant, while the component of the circle’s velocity vector that is parallel to the edge connecting the centers should be the result of a one-dimensional elastic collision.

Let a vector, e, extend from the center of circle A, to the center of circle B.

Obtain the projection, , of A’s velocity vector on vector e.

Also, obtain the projection, , of B’s velocity vector onto vector e.

In an elastic collision, where circles have mass, and the final velocities will have projections onto edge, e,

Now Circle A’s velocity after deflection can be described with the system,

i.e.

Again, the coefficient matrix of this system has inverse,

multiplying both sides gives

i.e.

Use the same process to determine the velocity of the other circle after deflection.

**Parametric Lines**

A useful parameterization gives points along a line defined by endpoints and

This parameterization has the property that for values , is on the line segment, between the two endpoints. On the other hand, if t is less than zero, or t is greater than one, is outside of the two endpoints.

Below are points (from left to right) for t = -0.5 in red, t = 0, 0.25, 0.5, 0.75, and 1 in green, and for t = 1.5 in red again:

**At what point do two line segments intersect?**

Lines A and B intersect for parametric values s and t, such that

I’ll use the parametric equation of lines A and B and solve for the values s and t for which the equality is satisfied.

then

In matrix form

If I define vectors

The solutions can be expressed as simple ratios of determinants.

If the line segments do intersect, they do at point

If either s or t are not in the range of 0 to 1, then the line segments do not cross. If the line segments do not cross because they run parallel to one another, the determinant must equal zero.

**How can I tell if an object is enclosed within a region?**

If, by protocol, regions are polygons defined with counter-clockwise directed edges, an object inside the region is then oriented counter-clockwise with respect to all the edges that define the region, whereas an object that is outside of the region must be oriented clockwise with at least one of the region’s edges.

With the operation defined earlier, it is possible to determine this property. While a directed edges aren’t vectors, the coordinates of the involved objects can be transformed to the origin and thought of as such.

bool aIsCounterClockwiseToB(point a, edge b) { int u_x <- b.x2 - b.x1 int u_y <- b.y2 - b.y1 int v_x <- a.x - b.x1 int v_y <- a.y - b.y1 int A <- u_x*v_y - u_y*v_x return A > 0 }

bool aIsWithinRegionB(point a, edge[] b) { bool isInRegion <- true for edge in b { if !aIsCounterClockwiseToB(a, edge) { isInRegion <- false } } return isInRegion }

Of course, regions could be defined with clockwise oriented edges and the reverse of the above algorithms could determine if an object is enclosed or not, just as well. But note that this algorithm will only work for convex polygonal regions. In order to determine if a point is inside of a concave polygonal region, it would have to first be broken down into several convex polygonal regions.

More to follow…