Browse Source

- fixed scale bug (balloons were displayed twice two small)

- added spring rendering code
master
Youen Toupin 9 years ago
parent
commit
67bdfa615c
  1. 44
      Plugin/ModuleAerostat.cs

44
Plugin/ModuleAerostat.cs

@ -84,11 +84,17 @@ namespace Aerostats
[KSPField(isPersistant = false, guiActive = false)]
public float BalloonDragCoeff = 0.3f;
/// <summary>
/// Length of spring that can not be extended (no force applied under this length)
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
public float SpringRestLength = 10.0f;
/// <summary>
/// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter
/// </summary>
[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<LineRenderer>();
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);
}
}
}
}

Loading…
Cancel
Save