(Perfectly) Elastic Collision Response Between Circles

Intro

Now that I’ve covered Circle Collisions, I’m going to talk about how to resolve the collisions we’ve detected. First, we’re going to have to advance the entire simulation until the collision happens, then we’re going to adjust the velocities of both the involved entities. This is called the collision response.

In this post, we’re going to talk about elastic collisions, these can also be called totally or perfectly elastic collisions.

Intuition

For this post, we’re going to have to dive into some mechanics. Let’s first define a few things:

  • is the momentum of an entity of mass with velocity
  • is the kinetic energy of that entity
  • is the impulse (difference in momentum) between momenta and

In every collision, momentum is conserved, so in a formula where are the momenta before and are the momenta after the collision:

This is an elastic collision, so in addition to momentum, kinetic energy is also conserved:

This formula states that no energy will be converted to something else, such as heat or potential energy.

Here is an animation of a perfectly elastic collision:

On the other side of the spectrum, there are perfectly inelastic collisions, these lose maximal kinetic energy to other forms of energy. This also has the effect that post-collision the objects will stick together. Here is an animation of a perfectly inelastic collision:

Elastic and inelastic collisions are not the only kinds of collisions that exist, there is an entire spectrum between both. We can model the elasticity of collisions using a restitution coefficient:

This is a coefficient between and that describes the elasticity of a collision, for the rest of this post I will assume that all collisions are perfectly elastic, so that . You can also view the coefficient of restitution as the “bounciness” of the collision.

In any collision, the impulse is applied along the collision normal, this means that the momentum vector, and because of that also the velocity vector, will only change along that normal. This is illustrated here:

This allows us to turn any 2d collision problem into an equivalent 1d problem.

Derivation

Prelude

Let

Since we can turn this into a 1d collision problem along the collision normal, let’s first calculate the normal:

Now let’s use the dot product to project the velocities onto the collision normal:

Both impulses are equal and opposite

The law of conservation of momentum states that:

Rearranging the law of conservation of momentum, we can prove that the impulses on both entities are indeed equal in magnitude and opposite in direction:

At collision, an impulse will be applied to both circles, so we can write this as:

Since we know that , we can define :

Finding the magnitude

Now we know that we have to find an impulse and that this impulse is applied along the collision normal, all that’s left is to find the magnitude of this , .

The kinetic energy along the collision normal is conserved, so we know:

When applying an impulse to an entity with velocity and mass , we get:

We can use this to substitute :

This is an equation with one unknown in , solving this for :

If you’d rather have it in a form with :

One of the solutions of this equation is 0, which is quite evidently not the answer because there is a collision response.

And if you’re curious about how the coefficient of restitution fits into this:

Putting everything together

Putting it all together, we get these equations:

Pseudocode

1
2
3
4
5
6
7
8
9
10
func collision_response(e1: entity, e2: entity)
  let m1 = e1.mass
  let m2 = e2.mass
  let n = (e2.pos - e1.pos).norm()
  let dv = e2.vel - e1.vel
  let du = dv.dot(n)
  let j = 2 * du * (m1 * m2) / (m1 + m2)
  e1.vel += (j / m1) * n
  e2.vel -= (j / m2) * n
end

Static Entities

You can model static/unmoving entities as entities with infinite mass:

Let:

Then we can solve the limit as goes to :

This impulse is only applied to one entity.

CodinGame

In physical CodinGame games, the collision response is applied in a two-step fashion, where the first step is unchanged but the second one has a minimum impulse of whatever the game defines:

You can pick whichever form you prefer, of course. :)

This means that the law of conservation of momentum is broken for low-impact collisions since energy gets added in that case.

Thank You
sethorizer
MrBrown77
Icebox
player_one