Weapons, in a lot of games they are very important. And although they might seem quite simple (you just push a button and it works), there is quite a bit more to them. To illustrate this, I’ll give a detailed explanation of how I programmed the shotgun in my arcade game.

# The bullet

### Firing the bullet

There are three main parts to this gun, the gun itself (which isn’t that interesting as it is just a sprite), the bullet and the shell that flies out of it. Everytime the player pulls the trigger, eight bullets come out that all fly off in a “random” direction. This direction is actually a speed vector $\vec{v}$, of which the y-component $\vec{v}_y$ is randomly chosen between $-2.5$ and $2.5$. But as every bullet must have the same speed, which in this case means a speed vector of equal length, we cannot simply set the x-component of the speed vector, as this has to be calculated:

Translated to the beautiful C++ language, it looks something like this:

OK, simple enough so far, you might think. But no, it gets better. Every shotgun bullet also has horizontal drag $d$, so it will slow down, and for the effect, the drag increases every update cycle. OK, still not that hard, we’ll just update the horizontal speed:

But now the direction of the vector has changed, and as that’ll look weird, we’ll also have to update the vertical component:

After recalculating the speed vector $\vec{v}$ we can also recalculate the position vector $\vec{p}$ (but only if the speed vector is still positive):

So, we’ll put this in our C++ code as well:

Also, before we can fire a bullet, we need to make sure we haven’t just already fired one. In order to do this, the Shotgun class contains a cooldown variable. For every step this cooldown is updated:

Every time we fire, we set this variable to $60$, so the player will have to wait about two seconds before being able to fire again. Firing then is quite straightforward:

### Making the bullet hit something

So, when the bullet is away, we’ll have to check if the bullet hit something. First, we have to check for hits with the level, which is quite easy as the level itself has a global Collide* method, which returns the x-position of the hit, or -1 if there was no hit:

We then have to check for a hit with an enemy, but for this there is another method implemente,d IntersectEnemy, which returns a pointer to the intersecting enemy, or NULL if there was no enemy. If there was a hit, the enemy needs to be hit with a damage of 1 HP, and with bullet (kBullet)-damage (which is used for the death animation)

We then render the bullet:

# The shell

Though the shell flying away from the gun after firing is just a simple effect, it has quite some code behind it. Just like the bullet it has a speed vector $\vec{v}$ and a position vector $\vec{p}$, but as it spins, it also has an angle $\alpha$ and an angular velocity $\omega$. The bullet is also under the influence of the gravity which exerts a constant pull $g$ of 0.8 (which is just a random number )

Every step we first update the position vector, which is simple for the shell, as it has no drag:

Then comes the angle:

And then we update the speed vector according to the gravity:

Translated to C++, it looks like this:

Rendering works just like the ShotgunBullet, except a different function is used to render a rotated texture:

And like that, you see how complicated programming a simple shotgun in a 2D platformer can be. I’m already imagining the fun programming the flamethrower must bring me