- Home /
Gaussian Distribution in circle on x and y axis
Hey, so I have a weapon script which is exactly like CS:GO, except for the weapon spread. As you know if you've played said shooter, you know that your bullets become less accurate the longer you hold the fire button. My script can do this, but it does so in a square using normal distribution.
I want it to instead be in a circle and using gaussian distribution! I don't know quite how gaussian distribution works yet, but Ive found a short script that does it, but I can't figure out how to do it in a circle!
Here is the necessary parts of my weapon script:
//NOT MADE BY ME
public static double NextGaussianDouble()
{
double U, u, v, S;
do
{
u = 2.0 * Random.value - 1.0;
v = 2.0 * Random.value - 1.0;
S = u * u + v * v;
}
while (S >= 1.0);
double fac = Mathf.Sqrt(-2.0f * Mathf.Log((float)S) / (float)S); //I just changed the doubles to floats. Is there any point in using doubles at all in this case?
return u * fac;
}
That was the gaussian distribution, and here is the part that makes the direction:
w.CurSpread += 1.0f/w.MaxAmmo;
w.Spread = w.SpreadSpeed.Evaluate(w.CurSpread)*w.MaxSpread;
float spread = w.Spread * (float)NextGaussianDouble();
Vector3 x = ((weapon[selected].right * w.PatternX.Evaluate(w.CurSpread) * w.Recoil.x) + (weapon[selected].right * spread));
Vector3 y = ((weapon[selected].up * w.PatternY.Evaluate(w.CurSpread) * w.Recoil.y) + (weapon[selected].up * spread));
Vector3 direction = (x + y + weapon[selected].forward*100);
This version of the script nails the distribution, but it sprays it in a diagonal line! I want it to spray in a circle! Please help!
$$anonymous$$an, thanks! I am sure I remember your username from some other post of $$anonymous$$e? Would you $$anonymous$$d converting your comment to an answer so I may accept it?
I did want it to spread in a circle, but I decided to put that idea on a shelf for now.
Answer by _Gkxd · Jul 30, 2015 at 06:33 PM
Without looking into the details of your code, I can see that you're only calling NextGaussianDouble
once. If this is the case, then you are only going to get a distribution in one dimension.
If you want a two dimensional distribution, you need to call NextGaussianDouble
more than once. How you use these values is up to you, but generally you would have a "x" amount and a "y" amount.
If you use the same value for your "x" and "y" amounts, you will get a one dimensional distribution on the diagonal line y = x. I think this is what's happening, since you only seem to be calling NextGaussianDouble
once.
Your answer
Follow this Question
Related Questions
Why does inside unit circle use the world point? 2 Answers
Random Coordinates Outside a Circle 1 Answer
Gun accuracy with Random.Range? 1 Answer
Gun Random Rotation 0 Answers
random.onUnitCircle ? 6 Answers