@ -84,11 +84,17 @@ namespace Aerostats
[KSPField(isPersistant = false, guiActive = false)]
[KSPField(isPersistant = false, guiActive = false)]
public float BalloonDragCoeff = 0.3f ;
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 = 1 0.0f ;
/// <summary>
/// <summary>
/// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter
/// Force applied by the spring between the balloon and the vessel, proportional to spring extension, in newton per meter
/// </summary>
/// </summary>
[KSPField(isPersistant = false, guiActive = false)]
[KSPField(isPersistant = false, guiActive = false)]
public float SpringHardness = 2 0 0 0 0.0f ;
public float SpringHardness = 5 0 0 0.0f ;
private bool Staged ;
private bool Staged ;
@ -104,6 +110,7 @@ namespace Aerostats
private float Inflation = 0 ;
private float Inflation = 0 ;
private GameObject Balloon ;
private GameObject Balloon ;
private LineRenderer Spring ;
private void PlayAnimation ( string animationName , float animationSpeed )
private void PlayAnimation ( string animationName , float animationSpeed )
{
{
@ -157,6 +164,23 @@ namespace Aerostats
LiftingGasQuantity = Math . Min ( MinimumFillQuantity , RemainingCompressedGas ) ;
LiftingGasQuantity = Math . Min ( MinimumFillQuantity , RemainingCompressedGas ) ;
RemainingCompressedGas - = LiftingGasQuantity ;
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 ( )
private void FixedUpdate ( )
@ -226,7 +250,7 @@ namespace Aerostats
// V = 4/3*pi*r^3
// V = 4/3*pi*r^3
float radius = Mathf . Pow ( currentGasVolume * 0.75f / Mathf . PI , 0.333f ) ;
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 . transform . localScale = new Vector3 ( scale , scale , scale ) ;
Balloon . rigidbody . mass = ( BalloonEmptyMass + balloonGasMass ) * 0.001f ;
Balloon . rigidbody . mass = ( BalloonEmptyMass + balloonGasMass ) * 0.001f ;
@ -241,28 +265,24 @@ namespace Aerostats
Balloon . rigidbody . AddForce ( - airVelocity . normalized * dragForce / 1 0 0 0.0f , ForceMode . Force ) ;
Balloon . rigidbody . AddForce ( - airVelocity . normalized * dragForce / 1 0 0 0.0f , ForceMode . Force ) ;
// spring between balloon and base
// spring between balloon and base
float restLength = 2.0f + radius ;
float restLength = SpringRestLength + radius ;
var springVec = Balloon . rigidbody . position - part . rigidbody . position ;
var springVec = Balloon . rigidbody . position - part . rigidbody . position ;
float springLength = springVec . magnitude ;
float springLength = springVec . magnitude ;
float springForceMag = 0 ;
float springForceMag = 0 ;
var balloonAttachPoint = Balloon . rigidbody . position - Balloon . transform . up . normalized * radius ;
if ( springLength > restLength )
if ( springLength > restLength )
{
{
float tensingLength = springLength - restLength ;
float tensingLength = springLength - restLength ;
springForceMag = tensingLength * SpringHardness ;
springForceMag = tensingLength * SpringHardness ;
var springForce = springVec * ( springForceMag / springLength * 0.001f ) ;
var springForce = springVec * ( springForceMag / springLength * 0.001f ) ;
part . rigidbody . AddForce ( springForce , ForceMode . Force ) ;
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" ) ;
Util . PostSingleScreenMessage ( "spring force" , "Spring force = " + springForceMag + "N" ) ;
}
}
}
public void Update ( )
Spring . SetPosition ( 0 , part . transform . position ) ;
{
Spring . SetPosition ( 1 , balloonAttachPoint ) ;
if ( Balloon ! = null )
}
{
Gizmos . DrawLine ( part . transform . position , Balloon . transform . position - Balloon . transform . up * Balloon . transform . localScale . x ) ;
}
}
}
}
}
}