In a lot of video games, weapons, specifically guns are not always accurate. For example, in one of the modern rogue-like games, soul knight, weapons have a specific accuracy value (In the video, not all the bullets managed to hit the target). This value indicates how much the bullet might miss the target in degrees. In other words, when the gun is pointing at 90 degrees, the bullet will actually fire at a random angle, between 80 degrees and 100 degrees. That is, if we assume that the gun has 20 degrees of inaccuracy value.
Now this brings up a problem. What exactly does 20 degree of inaccuracy mean? How likely will the bullet actually hit its target? For one thing, a gun with a 0 degree inaccuracy will almost always hit its target. What about other inaccuracy values? I will explore this problem further, by making up a formula that will calculate the chance of the bullet hitting.
Look at the image above. It depicts a few circles, with the large circle representing the hit box of an enemy, and the smaller circles representing those of the bullets. Of course, not all games use circles as hit boxes, but at least this is mostly the case in soul knight, and many other rogue-like RPG games. Even if circle hit boxes are not used, they can still be used as very helpful estimates. Assume r1 is the radius of the enemy hit box, and r2 radius of the bullets.
If the small circle (B) is placed next to the large circle (A), their radii will combine, forming a larger circle (C). As long as the centre of the B (indicated by the yellow cross) is inside the area of the C, it means that the B and the A are touching.
Now that we have a simplified hit box detection, we can move on to the probability part. The chance of the bullet hitting the enemy, would obviously be the angle in which the bullet would hit, divided by the the angle that the bullet can come out. For example, when the gun is pointing at an enemy, and the angle in which it can hit the enemy is 20 degrees, and the gun inaccuracy is 40 degrees, the probability of the bullet hitting would be 1/2, or 50%.
Using some basic knowledge about circles and tangent, we know that the angle where the circle’s radius (R) and one of the black lines coming from the player meet, it would be a 90 degrees, or right. 2 right triangles are formed, as indicated by the diagram.
We then use the trigonometry to work out A. We will focus only on one of the two triangles, let’s say A1, and times it by 2 in the end. Assume the distance between the player’s centre and the enemies centre is D, and the radius of the area in which the bullet count as hit is R.
sin A1 = R/D
A1 = arcsin (R/D)
Now that we know the angle of one of the triangles, we times it by two to find the total.
Finally, we divide it by the angle of inaccuracy, B in this case.
2 x arcsin (R/D) / B
A helpful reminder: remember that R is not the radius of the enemy, but the radius of the enemy added with the radius of the bullet.
Great. So the formula is there. Try testing it on one of your favorite 2D shooting games, and you might be able to improve your skills. For example, by playing with this formula, we can easily find out what guns should be used at close range for maximum damage, and what guns can deal good damage from afar. Because I took into account the hit box of the enemy, you can even calculate how far you should stand during boss fights (bosses tend to have large hit boxes), and how far when dealing with more basic enemies.
There’s still a lot to improve of course, for example, I didn’t take into account the fact that the enemy is moving, and the enemy may dodge a shot that would otherwise be accurate, nor the fact that in some games, bullets and enemies do not have circle hit boxes. Try testing it on one of your favorite 2D shooting games, and have fun gaming at home.