Skip to content

Collision Equations

Zorro edited this page Nov 24, 2021 · 6 revisions

Supplementary equations

Here are some useful equations that we will be using in our collision detection routines.

Distance equation

2D distance

The two dimensional distance between two 2D points is given by the Pythagorean theorem.

double Distance2D(double x1 , double y1 , double x2 , double y2) {
   return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

3D distance

Three dimensional distance is a simple extrapolation of two dimensional distance, by adding the squared delta z value to the square root.

double Distance3D(double x1 , double y1 , double z1 , double x2 , double y2 , double z2) {
   return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
}

Dot product

The dot product is useful in determining which side of a line a point is on. There are two useful forms of the dot product that we will be using in our collision calculations.

The dot product of two vectors a and b is given by :

a.b = ax*bx + ay*by
a.b = |a||b|cos(theta)

where theta is the angle between the two vectors and |a| and |b| are the magnitudes of vector a and b respectively.

Cross product

The cross product can be difficult to explain. Best leave it to wikipedia. See the link below.

https://en.wikipedia.org/wiki/Cross_product

General first degree line equation

A general first degree equation of a line is given by the following form :

Ax + By + C = 0

To find the equation of a line from two points separated on the axes by delta x and delta y, use these values for A,B, and C along with any point x1,y1 on the line :

A = -dy B = dx C = x1*dy - y1*dx

Circle equation 2D

The form of a 2 dimensional circle equation is as follows :

(x-j)^2 + (y-k)^2 - R^2 = 0

This defines a circle centered on j,k with radius R.

Back to Collision Detection

Clone this wiki locally