@ -53,17 +53,28 @@ namespace Aerostats
// replace mesh and collider by a sphere (hack until proper assets are created)
// replace mesh and collider by a sphere (hack until proper assets are created)
var sphere = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
var sphere = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
gameObject . GetComponentInChildren < MeshFilter > ( ) . mesh = sphere . GetComponent < MeshFilter > ( ) . mesh ;
var meshFilter = gameObject . GetComponentInChildren < MeshFilter > ( ) ;
Destroy ( gameObject . collider ) ;
meshFilter . mesh = sphere . GetComponent < MeshFilter > ( ) . mesh ;
var collider = gameObject . AddComponent < SphereCollider > ( ) ;
var t = meshFilter . transform ;
collider . radius = 0.67f ; // don't ask me, I don't know why
while ( t ! = transform )
{
t . localPosition = new Vector3 ( 0 , 0 , 0 ) ;
t . localRotation = Quaternion . identity ;
t . localScale = new Vector3 ( 1 , 1 , 1 ) ;
t = t . parent ;
}
var colliderObject = part . collider . gameObject ;
Destroy ( colliderObject . collider ) ;
var collider = colliderObject . AddComponent < SphereCollider > ( ) ;
collider . radius = 0.5f ;
Destroy ( sphere ) ;
Destroy ( sphere ) ;
gameObject . AddComponent < Rigidbody > ( ) ;
gameObject . AddComponent < Rigidbody > ( ) ;
rigidbody . mass = EmptyMass * 0.001f ;
rigidbody . mass = EmptyMass * 0.001f ;
rigidbody . velocity = initialVelocity ; // start with the same velocity or everything explodes when deploying from a moving vessel
part . mass = rigidbody . mass ;
rigidbody . angularDrag = 1 0.0f ;
rigidbody . velocity = initialVelocity ;
rigidbody . position = transform . position ;
rigidbody . position = transform . position ;
part . rb = rigidbody ;
InjectGas ( initialGasQuantity ) ;
InjectGas ( initialGasQuantity ) ;
@ -108,7 +119,7 @@ namespace Aerostats
CelestialBody body = vessel . mainBody ;
CelestialBody body = vessel . mainBody ;
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float balloonInternalTemperature = externalTemperature ;
float balloonInternalTemperature = Mathf . Max ( externalTemperature , 2 0.0f ) ;
float externalPressure = Math . Max ( ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f , 0.00001f ) ;
float externalPressure = Math . Max ( ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f , 0.00001f ) ;
float currentGasMoles = GasUtilities . StpVolumeToMoles * LiftingGasQuantity ;
float currentGasMoles = GasUtilities . StpVolumeToMoles * LiftingGasQuantity ;
@ -124,9 +135,15 @@ namespace Aerostats
// V = 4/3*pi*r^3
// V = 4/3*pi*r^3
Radius = Mathf . Pow ( CurrentVolume * 0.75f / Mathf . PI , 0.333f ) ;
Radius = Mathf . Pow ( CurrentVolume * 0.75f / Mathf . PI , 0.333f ) ;
float scale = Radius + 0.1f ;
float scale = Mathf . Max ( Radius * 2.0f , 0.1f ) ;
if ( AttachedCase ! = null )
{
float dist = Vector3 . Distance ( AttachedCase . transform . position , transform . position ) ;
scale = Mathf . Min ( Mathf . Max ( dist , transform . localScale . x * 0.5f ) - 0.005f , scale * 0.5f ) * 2.0f ; // to prevent physics explosion due to interpenetration of objects, don't scale the balloon more than the space available between the case and the balloon (this happens when expanding fast after deploy in vaccum for example)
}
transform . localScale = new Vector3 ( scale , scale , scale ) ;
transform . localScale = new Vector3 ( scale , scale , scale ) ;
rigidbody . mass = ( EmptyMass + balloonGasMass ) * 0.001f ;
rigidbody . mass = ( EmptyMass + balloonGasMass ) * 0.001f ;
part . mass = rigidbody . mass ;
return LiftingGasQuantity ;
return LiftingGasQuantity ;
}
}
@ -140,7 +157,7 @@ namespace Aerostats
CelestialBody body = vessel . mainBody ;
CelestialBody body = vessel . mainBody ;
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float balloonInternalTemperature = externalTemperature ;
float balloonInternalTemperature = Mathf . Max ( externalTemperature , 2 0.0f ) ;
float externalPressure = Math . Max ( ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f , 0.00001f ) ;
float externalPressure = Math . Max ( ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f , 0.00001f ) ;
Util . PostDebugSingleScreenMessage ( "external atmo" , "Temperature = " + externalTemperature + "K, Pressure=" + externalPressure + "Pa" ) ;
Util . PostDebugSingleScreenMessage ( "external atmo" , "Temperature = " + externalTemperature + "K, Pressure=" + externalPressure + "Pa" ) ;
@ -158,23 +175,28 @@ namespace Aerostats
InjectGas ( 0 ) ; // update security valve venting
InjectGas ( 0 ) ; // update security valve venting
var gravityAccel = FlightGlobals . getGeeForceAtPosition ( worldPos ) ;
var gravityAccel = FlightGlobals . getGeeForceAtPosition ( rigidbody . position ) ;
//Util.PostSingleScreenMessage("gravity", "Gravity accel = (" + gravityAccel.x + ", " + gravityAccel.y + ", " + gravityAccel.z + ")");
//Util.PostSingleScreenMessage("gravity", "Gravity accel = (" + gravityAccel.x + ", " + gravityAccel.y + ", " + gravityAccel.z + ")");
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float externalTemperature = ( float ) FlightGlobals . getExternalTemperature ( worldPos , body ) ;
float balloonInternalTemperature = externalTemperature ;
float externalPressure = ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f ;
float externalPressure = Math . Max ( ( float ) FlightGlobals . getStaticPressure ( worldPos , body ) * 1 0 0 0.0f , 0.00001f ) ;
float airDensity = ( float ) FlightGlobals . getAtmDensity ( externalPressure / 1 0 0 0.0f , externalTemperature , body ) ;
float airDensity = ( float ) FlightGlobals . getAtmDensity ( externalPressure / 1 0 0 0.0f , externalTemperature , body ) ;
rigidbody . AddForce ( - gravityAccel * Lift / 1 0 0 0.0f , ForceMode . Forc e) ;
rigidbody . angularDrag = Mathf . Max ( 0.5f , 1 0.0f * externalPressure / GasUtilities . StandardPressur e) ;
// balloon drag
if ( externalPressure > 0.00001f )
var airVelocity = rigidbody . velocity + Krakensbane . GetFrameVelocity ( ) /*- vessel.mainBody.getRFrmVel(vessel.GetWorldPos3D())*/ ;
{
float sqVel = ( float ) airVelocity . magnitude ;
// lift
sqVel * = sqVel ;
rigidbody . AddForce ( - gravityAccel * Lift / 1 0 0 0.0f , ForceMode . Force ) ;
float dragForce = 0.5f * airDensity * sqVel * DragCoeff * ( Mathf . PI * Radius * Radius ) ;
Util . PostDebugSingleScreenMessage ( "balloon drag" , "Drag = " + dragForce + "N" ) ;
// balloon drag
rigidbody . AddForce ( - airVelocity . normalized * dragForce / 1 0 0 0.0f , ForceMode . Force ) ;
var airVelocity = rigidbody . velocity + Krakensbane . GetFrameVelocity ( ) /*- vessel.mainBody.getRFrmVel(vessel.GetWorldPos3D())*/ ;
float sqVel = ( float ) airVelocity . magnitude ;
sqVel * = sqVel ;
float dragForce = 0.5f * airDensity * sqVel * DragCoeff * ( Mathf . PI * Radius * Radius ) ;
Util . PostDebugSingleScreenMessage ( "balloon drag" , "Drag = " + dragForce + "N" ) ;
rigidbody . AddForce ( - airVelocity . normalized * dragForce / 1 0 0 0.0f , ForceMode . Force ) ;
}
}
}
}
}
}
}