Community post
Advanced game programming - Simulate realistic weapons spread. With a good approach.
WarArm team (The video game team I’m part of) is currently trying to improve the weapon’s gun experience that must be ergonomic for the player and must reflect a soldier who in war tries to aim right on target. We noticed over time that the soldier’s fire was too random in the scope of the target and the propagation of the projectiles took place in an uncontrolled manner. How to fix the problem? How to control the randomness of the bullets? Someone would just say to increase the accuracy and thus reduce the range of randomness, but so it becomes too easy for the player. We want more control of the weapon, it needs to shoot more in the center and less on the sides.
Let me explain with pictures
This transformation get in input two-dimensional continuous uniform distribution to give in output a normal distribution. If x1 and x2 are our two random generated values between 0 and 1, we can use these random variables in this formula to get a normal distributed value:
At the same formula can be applied the cosine
Code example in C#
double RandomNormalDistribution(double mean = 0, double stdDev = 1f) //mean = 0 is the center of the screen
{
//uniform in (0,1] interval
double x1 = (1.0 - rand.NextDouble());
double x2 = (1.0 - rand.NextDouble());
double randStdNormal = mean + stdDev * (Math.Sqrt(-2.0f * Math.Log(x1)) * Math.Sin(2.0f * Math.PI * x2)); //normal(mean, stdDev)
return randStdNormal;
}
This is an example of use:
//You can change the accuracy value (remember to leave the mean to zero, is the center of the screen)
double ErrorX = RandomNormalDistribution(stdDev: Accuracy);
double ErrorY = RandomNormalDistribution(stdDev: Accuracy);
The two error variables (ErrorX and ErrorY) will be used to define the point of the shot. Firing several shots you will notice that they will tend toward the center! It will no longer be a totally random shot. I wonder if the best games on the market will use a similar technique.
What do you think of this idea?
Replies (10)
Sono una frana a matematica... ma super-interessante quello che fai.
Finché rimane virtuale, tutto ok ;-)
You rock 😊
Il mondo virtuale esiste per sperimentare le proibizioni del mondo reale che vanno dai limiti del nostro corpo ai limiti fisici, a dimensioni spaziali diverse e tutto quel che si può immaginare, infatti è come giocare con ciò che ha immaginato chi ha programmato il gioco. Entriamo nella finzione per conoscere quello che non potremo mai fare e non è meraviglioso tutto questo? E' un modo per allargare gli orizzonti creativi, per porsi nuove domande.
Ovviamente quello che posso dire è che non bisogna esagerare al tal punto di finire per confondere la realtà con la finzione, reputando la finzione più vera della realtà stessa.
Grazie del commento, e ricorda che la matematica è la regina delle scienze, utile ovunque e dà senso ad ogni cosa, una volta compresa ovviamente :D !
Thank you
Grazie mille per la risposta dettagliata ;-)
Io personalmente mi baso su principi buddisti... causa-ed-effetto... azione-reazione.
Son messo male con la matematica.
P.S. è da un po' che non gioco ai videogames... Utilizzavo il Commodore64 fino alla Play2... poi qualche app game in android.
Nun je la posso fa'.
Non ho ben capito l'ultima sillaba del tutto.Ahaahhaha, complimenti !
Tutto chiarissimo .-.
Ti chiedo un chiarimento di una cosa. Se ho ben capito per approssimare una distribuzione gaussiana utilizzi una formula che necessita in imput di due numeri casuali tra 0 e 1. Quei numeri casuali come vengono generati? La logica che usa il computer per stabilire quale numero far uscire casualmente può incidere significativamente su quanto è corretta l'approssimazione che stai cercando di ottenere? Ci sono funzioni che generano numeri causali "più casualmente" di altre o comunque siano più utili al tuo scopo?
Bel post comunque, solo per la gaussiana meriteresti un centinaio di voti :)
Ciao, le funzioni standard dei vari linguaggi generano numeri pseudocasuali con probabilità uniforme, queste sono testate affondo da chi le ha create attraverso dimostrazioni matematiche che verificano sia effettivamente così. Il teorema di Box-Muller è infatti molto potente perché presi in ingresso due numeri casuali con probabilità uniforme permette di avere altri due numeri con probabilità su gaussiana! Quindi, dato che un computer non può generare numeri su gaussiana, ma solo numeri con probabilità uniforme, questa permette di avere valori normali attraverso quelle formule matematiche.
"Ci sono funzioni che generano numeri causali "più casualmente" di altre o comunque siano più utili al tuo scopo?" Ci possono essere funzioni più efficaci, che generano un grande intervallo di valori casuali, ma ricordiamo sempre che i valori sono pseudo casuali, cioè prevedibili, generati da un seed di input.
Grazie!