mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-10 20:05:54 +00:00
Gaussian Spread
This commit is contained in:
parent
f3a701d3f9
commit
2c72f36108
11 changed files with 84 additions and 11 deletions
40
Scripts/Utils/RandomStuff.cs
Normal file
40
Scripts/Utils/RandomStuff.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Utils;
|
||||
|
||||
public static class RandomStuff
|
||||
{
|
||||
public static double GaussianRandom(double mean = 0.0, double stdDev = 1.0)
|
||||
{
|
||||
// Box-Muller transform
|
||||
float u1 = GD.Randf();
|
||||
float u2 = GD.Randf();
|
||||
|
||||
// Ensure u1 is strictly > 0
|
||||
while (u1 <= 0f)
|
||||
u1 = GD.Randf();
|
||||
|
||||
float randStdNormal = Mathf.Sqrt(-2f * Mathf.Log(u1)) * Mathf.Sin(2f * Mathf.Pi * u2);
|
||||
return mean + stdDev * randStdNormal;
|
||||
}
|
||||
|
||||
public static float GaussianClamped(float mean, float stdDev, float min, float max)
|
||||
{
|
||||
float value;
|
||||
do
|
||||
{
|
||||
float u1 = GD.Randf();
|
||||
float u2 = GD.Randf();
|
||||
|
||||
// Ensure u1 > 0 to avoid log(0)
|
||||
while (u1 <= 0f) u1 = GD.Randf();
|
||||
|
||||
float randStdNormal = Mathf.Sqrt(-2f * Mathf.Log(u1)) * Mathf.Sin(2f * Mathf.Pi * u2);
|
||||
value = mean + stdDev * randStdNormal;
|
||||
}
|
||||
while (value < min || value > max); // reject out-of-bounds values
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
1
Scripts/Utils/RandomStuff.cs.uid
Normal file
1
Scripts/Utils/RandomStuff.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://tkwp2w30xi32
|
||||
|
|
@ -4,6 +4,7 @@ using System.Diagnostics;
|
|||
using Cirno.Scripts;
|
||||
using Cirno.Scripts.Components;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
|
||||
public partial class Weapon : Node2D
|
||||
{
|
||||
|
|
@ -123,6 +124,18 @@ public partial class Weapon : Node2D
|
|||
// Calculate angle offset for this bullet
|
||||
float spreadOffset = -halfSpread + (spreadStep * i);
|
||||
|
||||
// Add random spread
|
||||
if (WeaponData.RandomSpread > 0)
|
||||
{
|
||||
// Gaussian with mean = 0, stddev = WeaponData.RandomSpread
|
||||
spreadOffset += RandomStuff.GaussianClamped(
|
||||
mean: 0f,
|
||||
stdDev: WeaponData.RandomSpread, // tuning knob
|
||||
min: -halfSpread,
|
||||
max: halfSpread
|
||||
);
|
||||
}
|
||||
|
||||
// Rotate the ShootDirection by the spread angle
|
||||
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue