# Creators: Shootah Part 4 – Colliders

This week we mainly dealt with building and using a box collider in our game. The box colliders are written in a way such that:

• They are connected to something in the game and follow it around
• What they are attached to must have the properties x and y for position
• It is possible to test if they are touching each other
• If two colliders are found to be touching, we tell the things that they’re attached to that they’ve been hit by something
• What they are attached to must have a function hit() that we can call

Extents of the Collider

Our collider is a box, of a given width and height, centred on the x and y of the thing it’s connected to:

For checking collisions, we need to know the values of the left and right side of the box and the y values of the top and the bottom of the box.

For convenience, we write a function which returns an object (using curly brackets) with the left, righttop, and bottom values (shortened lr, t and b respectively) as properties:

```extents(){
return {
l: this.connected.x - this.width / 2,
r: this.connected.x + this.width / 2,
t: this.connected.y - this.height / 2,
b: this.connected.y + this.height / 2
};
}```

When making an object like this, we set a property value by first writing the property name, followed by a  colon (:)  and then a space and the value we want it to have. Each property is separated with a comma. The don’t need to be on separate lines, but it makes it easier to read.

Touching Colliders

So how do we know that two colliders are touching? Actually there are four ways in which they definitely can’t be touching:

• One is completely to the left of the other
• One is completely to the right of the other
• One is completely above the other
• One is completely below the other

And if none of these are true, then it must be touching. So actually, we’re going to check that they’re not NOT touching (double negative, used correctly!).

How do we know if something is completely to the left of something else? Look at this diagram:

We know that box 2 (in blue) is totally to the left of box 1 (in orange) because we can see it is, but how could get the computer to check it? Remember, left and right are just x values on the screen. Box 2 is left of box 1 because both it’s left and right values are smaller than the left value of box 1.

The checks for the other directions are very similar:

• The second box is right of the first box when both of it’s x values (left and right) are greater than the first’s right side.
• The second box is above of the first box when both of it’s y values (top and bottom) are less than the first’s top side.
• The second box is below of the first box when both of it’s y values (top and bottom) are greater than the first’s bottom side.

Sending Messages

Each collider has a property disc that describes the thing, or type of thing, that it’s connected to.

All colliders know what they’re connected to, so when we determine two have touched, we call a function called hit() on each of the connected objects, passing it the desc of the other collider. This means, in our game, when our enemy is hit, it can know that it’s been hit by a bullet – or maybe something else – and react appropriately.

Checking Every Collider Against Every Other

In our code, we gather all the active colliders at each frame. We then need to check each one against each every other one. How can we do that?

Consider a list of four items:

To check them all against each other we first need to check 0 against the other three. Simple enough.

We we need to check 1. But we don’t need to check 1 against 0, since we already did that. Nor do we need to check it against itself. We only need to check it against 2 and 3.

If we write out the full sequence, we see that for four items we need three passes to check all combinations:

• First pass: Check 0-1, 0-2, 0-3
• Second pass: Check 1-2, 1-3
• Third pass: Check 2-3

We can write a pair of loops to do this:

```for (let i = 0; i < c.length - 1; i++){
for (let j = i + 1; j < c.length; j++){
c[i].touching(c[j]);
}
}```

Note two things about these loops:

• The first loop goes from zero, but stops one short of the last item in the list (i < c.length – 1). It picks the first item to be checked.
• The second loop doesn’t start from zero. It starts from what ever i currently is, plus one. It picks the second item to be checked.

Other Stuff

We also did a few other things this week:

1. We fixed a small bug that was keeping our spaceship moving when we didn’t want it.
2. We added a little drag to slow the spaceship down to a stop when we lift our fingers off the keys
3. We set bullets inactive when they hit something