From 67bdfa615ca405cd0523476da37aecdea668ef37 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Sun, 31 May 2015 23:39:18 +0200 Subject: [PATCH] - fixed scale bug (balloons were displayed twice two small) - added spring rendering code --- Plugin/ModuleAerostat.cs | 44 +++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/Plugin/ModuleAerostat.cs b/Plugin/ModuleAerostat.cs index 49337a0..302f983 100644 --- a/Plugin/ModuleAerostat.cs +++ b/Plugin/ModuleAerostat.cs @@ -84,11 +84,17 @@ namespace Aerostats [KSPField(isPersistant = false, guiActive = false)] public float BalloonDragCoeff = 0.3f; + /// + /// Length of spring that can not be extended (no force applied under this length) + /// + [KSPField(isPersistant = false, guiActive = false)] + public float SpringRestLength = 10.0f; + /// /// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter /// [KSPField(isPersistant = false, guiActive = false)] - public float SpringHardness = 20000.0f; + public float SpringHardness = 5000.0f; private bool Staged; @@ -104,6 +110,7 @@ namespace Aerostats private float Inflation = 0; private GameObject Balloon; + private LineRenderer Spring; private void PlayAnimation(string animationName, float animationSpeed) { @@ -157,6 +164,23 @@ namespace Aerostats LiftingGasQuantity = Math.Min(MinimumFillQuantity, RemainingCompressedGas); RemainingCompressedGas -= LiftingGasQuantity; + + Spring = Balloon.AddComponent(); + Spring.useWorldSpace = true; + Spring.material = new Material(Shader.Find("VertexLit")); + Spring.SetColors(Color.black, Color.black); + Spring.SetWidth(0.1f, 0.1f); + Spring.SetVertexCount(2); + + part.OnJustAboutToBeDestroyed += OnPartDestroyed; + } + + private void OnPartDestroyed() + { + Destroy(Balloon); // another option could be to let it float freely, but in this case the buoyancy code should be implemented in a separate MonoBehavior + Balloon = null; + Spring = null; + part.OnJustAboutToBeDestroyed -= OnPartDestroyed; } private void FixedUpdate() @@ -226,7 +250,7 @@ namespace Aerostats // V = 4/3*pi*r^3 float radius = Mathf.Pow(currentGasVolume * 0.75f / Mathf.PI, 0.333f); - float scale = radius + 0.1f; + float scale = radius * 2.0f + 0.1f; Balloon.transform.localScale = new Vector3(scale,scale,scale); Balloon.rigidbody.mass = (BalloonEmptyMass + balloonGasMass) * 0.001f; @@ -241,28 +265,24 @@ namespace Aerostats Balloon.rigidbody.AddForce(-airVelocity.normalized * dragForce / 1000.0f, ForceMode.Force); // spring between balloon and base - float restLength = 2.0f + radius; + float restLength = SpringRestLength + radius; var springVec = Balloon.rigidbody.position - part.rigidbody.position; float springLength = springVec.magnitude; float springForceMag = 0; + var balloonAttachPoint = Balloon.rigidbody.position - Balloon.transform.up.normalized * radius; if(springLength > restLength) { float tensingLength = springLength - restLength; springForceMag = tensingLength * SpringHardness; var springForce = springVec * (springForceMag / springLength * 0.001f); part.rigidbody.AddForce(springForce, ForceMode.Force); - Balloon.rigidbody.AddForceAtPosition(-springForce, Balloon.rigidbody.position - Balloon.transform.up * radius, ForceMode.Force); + Balloon.rigidbody.AddForceAtPosition(-springForce, balloonAttachPoint, ForceMode.Force); } Util.PostSingleScreenMessage("spring force", "Spring force = " + springForceMag + "N"); - } - } - } - public void Update() - { - if (Balloon != null) - { - Gizmos.DrawLine(part.transform.position, Balloon.transform.position - Balloon.transform.up * Balloon.transform.localScale.x); + Spring.SetPosition(0, part.transform.position); + Spring.SetPosition(1, balloonAttachPoint); + } } } }