Skip to content

Commit

Permalink
Tilted belts (#423)
Browse files Browse the repository at this point in the history
* so, this is how to tilt the belts. now, find out where to get the correct values...

* "there is no tilt"

* wobble.

* WIP

* fixed the magnetopause

* interesting magnetic poles

* tilted belts.

* this should work (tm).

* changelog
  • Loading branch information
SirMortimer committed Jun 9, 2019
1 parent 3738066 commit 609d627
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v3.0.3 for all versions of KSP from 1.4.0 to 1.7.x

- 2019-06-09

### Changes since the last release

* Support tilted magnetic fields and radiation belts (Sir Mortimer)

## v3.0.2 for all versions of KSP from 1.4.0 to 1.7.x

- 2019-06-08
Expand Down
21 changes: 19 additions & 2 deletions GameData/KerbalismConfig/System/Radiation.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ RadiationModel

has_inner = true
inner_dist = 1.25
inner_radius = 0.05
inner_radius = 0.15
inner_compression = 1.15
inner_extension = 1.0
inner_deform = 0.05
Expand Down Expand Up @@ -160,6 +160,8 @@ RadiationBody
radiation_model = metallic
radiation_inner = 1.0
radiation_pause = -0.007
geomagnetic_pole_lat = 96
geomagnetic_pole_lon = 0
}

RadiationBody
Expand All @@ -176,6 +178,8 @@ RadiationBody
radiation_inner = 10.0 // rad/h inside the inner belt
radiation_outer = 2.0 // rad/h inside the outer belt
radiation_pause = -0.010 // rad/h inside the magnetopause
geomagnetic_pole_lat = 80.37 // lat of the geomagnetic north pole
geomagnetic_pole_lon = -72.62 // lon of the geomagnetic north pole
}

RadiationBody
Expand All @@ -199,6 +203,8 @@ RadiationBody
radiation_inner = 200.0
radiation_outer = 5.0
radiation_pause = -0.010
geomagnetic_pole_lat = 88
geomagnetic_pole_lon = 0
}

RadiationBody
Expand All @@ -208,6 +214,17 @@ RadiationBody
radiation_pause = 0.041 // surface radiation
}

RadiationBody
{
name = Bop
radiation_model = metallic
radiation_inner = 1.0
radiation_pause = -0.007
geomagnetic_pole_lat = 45
geomagnetic_pole_lon = 0
}




// ============================================================================
Expand Down Expand Up @@ -242,4 +259,4 @@ RadiationBody
ec_rate = 2.5
active = e10
}
}
}
99 changes: 79 additions & 20 deletions src/Kerbalism/Radiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public RadiationBody(CelestialBody body)
this.model = RadiationModel.none;
this.body = body;
this.reference = 0;
this.geomagnetic_pole = new Vector3(0.0f, 1.0f, 0.0f);
}

// ctor: deserialize
Expand All @@ -174,21 +175,33 @@ public RadiationBody(ConfigNode node, Dictionary<string, RadiationModel> models,
radiation_inner = Lib.ConfigValue(node, "radiation_inner", 0.0) / 3600.0;
radiation_outer = Lib.ConfigValue(node, "radiation_outer", 0.0) / 3600.0;
radiation_pause = Lib.ConfigValue(node, "radiation_pause", 0.0) / 3600.0;
geomagnetic_pole_lat = Lib.ConfigValue(node, "geomagnetic_pole_lat", 90.0f);
geomagnetic_pole_lon = Lib.ConfigValue(node, "geomagnetic_pole_lon", 0.0f);
reference = Lib.ConfigValue(node, "reference", 0);

// get the radiation environment
if (!models.TryGetValue(Lib.ConfigValue(node, "radiation_model", ""), out model)) model = RadiationModel.none;

// get the body
this.body = body;
}

float lat = (float)(geomagnetic_pole_lat * Math.PI / 180.0);
float lon = (float)(geomagnetic_pole_lon * Math.PI / 180.0);

float x = Mathf.Cos(lat) * Mathf.Cos(lon);
float y = Mathf.Sin(lat);
float z = Mathf.Cos(lat) * Mathf.Sin(lon);
geomagnetic_pole = new Vector3(x, y, z);
}

public string name; // name of the body
public double radiation_inner; // rad/s inside inner belt
public double radiation_outer; // rad/s inside outer belt
public double radiation_pause; // rad/s inside magnetopause
public int reference; // index of the body that determine x-axis of the gsm-space
public float geomagnetic_pole_lat = 90.0f;
public float geomagnetic_pole_lon = 0.0f;
public Vector3 geomagnetic_pole;

// shortcut to the radiation environment
public RadiationModel model;
Expand Down Expand Up @@ -340,17 +353,59 @@ public static void Preprocess()
// - the space is then orthonormalized
// - if the reference body is the same as the body,
// the galactic rotation vector is used as x-axis instead
public static Space Gsm_space(CelestialBody body, CelestialBody reference)
public static Space Gsm_space(RadiationBody rb, bool tilted)
{
CelestialBody body = rb.body;
CelestialBody reference = FlightGlobals.Bodies[rb.reference];

Space gsm;
gsm.origin = ScaledSpace.LocalToScaledSpace(body.position);
gsm.scale = ScaledSpace.InverseScaleFactor * (float)body.Radius;
if (body != reference)
{
gsm.x_axis = ((Vector3)ScaledSpace.LocalToScaledSpace(reference.position) - gsm.origin).normalized;
gsm.y_axis = (Vector3)body.RotationAxis; //< initial guess
gsm.z_axis = Vector3.Cross(gsm.x_axis, gsm.y_axis).normalized;
gsm.y_axis = Vector3.Cross(gsm.z_axis, gsm.x_axis).normalized; //< orthonormalize
if(!tilted)
{
gsm.y_axis = body.RotationAxis; //< initial guess
gsm.z_axis = Vector3.Cross(gsm.x_axis, gsm.y_axis).normalized;
gsm.y_axis = Vector3.Cross(gsm.z_axis, gsm.x_axis).normalized; //< orthonormalize
}
else
{
/* "Do not try and tilt the planet, that's impossible.
* Instead, only try to realize the truth...there is no tilt.
* Then you'll see that it is not the planet that tilts, it is
* the rest of the universe."
*
* - The Matrix
*
*
* the orbits are inclined (with respect to the equator of the
* Earth), but all axes are parallel. and aligned with the unity
* world z axis. or is it y? whatever, KSP uses two conventions
* in different places.
* if you use Principia, the current main body (or if there is
* none, e.g. in the space centre or tracking station, the home
* body) is not tilted (its axis is the unity vertical.
* you can fetch the full orientation (tilt and rotation) of any
* body (including the current main body) in the current unity
* frame (which changes of course, because sometimes KSP uses a
* rotating frame, and because Principia tilts the universe
* differently if the current main body changes) as the
* orientation of the scaled space body
*
* body.scaledBody.transform.rotation or something along those lines
*
* - egg
*/

Vector3 pole = rb.geomagnetic_pole;
Quaternion rotation = body.scaledBody.transform.rotation;
gsm.y_axis = (rotation * pole).normalized;

gsm.z_axis = Vector3.Cross(gsm.x_axis, gsm.y_axis).normalized;
gsm.x_axis = Vector3.Cross(gsm.y_axis, gsm.z_axis).normalized; //< orthonormalize
}
}
else
{
Expand Down Expand Up @@ -465,28 +520,27 @@ public static void Render()

// generate radii-normalized GMS space
RadiationBody rb = Info(body);
Space gsm = Gsm_space(rb.body, FlightGlobals.Bodies[rb.reference]);
Space gsm_tilted = Gsm_space(rb, true);

// [debug] show axis
//LineRenderer.commit(gsm.origin, gsm.origin + gsm.x_axis * gsm.scale * 5.0f, Color.red);
//LineRenderer.commit(gsm.origin, gsm.origin + gsm.y_axis * gsm.scale * 5.0f, Color.green);
//LineRenderer.commit(gsm.origin, gsm.origin + gsm.z_axis * gsm.scale * 5.0f, Color.blue);

// get magnetic field data
RadiationModel mf = Info(body).model;
//LineRenderer.Commit(gsm_tilted.origin, gsm_tilted.origin + gsm_tilted.x_axis * gsm_tilted.scale * 5.0f, Color.red);
//LineRenderer.Commit(gsm_tilted.origin, gsm_tilted.origin + gsm_tilted.y_axis * gsm_tilted.scale * 5.0f, Color.green);
//LineRenderer.Commit(gsm_tilted.origin, gsm_tilted.origin + gsm_tilted.z_axis * gsm_tilted.scale * 5.0f, Color.blue);

// enable material
mat.SetPass(0);

// get magnetic field data
RadiationModel mf = rb.model;

// render active body fields
Matrix4x4 m = gsm.Look_at();
if (show_inner && mf.has_inner) mf.inner_pmesh.Render(m);
if (show_outer && mf.has_outer) mf.outer_pmesh.Render(m);
if (show_pause && mf.has_pause) mf.pause_pmesh.Render(m);
Matrix4x4 m_tilted = gsm_tilted.Look_at();
if (show_inner && mf.has_inner) mf.inner_pmesh.Render(m_tilted);
if (show_outer && mf.has_outer) mf.outer_pmesh.Render(m_tilted);
if (show_pause && mf.has_pause) mf.pause_pmesh.Render(Gsm_space(rb, false).Look_at());
}
}


public static RadiationBody Info(CelestialBody body)
{
RadiationBody rb;
Expand Down Expand Up @@ -526,9 +580,9 @@ public static RadiationBody Info(CelestialBody body)
if (mf.Has_field())
{
// generate radii-normalized GSM space
gsm = Gsm_space(rb.body, FlightGlobals.Bodies[rb.reference]);
gsm = Gsm_space(rb, true);

// move the poing in GSM space
// move the point in GSM space
p = gsm.Transform_in(position);

// accumulate radiation and determine pause/belt flags
Expand All @@ -546,6 +600,9 @@ public static RadiationBody Info(CelestialBody body)
}
if (mf.has_pause)
{
gsm = Gsm_space(rb, false);
p = gsm.Transform_in(position);

D = mf.Pause_func(p);
radiation += Lib.Clamp(D / -0.1332f, 0.0f, 1.0f) * rb.radiation_pause;
magnetosphere |= D < 0.0f && rb.body.flightGlobalsIndex != 0; //< ignore heliopause
Expand Down Expand Up @@ -602,7 +659,7 @@ public static double ComputeSurface(CelestialBody b, double gamma_transparency)
if (mf.Has_field())
{
// generate radii-normalized GSM space
gsm = Gsm_space(rb.body, FlightGlobals.Bodies[rb.reference]);
gsm = Gsm_space(rb, true);

// move the poing in GSM space
p = gsm.Transform_in(position);
Expand All @@ -620,6 +677,8 @@ public static double ComputeSurface(CelestialBody b, double gamma_transparency)
}
if (mf.has_pause)
{
gsm = Gsm_space(rb, false);
p = gsm.Transform_in(position);
D = mf.Pause_func(p);
radiation += Lib.Clamp(D / -0.1332f, 0.0f, 1.0f) * rb.radiation_pause;
}
Expand Down

0 comments on commit 609d627

Please sign in to comment.