Tuesday, October 10, 2017

game design - Accuracy vs distance formula


Consider a shooter and a target. My question is if there is any realistic approximation of calculating with which probability the shooter hits the target.


Neglecting the weapon and shooter skill, I guess the first order approximation should be that the probability to hit would be proportional to 1/r^2, where r is the distance to the target.


The motivation to this relation comes from using the idea that the area of a sphere centered at the shooter decays as r^2. So, the probability to hit a target should be, in worse case scenario, decaying as 1/r^2.


I've tried Googling some relation for any weapon, but I didn't find any...


Does anyone knows more information about this topic? Is this approximation valid?


EDIT:



More on the question: I'm considering a tactics game. In particular, I would like to model the shooting between two units (so, not a FPS game, the player does not aim, it issue orders). To do that, I'm considering that the unit has some experience, the weapon has some accuracy, and the environment (foggy, vegetation, etc) effects the overall accuracy. Before working some difficult model, the simplest model to test is considering all factors constant, and accuracy only depending on distance.


The question is how does this accuracy should depend on the distance. My first guess would be a 1/r^2 decay. But, has well mentioned on comments, this looks like a very rapid decay.



Answer



Your approximation basically dictates shots are landing on a part of the surface of a sphere, determined by angle; the target area within that surface is a constant; the probability distribution is constant within the surface and zero elsewhere.


Gajet has already given a number of good reasons why some of these assumptions fall flat, but holds on to the same inaccuracy model: a bounded error in angle. The result still falls off with r^-2, but with a small constant.


Say the shooter has a maximum spread of 5°. He has a chance of firing between a 0° and 1° error, but the area of the ring between 4° and 5° is much larger than the area of the ring/circle between 0° and 1°. Larger errors have a larger probability of occuring. Increase the error any further, and the probability suddenly drops to zero, because we're out of the five degree limit. That doesn't seem very realistic.


A more accurate representation would be to have a guassian distribution of angular error, i.e.: A(ϕ) = sqrt(a/π) exp(-a ϕ²). The variable a can be used to include the shooter's skill etc. Note this solution is one-dimensional. If your target is very tall compared to its width, you might omit the vertical error altogether and just assume the shot landed at the correct elevation. Alternatively, you can run the calculation twice and multiply the result, assuming the target is roughly rectangular.


To get from the probability function to the actual probability of hitting a target, we integrate the function A and end up with an expensive error function - which is actually called the error function: p(ϕ) = erf(ϕ sqrt(a)). The angle ϕ equates to the angle between the targeted point and the target's edge. In terms of target size s and distance r: p(r) = erf(arctan(s/2r) sqrt(a)). This function is plotted below for a target of size 1 and accuracy values of a=2 and a=10.


Probabilty of hitting target versus distance


Note that unlike a falloff of r^-2, the probabilty neatly stays below one, no matter how close the target is. In fact, even a target at exactly zero distance can be missed, due to the extremely small probability the error is more than 90°.



Like I said before, the error function is quite expensive, but its argument ϕ sqrt(a), doesn't vary all that much for any sensible shooter scenario. We can do a lot better by evaluating part of the Taylor series instead and capping the result. First, we map x = arctan(s/2r) sqrt(a), then evaluate: 2 x - (2/3) x^3 + (1/5) x^5 .... Omit or add as many terms as deemed necessary, but keep in mind an even number of terms will cause unwanted behavior for low distances. Plotted below is the real error function, compared to the first three non-zero terms of its Taylor series.


Probability using error function and Taylor series


As a final note, this is purely mathematics. Throw in a couple of sine functions, random coefficients and logarithms and your game might be equally fun.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...