Page 1 of 1

## Putting Math In the Context of Game Programming Part IV- 1D Collisions

### #1 macosxnerd101

• Self-Trained Economist

Reputation: 9036
• Posts: 33,516
• Joined: 27-December 08

Posted 22 April 2011 - 09:17 PM

In this tutorial, we will examine a number of different collisions and how to deal with them mathematically. Some of the concepts accompanying collisions include energy, center of mass, impulse and momentum, conserved quantities, and calculus. I am assuming a basic familiarity with these concepts. The collisions we will be covering include single-object collisions, as well as one dimensional elastic and inelastic collisions.

Single Object Collisions (or more precisely, collisions with a wall)
Let's think of these as our single car accidents, or our tennis balls bouncing against a wall. In this system, we care about Impulse, which is the Integral(momemtum dt), or the change in momentum over a period of time. What happens in these collisions is that the object travels towards our brick wall (or equivelant), collides with it, then has some reaction. As an example, a tennis ball of mass 3 kg is travelling 10 meters/sec towards a brick wall. After colliding with the wall, the ball is travelling in the opposite direction at 7 meters/sec.

We know by Newton's First Law that the wall acts on the ball with a Force. Here is where things get interesting. How long did the ball have to stay in contact with the wall to slow down? What Impulse did the wall apply on the ball? These are variables that we, as the programmers, can control. As an example, if we want the ball to have a velocity of -7 m/s after leaving the wall, we know the ball has to decellerate -17 m/s^2. Based on this, we can determine the Force the wall applies on the ball based on the amount of time the ball was in contact with the wall. We can calculate: F = mass of the wall * acceleration. The acceleration = -17 m/s / numSeconds the ball was in contact with the wall. Pretty straight-forward.

What happens if our ball accelerates towards the wall according to functiong a(t)? We only care about the velocity at the point of impact to deal with momentum and impulse. So since momentum = Integral(Force dt) = Integral(mass * acceleration * dt), we can simply integrate the Force from 0 to time x, the time of the collision, to get instantaneous momentum.

One Dimensional Collisions- Elastic

Elastic collisions deal with conservation of energy. That is, if the total kinetic energy before the collision is equivelant to the total kinetic energy after the collision, the collision was elastic. The formula for kinetic energy of a single object is Ke = 1/2 * mass * velocity^2. So the sum of the Kinetic Energies of the individual objects before the collision is the same as after the collision.

As with all collisions, momentum is conserved. So the sum of the objects' momentums before the collision is equal to the sum of their momentums after the collision. Or mathematically:
```Sum(mass[i] * velocity[i]) = Sum(mass[f] * velocity[f])

```

Handling one dimensional elastic collisions is pretty simple. It simply comes down to defining a formula to modify the velocities of the objects while conserving momentum and energy. Identifying them when designing your game, however, is important because energy determines how much work an object can perform. With inelastic collisions, kinetic energy is lost, so certain objects will stop doing work sooner.

One Dimensional Inelastic Collisions

Inelastic collisions, unlike elastic collisions, do not conserve kinetic energy. However, they do follow conservation of momentum. There are two types of inelastic collisions- perfectly inelastic collisions and non-perfect inelastic collisions. Perfectly inelastic collisions occur when the two objects collide and stick together, whereas non-perfect inelastic collisions have a restution. That is, objects bounce and move elsewhere. In one dimension, this is one object moving forward, and the other backwards.

Let's take a look at the math for perfectly inelastic collisions. The math is pretty straight-forward, and it's easy to solve for the final velocity. Notice that if we distribute the vFinal and write out all the individual terms, this looks exactly like the conservation of momentum equation.
```m[1]vInit[1] + m[2]vInit[2] = (m[1] + m[2])vFinal

//Or in more general form
Sum(mass[i] * vInit[i]) = Sum(mass[i]) * vFinal

```

Non-perfect inelastic collisions have a coefficient of restitution to account for as well. There are a couple different formulas to calculate it, including:
```Cr = (vFinal[2] - vFinal[1])/(vInit[1] - vInit[2])
Cr = sqrt(reboundDist/distanceToCollision) (or sqrt(reboundHeight/dropHeight))

```

When calculating the finaly velocities, we take in the Coefficient of Restitution into account with the following formulas:
```vFinal[1] = [Cr * mass[2] * (vInit[2] - vInit[1]) + m[1]*vInit[1] + m[2]*vInit[2] ]/(m[1] + m[2])

vFinal[2] = [Cr * mass[1] * (vInit[1] - vInit[2]) + m[1]*vInit[1] + m[2]*vInit[2] ]/(m[1] + m[2])

```

Those final velocities will now allow us to take energy into account when describing an object's motion after the collision.

Conclusion
Hopefully this tutorial gives you a better idea of how to handle basic collisions. My next tutorial will discuss handling collisions in two dimensions.

Is This A Good Question/Topic? 4

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).