## Stretch of imagination

Let's now take a look at that catapult. It's an elastic cord and the further we pull it, the more tension is applied, and the more rapid the acceleration when we let go and the cord snaps back. This in turn imparts the initial velocity to the bird once the acceleration due to the tension is dissipated. In essence, the further back we pull the cord the greater the initial velocity.

We could simulate the cord snapping back to rest. The relevant pieces are Hooke's law: the force exerted from the stretched cord is proportional to the stretched length, and Newton gave us F=ma, or the force is equal to mass times the acceleration.

In reality though, the player wouldn't be able to see anything - the action is over so quickly. It's easier from a programmer's point of view to code up a simple formula: the initial velocity is equal to the length of the stretched cord times some constant. Work out a good value for the constant through experimentation and move on to the next problem to simulate.

I would guess that the game player will always apply the maximum stretch to get the maximum initial velocity - this will provide the maximum damage to the pigs' edifices on contact.

## Collision physics

Since we're talking about what happens on contact, we should take a look at the physics of collisions. Here our old friend Isaac Newton is the master.

There are two parts to collisions when simulating them in a game on a computer. The first is how to detect a collision between two objects. This, to put it bluntly, is hard.

In *Angry Birds*, all collisions are between a moving object and a stationary one, the easiest case to simulate. Furthermore, I'm going to postulate that the reason nearly all the birds are circular is that it makes it a bit easier to detect a possible collision. Rather than provide a detailed discussion here of what's required to detect a collision, I'll just illustrate the problems.

First of all, the objects have a centre of mass, and it is the centre of mass that describes the path that the object takes. The object has a shape that extends around that centre of mass: to detect a collision you have to track the shape as the centre of mass moves along its trajectory.

The easiest shape to track is a circle, and it's probably easiest to just track the portion of the object in front. If you play a lot of *Angry Birds*, you'll have noticed that the game doesn't track the extremities perpendicular to the line of flight very well - some birds will fly close enough to an object to hit it, but will continue onwards without contact or deflection.

The second part of a collision is what happens because of the collision. This is known as the collision response. Here again we makes use of some simplifying assumptions.

The first assumption is that the objects colliding are treated as being rigid. What this means is that we assume that the objects do not deform when they collide.

The reason for this is to avoid all those tricky calculations about how much collision energy could be absorbed by something crumpling or denting or compressing. In other words, the collisions in the game are not like crashing your car into a wall, but are instead similar to two snooker balls colliding.

There is a fundamental principle at play here: Newton's law of the conservation of momentum. What this says in essence is that the sum of the momentums of both objects just before they collide is equal to the sum of the momentums just after.

For this law to apply, the momentum is assumed to be a vector - that is, the momentum has a direction as well as a magnitude (if you think about it, velocity is a vector as well, as is acceleration). The momentum of an object is its velocity multiplied by its mass.

This is now where we fudge things a little in the game. For a rigid body to collide with another, there's going to be a change of direction for both of them. Their velocities will change. In order to change a velocity we have to apply an acceleration, which in turn implies that a force has to be applied over a period of time. But, since 'rigid' implies 'non-deformable' we have no time. The change in velocity is immediate.

To counter this problem we use a new quantity called the impulse. This is equivalent to a very large force applied over a very small time, and is essentially a way for us to get around the idealisation of a rigid body. (Think about what happens at the atomic level when two snooker balls collide: there is some complicated interaction between the various electric fields of the atoms of the two balls to cause a repulsive force. Just because we want a perfect rigid ball doesn't mean that it actually is.)

We can then calculate the impulses at the collision point and apply them to change the two bodies' velocities without them deforming.

## Act on impulse

The way to calculate the impulses is determined by the realism you want to achieve. The simplest model to use is known as Newton's Law of Restitution.

Here we postulate a coefficient of restitution that models the elasticity of the collision and defines the relation between the incoming and outgoing velocities or the amount of energy absorbed by the collision.

A perfect elastic collision has a value of one and describes the collision between two perfect snooker balls. A perfect inelastic collision has a value of zero and basically describes the collision between a lump of wet clay and a wooden floor: splat.

Without going into detail, you have to calculate the perpendicular to the point of collision, the normal. It's along this line the collision occurs, and with *Angry Birds* we're generally talking about a circle - the bird - hitting a straight edge - the plank, sheet of glass, and so on. The normal is perpendicular to the straight edge.

Using some relatively straightforward mathematics, we can work out the relative velocities along the normal, the impulses that apply, and hence the new relative velocities after the collision.

Figure 2 shows a stylised view of a collision between a red bird and a plank of wood. This example also shows that the momentum transferred to the wood can also cause a rotation, creating angular momentum as well.

*Angry Birds* does fudge some of this detail to a certain extent: there are explosions, smashings, bonus points, clouds of feathers and the like, all of which help to disguise the somewhat unrealistic collisions.

All in all though, *Angry Birds* is an excellent example of how to use physics to produce realistic and engaging two-dimensional gameplay.