2D-Collision response

Description
This article covers what to do when we know a collision has occured using what is usually called the impulse method. It assumes you have some kind of collision detection and that it supplies you with some data about the collision. First we need to know which object that collided and where. The relative velocity of the objects is also necessary. In order to determine the direction the objects will "fly" away we must know the normal of the collision. This is usually the normal of the edge that we collide with. When we know this we are ready to handle the collision.

The math shown here might look complex to some but we will explain all of it.

Without rotation
Eq 1a: $$\mathbf{v}_2^A = \mathbf{v}_1^A + \frac{j}{M^A}\mathbf{n}$$

Eq 1b: $$\mathbf{v}_2^B = \mathbf{v}_1^B + \frac{j}{M^B}\mathbf{n}$$

Eq 2: $$j = \frac{-(1+e)\mathbf{v}^{AB}\cdot\mathbf{n}}{\mathbf{n}\cdot\mathbf{n}(\frac{1}{M^A}+\frac{1}{M^B})}$$

where
 * A and B is used to refer to each object
 * $$e$$ is the bounciness.
 * $$\mathbf{v}$$ is the velocity of each object.
 * $$\mathbf{v}^{AB}$$ is the relative velocity of the objects colliding.
 * $$\mathbf{n}$$ is the collision normal, usually perpendicular to one of the polygons.
 * $$M$$ is the mass of each object.

Static objects
If one of the objects colliding is static (as in inmovable) suppose it has infinitive mass. As the mass goes towards infinity $$\tfrac{1}{M}$$ goes towards zero. Thus it is possible to simplify equation 2 to $$j = \frac{(1+e)\mathbf{v}\cdot\mathbf{n}}{\mathbf{n}\cdot\mathbf{n}(\frac{1}{M})}$$.

With rotation
Eq 1: $$\mathbf{v}_1^{AP} = \mathbf{v}_1^A + \mathbf{\omega}_1^A\mathbf{r}_\bot^{AP}$$

Eq 2a: $$\mathbf{v}_2^A = \mathbf{v}_1^A + \frac{j}{M^A}\mathbf{n}$$

Eq 2b: $$\mathbf{\omega}_2^A = \mathbf{\omega}_1^A + \frac{r_\bot^{AP}\cdot j\mathbf{n}}{I^A}$$

Eq 3: $$j = \frac{ -(1+e)\mathbf{v}_1^{AB}\cdot\mathbf{n} }{ \mathbf{n}\cdot\mathbf{n}( \frac{1}{M^A}+\frac{1}{M^B}) + \frac{(\mathbf{r}_\bot^{AP}\cdot\mathbf{n})^2}{I^A} + \frac{(\mathbf{r}_\bot^{BP}\cdot\mathbf{n})^2}{I^B} } $$

where
 * A and B is used to refer to each object
 * $$e$$ is the bounciness.
 * $$\mathbf{v}$$ is the velocity for each object.
 * $$\mathbf{\omega}$$ is the angeluar velocity for each object.
 * $$\mathbf{v}^{P}$$ is the velocity in the collision point for each object. Remember that the velocity in a point of the object is not equal to the velocity of the object!
 * $$\mathbf{r}_\bot^{P}$$ is the tangent of the point of collision for each object.
 * $$\mathbf{v}^{AB}$$ is the relative velocity of the objects colliding.
 * $$\mathbf{n}$$ is the collision normal, usually perpendicular to one of the polygons.
 * $$M$$ is the mass for each object.
 * $$I$$ is the inertia. See below for calculation.

Moment of Inertia
The moment of inertia, I, is a measure of how difficult it is to change the rotational velocity (i.e. spin) of an object. It is analogous to the inertia that mass provides in Newton's 2nd Law: F = ma (using MoI: τ = Iω, where τ is the torque and ω is the angular velocity)

Rectangle
$$I = \frac{1}{12}m(a^2+b^2)$$

Where a and b is the width and height of the rectangle.

Circle
$$I = \frac{1}{2}mr^2$$

Where r is the radius of the circle.

Sphere (Solid)
Many game objects should be represented as solid spheres, even though they appear on the screen as circular objects (2d projection of a sphere).

$$I = \frac{2}{5}mr^2$$