# Crash, Bang, Wallop! – Collisions This session we are going to add collision detection between our particles and make them ‘bounce’ off one another. This will actually come down to 3 separate tasks.

Firstly we need to know when to particles have collided (collision testing, or detection).

Secondly we need to change the behaviour of the particles to respond to the collision - for now this will simply be to make them bounce apart.

Thirdly we need to catch a tricky problem common to all physics simulations – stickiness! Because we will be testing for collisions at discrete moments in time (whenever our collide function is called in the main game loop) there are going to be occasions where we have just missed the actual moment of collision. At this point our particles will be overlapping and begin to repeatedly trigger our collision test make the particles ‘bounce’ constantly in opposite directions each call – the visual effect of this will be to make the particles appear stuck together. To avoid this problem we will also add a bit of code to ensure that, in addition to causing the particles to bounce, we also make sure we remove any overlap.

## Checking all the particles once, but only once!

Lets start with how we are going to call our collision code. Naively, for each particle we could just insert a second loop to check against the particle list particle by particle – but wait… A simple nested loop will actually check every *pair* of particles twice – A with B, and then later B with A. We want to avoid this duplication as well as avoid checking particles against themselves – A with A and B with B etc.

To achieve this we will use a modified internal loop that only checks the particle against the other particles *that are later in the list*. To do this we need to modify the way we loop through the list in the outer loop so that we can keep track of the position in the list of the particle we are currently checking. Find the existing update-draw loop for each particle and replace it with this code:

for i, particle in enumerate(my_particles):

particle.move()

particle.bounce()

for particle2 in my_particles[i+1:]:

collide(particle, particle2)

particle.display()

Can you see how this works? Enumerating the particle list means that the variable i will keep track of the index of each particle in the list as we iterate through it. We can use i to begin the nested for loop from 1 place further on in the list than the particle being tested. The function that we call – collide – will test to see if the pair of particles are in collision and act upon them if they are. Let’s look at that next.

## Collision Testing & Handling

The collide function should be added under the existing findParticle function, before the Particle class definition. Here is the collision code in its entirety:

def collide(p1, p2):

dx = p1.x - p2.x

dy = p1.y - p2.y

dist = math.hypot(dx, dy)

if dist < p1.size + p2.size:

tangent = math.atan2(dy, dx)

angle = 0.5 * math.pi + tangent

angle1 = 2*tangent - p1.angle

angle2 = 2*tangent - p2.angle

speed1 = p2.speed*elasticity

speed2 = p1.speed*elasticity

(p1.angle, p1.speed) = (angle1, speed1)

(p2.angle, p2.speed) = (angle2, speed2)

# Move overlapping particles apart

separation = (p1.size + p2.size - dist) / 2.0

x_separation = math.sin(angle) * separation

y_separation = math.cos(angle) * separation

p1.x += x_separation

p1.y -= y_separation

p2.x -= x_separation

p2.y += y_separation

Initially you can see that we simply find the difference in x positions (dx) between the particles’ centres and the difference in y (dy). We use basic Pythagoras to find the distance between the centres of the particles (the hypotenuse of the dx dy triangle).

The collision test is now simply to see if this distance is less than the combined sizes of the particles – this should all be familiar to those of you who worked through the asteroids game.

If there is a collision, we change each particle’s movement angle and make them appear to bounce – I will go through the theory in the session.

## Parting is such sweet sorrow

Finally, we need to deal with the ‘sticky’ problem of intersecting. If, at the moment we check them, the particles have already overlapped, then simply changing their speeds and angles is going to leave us in a tricky state. On the very next loop around our code, and despite the fact that they are now moving apart, we may well find them in collision again (because they were overlapped) and ‘bounce’ them again… and again… and again. This causes a very obvious glitch. To solve it we must ensure that we not only leave our particles moving apart, but also ensure that they are forcibly separated before the next time around the loop. We do this in the code by finding the separation required to ensure that the particles are *not* overlapped and then we simply force their positions apart by setting the particles’ x y values directly.

Once you have the code working, try commenting this section of code out and see what happens without it, Can you see and understand why the problem occurs? It’s a good one to think about as similar issues can often turn up in timing, or sampling, loops.

## Projects for the Holiday

Hopefully you can see that you have an interesting ‘engine’ for a whole variety of games at the end of this session – see what you can do with it over the summer and I look forward to playing some of your games next term.