Skip to content

Commit

Permalink
LightShifter.ApplyPhysics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigma88 committed Mar 16, 2020
1 parent a8cfd2c commit 8e48214
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 62 deletions.
16 changes: 4 additions & 12 deletions src/Kopernicus/Components/KopernicusSolarPanels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public virtual void FixedUpdate()
if (SP != null)
{
KopernicusStar star = KopernicusStar.CelestialBodies[SP.trackingBody];
PhysicsGlobals.SolarLuminosityAtHome = star.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = star.shifter.solarInsolation;
KopernicusStar.CalculatePhysics();
star.shifter.ApplyPhysics();

vessel.solarFlux = star.CalculateFluxAt(vessel);
}
Expand Down Expand Up @@ -131,9 +129,7 @@ public override void FixedUpdate()
else
{
// Use this star
PhysicsGlobals.SolarLuminosityAtHome = star.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = star.shifter.solarInsolation;
KopernicusStar.CalculatePhysics();
star.shifter.ApplyPhysics();
vessel.solarFlux = star.CalculateFluxAt(vessel);

// Change the tracking body
Expand Down Expand Up @@ -169,9 +165,7 @@ public override void FixedUpdate()
SP.GetTrackingBodyTransforms();

// Restore the starting star
PhysicsGlobals.SolarLuminosityAtHome = trackingStar.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = trackingStar.shifter.solarInsolation;
KopernicusStar.CalculatePhysics();
trackingStar.shifter.ApplyPhysics();

totalFlux += trackingStar.CalculateFluxAt(vessel);

Expand All @@ -195,9 +189,7 @@ public override void FixedUpdate()
}

// Restore The Current Star
PhysicsGlobals.SolarLuminosityAtHome = KopernicusStar.Current.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = KopernicusStar.Current.shifter.solarInsolation;
KopernicusStar.CalculatePhysics();
KopernicusStar.Current.shifter.ApplyPhysics();
}
}

Expand Down
51 changes: 2 additions & 49 deletions src/Kopernicus/Components/KopernicusStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,49 +89,6 @@ public class KopernicusStar : Sun
/// </summary>
public KopernicusSunFlare lensFlare;

/// <summary>
/// Fixes the Calculation for Luminosity
/// </summary>
internal static void CalculatePhysics()
{
if (!FlightGlobals.ready)
{
return;
}

if (SolarIntensityAtHomeMultiplier == 0)
{
CelestialBody homeBody = FlightGlobals.GetHomeBody();
if (homeBody == null)
{
return;
}

while (Stars.All(s => s.sun != homeBody.referenceBody) && homeBody.referenceBody != null)
{
homeBody = homeBody.referenceBody;
}

SolarIntensityAtHomeMultiplier = Math.Pow(homeBody.orbit.semiMajorAxis, 2) * 4 * 3.14159265358979;
}

CalculatePhysics(SolarIntensityAtHomeMultiplier * PhysicsGlobals.SolarLuminosityAtHome);
}

/// <summary>
/// Applies The Calculation for Luminosity
/// </summary>
internal static void CalculatePhysics(Double solarLuminosity)
{
if (!FlightGlobals.ready)
{
return;
}

FieldInfo SolarLuminosity = typeof(PhysicsGlobals).GetField("solarLuminosity", BindingFlags.Instance | BindingFlags.NonPublic);
SolarLuminosity.SetValue(PhysicsGlobals.Instance, solarLuminosity);
}

/// <summary>
/// Returns the star the given body orbits
/// </summary>
Expand Down Expand Up @@ -373,18 +330,14 @@ public static void SunBodyFlux(ModularFlightIntegrator MFI)
}

// Set Physics
PhysicsGlobals.SolarLuminosityAtHome = star.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = star.shifter.solarInsolation;
CalculatePhysics();
star.shifter.ApplyPhysics();

// Calculate Flux
solarFlux += star.CalculateFluxAt(MFI.Vessel);
}

// Set Physics to the Current Star
PhysicsGlobals.SolarLuminosityAtHome = KopernicusStar.Current.shifter.solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = KopernicusStar.Current.shifter.solarInsolation;
CalculatePhysics();
KopernicusStar.Current.shifter.ApplyPhysics();

// Calculate Flux
solarFlux += Current.CalculateFluxAt(MFI.Vessel);
Expand Down
37 changes: 36 additions & 1 deletion src/Kopernicus/Components/LightShifter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

using System;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace Kopernicus.Components
Expand Down Expand Up @@ -115,5 +117,38 @@ public void ApplyAmbient()
ambientLight.vacuumAmbientColor = ambientLightColor;
}
}

public static Double SolarIntensityAtHomeMultiplier = 0;

public void ApplyPhysics()
{
if (!FlightGlobals.ready)
{
return;
}

if (SolarIntensityAtHomeMultiplier == 0)
{
CelestialBody homeBody = FlightGlobals.GetHomeBody();

if (homeBody == null)
{
return;
}

while (KopernicusStar.Stars.All(s => s.sun != homeBody.referenceBody) && homeBody.referenceBody != null)
{
homeBody = homeBody.referenceBody;
}

SolarIntensityAtHomeMultiplier = Math.Pow(homeBody.orbit.semiMajorAxis, 2) * 4 * 3.14159265358979;
}

PhysicsGlobals.SolarLuminosityAtHome = solarLuminosity;
PhysicsGlobals.SolarInsolationAtHome = solarInsolation;

FieldInfo SolarLuminosity = typeof(PhysicsGlobals).GetField("solarLuminosity", BindingFlags.Instance | BindingFlags.NonPublic);
SolarLuminosity.SetValue(PhysicsGlobals.Instance, SolarIntensityAtHomeMultiplier * PhysicsGlobals.SolarLuminosityAtHome);
}
}
}
}

0 comments on commit 8e48214

Please sign in to comment.