BrettRyland / BDArmory

Gun turrets and other weapon systems for KSP
34 stars 23 forks source link

Shell Eject Velocity Control #642

Closed Spartwo closed 2 months ago

Spartwo commented 3 months ago

Config Example(Ejects forward as a naval gun)

shellScale = 1.4 shellEjectVelocity = 6.0, 0.0, 0.0

BrettRyland commented 3 months ago

Since it appears to have mostly become lost in the noise of discord, what I was proposing is to make the following additional changes to the PR so that the variability of the ejected shells' trajectories is larger in the direction of the ejection direction (similar to how they behaved before — see the pictures in that link).

diff --git a/BDArmory/FX/ShellCasing.cs b/BDArmory/FX/ShellCasing.cs
index e7059ba55..589232ea2 100644
--- a/BDArmory/FX/ShellCasing.cs
+++ b/BDArmory/FX/ShellCasing.cs
@@ -10,22 +10,21 @@ public class ShellCasing : MonoBehaviour
         public float startTime;
         public Vector3 initialV;
         public Vector3 configV;
+        public Vector3 configVar;

         Vector3 velocity;
         Vector3 angularVelocity;

         float atmDensity;
-        const int collisionLayerMask = (int)(LayerMasks.Parts | LayerMasks.Scenery | LayerMasks.Unknown19 | LayerMasks.Wheels); // Why 19?
+        const int collisionLayerMask = (int)(LayerMasks.Parts | LayerMasks.Scenery | LayerMasks.EVA | LayerMasks.Wheels);

         void OnEnable()
         {
             startTime = Time.time;
-            velocity = initialV;
-            velocity += transform.rotation *
-                        (new Vector3(Random.Range(-.1f, .1f), Random.Range(-.1f, .1f),  Random.Range(-.1f, .1f)) + configV);
-            angularVelocity =
-                new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f),
-                    Random.Range(-10f, 10f)) * 10;
+
+            var variation = Random.insideUnitSphere;
+            velocity = initialV + transform.rotation * (new Vector3(configVar.x * variation.x, configVar.y * variation.y, configVar.z * variation.z) + configV);
+            angularVelocity = 100f * Random.insideUnitSphere;

             atmDensity =
                 (float)
diff --git a/BDArmory/Weapons/ModuleWeapon.cs b/BDArmory/Weapons/ModuleWeapon.cs
index dd37b5fb0..c568f31be 100644
--- a/BDArmory/Weapons/ModuleWeapon.cs
+++ b/BDArmory/Weapons/ModuleWeapon.cs
@@ -3326,6 +3326,11 @@ void WeaponFX()
                         ShellCasing shellComponent = ejectedShell.GetComponent<ShellCasing>();
                         shellComponent.initialV = part.rb.velocity;
                         shellComponent.configV = shellEjectVelocity;
+                        shellComponent.configVar = new Vector3(
+                            0.1f + 0.1f * Mathf.Abs(shellEjectVelocity.x),
+                            0.1f + 0.1f * Mathf.Abs(shellEjectVelocity.y),
+                            0.1f + 0.1f * Mathf.Abs(shellEjectVelocity.z)
+                        );
                         ejectedShell.SetActive(true);
                     }
                 }
Spartwo commented 3 months ago

Dropped in a float value that controls shell deviation

https://discord.com/channels/720416076571082863/720423078533791854/1256416138238230619