Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
ktyl | 5ad70b3f9c | |
ktyl | bed71f2c4c | |
Cat Flynn | bc28d4d172 | |
ktyl | f28670c083 | |
ktyl | ce2a05cdf9 | |
Cat Flynn | 0c396e2c3f | |
Cat Flynn | 8ccd003b00 |
|
@ -2,4 +2,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>net472</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="vim.math3d" Version="1.6.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,17 +1,18 @@
|
|||
[gd_scene load_steps=4 format=2]
|
||||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://scenes/Train.tscn" type="PackedScene" id=1]
|
||||
[ext_resource path="res://scenes/Railway.tscn" type="PackedScene" id=2]
|
||||
[ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3]
|
||||
[ext_resource path="res://scenes/orbits/OrbitSystem.tscn" type="PackedScene" id=4]
|
||||
|
||||
[node name="Main" type="Node2D"]
|
||||
|
||||
[node name="Train" parent="." instance=ExtResource( 1 )]
|
||||
|
||||
[node name="DirectionalLight" type="DirectionalLight" parent="."]
|
||||
transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 )
|
||||
|
||||
[node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
|
||||
|
||||
[node name="Railway" parent="." instance=ExtResource( 2 )]
|
||||
script = null
|
||||
[node name="Orbit System" parent="." instance=ExtResource( 4 )]
|
||||
transform = Transform( 0.856869, 0.3292, -0.396741, 0.0949996, 0.655565, 0.749139, 0.506706, -0.679604, 0.53046, -0.599122, 0, 0 )
|
||||
SemiMajorAxis = 6.166
|
||||
Eccentricity = 0.239
|
||||
Period = 1.0
|
||||
_speed = 0.877
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/orbits/OrbitSystem.cs" type="Script" id=1]
|
||||
[ext_resource path="res://scenes/orbits/Planet.tscn" type="PackedScene" id=2]
|
||||
|
||||
[node name="Orbit System" type="Spatial"]
|
||||
script = ExtResource( 1 )
|
||||
SemiMajorAxis = 6.881
|
||||
Eccentricity = 0.399
|
||||
Period = 10.0
|
||||
_a = NodePath("Planet A")
|
||||
_b = NodePath("Planet B")
|
||||
_barycenter = NodePath("Barycenter")
|
||||
|
||||
[node name="Barycenter" type="Spatial" parent="."]
|
||||
|
||||
[node name="Planet A" parent="." instance=ExtResource( 2 )]
|
||||
transform = Transform( 0.999977, 0.00603502, 0.00319089, -0.00604282, 0.999979, 0.0024369, -0.00317609, -0.00245615, 0.999992, 0, 0, 0 )
|
||||
|
||||
[node name="Planet B" parent="." instance=ExtResource( 2 )]
|
||||
transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, -8.44634, 0, -3.53339 )
|
||||
Mass = 0.125
|
|
@ -0,0 +1,11 @@
|
|||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/orbits/Planet.cs" type="Script" id=1]
|
||||
|
||||
[node name="Planet" type="Spatial"]
|
||||
script = ExtResource( 1 )
|
||||
Mass = 1.0
|
||||
|
||||
[node name="CSGSphere" type="CSGSphere" parent="."]
|
||||
radial_segments = 20
|
||||
rings = 20
|
|
@ -5,9 +5,10 @@ public class OrbitCamera : Spatial
|
|||
{
|
||||
[Export]
|
||||
private float _lookSensitivity = 200f;
|
||||
|
||||
[Export]
|
||||
private float _zoomSensitivity = 10f;
|
||||
[Export]
|
||||
private Vector2 _fovRange = new Vector2(10, 60);
|
||||
|
||||
[Export]
|
||||
private NodePath _cameraPath;
|
||||
|
@ -65,9 +66,18 @@ public class OrbitCamera : Spatial
|
|||
}
|
||||
}
|
||||
|
||||
private void Zoom(float amount)
|
||||
private void Zoom(int dir)
|
||||
{
|
||||
Camera.Fov += amount * _zoomSensitivity / Camera.Fov;
|
||||
if (dir != 1 && dir != -1)
|
||||
throw new ArgumentException();
|
||||
|
||||
var fov = Camera.Fov;
|
||||
var zoom = _zoomSensitivity / Camera.Fov;
|
||||
|
||||
fov += (float)dir * zoom;
|
||||
fov = Mathf.Clamp(fov, _fovRange.x, _fovRange.y);
|
||||
|
||||
Camera.Fov = fov;
|
||||
}
|
||||
|
||||
private void HandleMouseMovement(InputEventMouseMotion mouseMotion)
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
using Vim.Math3d;
|
||||
|
||||
public interface ILocation
|
||||
{
|
||||
DVector3 Position { get; set; }
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public interface IMassive
|
||||
{
|
||||
float Mass { get; }
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
using Vim.Math3d;
|
||||
|
||||
public interface IOrbit
|
||||
{
|
||||
DVector3 GetPosition(double t);
|
||||
IEllipse Ellipse { get; set; }
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
using Godot;
|
||||
|
||||
public static class IOrbitExtensions
|
||||
{
|
||||
public static void Draw(this IOrbit orbit, ImmediateGeometry geo)
|
||||
{
|
||||
geo.Clear();
|
||||
geo.Begin(Mesh.PrimitiveType.LineLoop);
|
||||
geo.SetColor(new Color(1, 0, 0));
|
||||
|
||||
int steps = 100;
|
||||
var ellipse = orbit.Ellipse;
|
||||
|
||||
for (int i = 0; i < steps; i++)
|
||||
{
|
||||
float t = i / (float)steps;
|
||||
float a = t * Mathf.Tau;
|
||||
|
||||
var v2 = ellipse.Foci[0] + ellipse.GetPosition(a);
|
||||
|
||||
geo.AddVertex(new Godot.Vector3
|
||||
{
|
||||
x = (float)v2.X,
|
||||
z = (float)v2.Y
|
||||
});
|
||||
}
|
||||
geo.End();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
public interface IPointMass : IMassive, ILocation
|
||||
{
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
public class Orbit
|
||||
{
|
||||
// The position of the primary body relative to the ellipse - the
|
||||
// orbit itself is presented as being centred on the primary, but
|
||||
// all our math is elliptical.
|
||||
private DVector2 PrimaryPosition => Ellipse.Focus0;
|
||||
|
||||
public Ellipse Ellipse { get; set; }
|
||||
|
||||
public DVector3 GetPosition(float t)
|
||||
{
|
||||
// TODO: determine period from mass of orbiting bodies
|
||||
var P = 10f;
|
||||
|
||||
var a = Ellipse.a;
|
||||
var e = Ellipse.e;
|
||||
|
||||
var M = Kepler.GetMeanAnomaly(t, P);
|
||||
var E = Kepler.GetEccentricAnomaly(e, M) + Math.PI;
|
||||
|
||||
var p = Kepler.GetEccentricPosition2D(e, a, t);
|
||||
return new DVector3(p.X, 0, p.Y);
|
||||
}
|
||||
|
||||
public void Draw(ImmediateGeometry geo)
|
||||
{
|
||||
geo.Clear();
|
||||
geo.Begin(Mesh.PrimitiveType.LineLoop);
|
||||
DrawEllipse(geo);
|
||||
geo.End();
|
||||
}
|
||||
|
||||
private void DrawEllipse(ImmediateGeometry geo)
|
||||
{
|
||||
geo.SetColor(new Color(1, 0, 0));
|
||||
|
||||
int steps = 100;
|
||||
|
||||
for (int i = 0; i < steps; i++)
|
||||
{
|
||||
float t = i / (float)steps;
|
||||
float a = t * Mathf.Tau;
|
||||
|
||||
var v2 = Ellipse.Focus0 + Ellipse.GetPosition(a);
|
||||
|
||||
geo.AddVertex(new Godot.Vector3
|
||||
{
|
||||
x = (float)v2.X,
|
||||
z = (float)v2.Y
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
[Tool]
|
||||
public class OrbitSystem : Node, IMassive, ILocation
|
||||
{
|
||||
[Export] private NodePath _a;
|
||||
[Export] private NodePath _b;
|
||||
[Export] private NodePath _barycenter;
|
||||
|
||||
private double _semiMajorAxis;
|
||||
[Export]
|
||||
private double SemiMajorAxis
|
||||
{
|
||||
get => _semiMajorAxis;
|
||||
set
|
||||
{
|
||||
_semiMajorAxis = value;
|
||||
|
||||
if (!Engine.EditorHint) return;
|
||||
|
||||
InvalidateGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private double _eccentricity;
|
||||
[Export(PropertyHint.Range, "0,1")]
|
||||
private double Eccentricity
|
||||
{
|
||||
get => _eccentricity;
|
||||
set
|
||||
{
|
||||
_eccentricity = value;
|
||||
|
||||
if (!Engine.EditorHint) return;
|
||||
|
||||
InvalidateGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
#region Point Masses
|
||||
private IPointMass Primary
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pointMasses[0] == null)
|
||||
{
|
||||
InitPointMasses();
|
||||
}
|
||||
return _pointMasses[0];
|
||||
}
|
||||
}
|
||||
private IPointMass Secondary
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pointMasses[1] == null)
|
||||
{
|
||||
InitPointMasses();
|
||||
}
|
||||
return _pointMasses[1];
|
||||
}
|
||||
}
|
||||
private readonly IPointMass[] _pointMasses = new IPointMass[2];
|
||||
private void InitPointMasses()
|
||||
{
|
||||
var a = GetNode<IPointMass>(_a);
|
||||
var b = GetNode<IPointMass>(_b);
|
||||
|
||||
if (a.Mass > b.Mass)
|
||||
{
|
||||
_pointMasses[0] = a;
|
||||
_pointMasses[1] = b;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pointMasses[0] = b;
|
||||
_pointMasses[1] = a;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public float Mass => Primary.Mass + Secondary.Mass;
|
||||
public DVector3 Position
|
||||
{
|
||||
get => Barycenter;
|
||||
set => Barycenter = value;
|
||||
}
|
||||
|
||||
public DVector3 Barycenter
|
||||
{
|
||||
get
|
||||
{
|
||||
var p0 = Primary.Position;
|
||||
var p1 = Secondary.Position;
|
||||
return p0.Lerp(p1, .5f);
|
||||
}
|
||||
// TODO - make setting the berycenter do something sensible?
|
||||
set => _ = value;
|
||||
}
|
||||
|
||||
private Orbit _orbit = null;
|
||||
private Orbit Orbit
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_orbit == null)
|
||||
{
|
||||
_orbit = new Orbit();
|
||||
}
|
||||
return _orbit;
|
||||
}
|
||||
}
|
||||
|
||||
private ImmediateGeometry _orbitGeometry = null;
|
||||
public ImmediateGeometry OrbitGeometry
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_orbitGeometry == null)
|
||||
{
|
||||
_orbitGeometry = new ImmediateGeometry();
|
||||
var m = new SpatialMaterial();
|
||||
m.VertexColorUseAsAlbedo = true;
|
||||
m.FlagsUnshaded = true;
|
||||
_orbitGeometry.MaterialOverride = m;
|
||||
AddChild(_orbitGeometry);
|
||||
}
|
||||
return _orbitGeometry;
|
||||
}
|
||||
}
|
||||
|
||||
private float _time = 0;
|
||||
|
||||
[Export]
|
||||
private float _speed = 3f;
|
||||
|
||||
public override void _Process(float delta)
|
||||
{
|
||||
_time += delta * _speed;
|
||||
InvalidateGeometry();
|
||||
}
|
||||
|
||||
private void InvalidateGeometry()
|
||||
{
|
||||
Orbit.Ellipse = new Ellipse(SemiMajorAxis, Eccentricity);
|
||||
Orbit.Draw(OrbitGeometry);
|
||||
|
||||
Secondary.Position = Orbit.GetPosition(_time);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
[Tool]
|
||||
public class Planet : Spatial, IPointMass
|
||||
{
|
||||
[Export]
|
||||
public float Mass { get; set; }
|
||||
|
||||
private DVector3? _position;
|
||||
public DVector3 Position
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_position.HasValue) return _position.Value;
|
||||
|
||||
var t = Translation;
|
||||
return new DVector3(t.x, t.y, t.z);
|
||||
}
|
||||
set
|
||||
{
|
||||
_position = value;
|
||||
Translation = new Godot.Vector3
|
||||
{
|
||||
x = (float)value.X,
|
||||
y = (float)value.Y,
|
||||
z = (float)value.Z
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
public struct Ellipse : IEllipse
|
||||
{
|
||||
public Ellipse(double a = 1, double e = 0)
|
||||
{
|
||||
SemiMajorAxis = a;
|
||||
Eccentricity = e;
|
||||
|
||||
// TODO: this is an immutable struct, so initialise everything else
|
||||
// in the constructor to avoid recalculating properties whenever
|
||||
// they are accessed
|
||||
|
||||
Apisides = new Apisides(a, e);
|
||||
b = Math.Sqrt(Apisides.max * Apisides.min);
|
||||
|
||||
var d = Math.Sqrt(a * a - b * b);
|
||||
Foci = new DVector2[]
|
||||
{
|
||||
new DVector2(-d, 0),
|
||||
new DVector2(d, 0)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Semi-major axis
|
||||
/// </summary>
|
||||
public double a => SemiMajorAxis;
|
||||
public double SemiMajorAxis { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Eccentricity
|
||||
/// </summary>
|
||||
public double e => Eccentricity;
|
||||
public double Eccentricity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Semi-minor axis
|
||||
/// </summary>
|
||||
public double b { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get a position on the auxiliary circle
|
||||
/// </summary>
|
||||
/// <param name="t">Angle in radians around the circle</param>
|
||||
/// <returns>2D position on the circle scaled by the semi-major axis</returns>
|
||||
public DVector2 GetAuxiliaryPosition2D(double t = 0) =>
|
||||
new DVector2(Math.Cos(t), Math.Sin(t)) * a;
|
||||
|
||||
private Apisides Apisides { get; }
|
||||
|
||||
public DVector2[] Foci { get; }
|
||||
|
||||
private double GetFocusDistance() => Math.Sqrt(a * a - b * b);
|
||||
}
|
||||
|
||||
public struct Apisides
|
||||
{
|
||||
public readonly double min;
|
||||
public readonly double max;
|
||||
|
||||
public Apisides(double a, double e)
|
||||
{
|
||||
min = a * (1 - e);
|
||||
max = a * (1 + e);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
public interface IEllipse
|
||||
{
|
||||
/// <summary>
|
||||
/// Semi-major axis
|
||||
/// </summary>
|
||||
double a { get; }
|
||||
double b { get; }
|
||||
/// <summary>
|
||||
/// Eccentricity
|
||||
/// </summary>
|
||||
double e { get; }
|
||||
/// <summary>
|
||||
/// Positions of the two foci of the ellipse
|
||||
/// </summary>
|
||||
DVector2[] Foci { get; }
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
using Vim.Math3d;
|
||||
using System;
|
||||
|
||||
public static class IEllipseExtensions
|
||||
{
|
||||
public static DVector2 GetPosition(this IEllipse e, double t) =>
|
||||
new DVector2(Math.Cos(t) * e.a, Math.Sin(t) * e.b);
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
using static System.Math;
|
||||
using Vim.Math3d;
|
||||
|
||||
public static class Kepler
|
||||
{
|
||||
/// <summary>
|
||||
/// 2 * PI
|
||||
/// </summary>
|
||||
private static double TAU => PI * 2.0;
|
||||
/// <summary>
|
||||
/// Universal gravitational constant
|
||||
/// </summary>
|
||||
private static double G => 6.677e-11;
|
||||
|
||||
/// <summary>
|
||||
/// Get the mean anomaly of an orbit given at a given time, given
|
||||
/// the period of the orbit.
|
||||
/// </summary>
|
||||
/// <param name="time">Time of mean anomaly</param>
|
||||
/// <param name="P">Orbital period</param>
|
||||
/// <returns></returns>
|
||||
public static double GetMeanAnomaly(double time, double P)
|
||||
{
|
||||
var M = ((time % P / P) * TAU) % TAU;
|
||||
return M % TAU;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="P">Orbital period</param>
|
||||
/// <returns></returns>
|
||||
public static double GetMeanAngularVelocity(double P) => TAU / P;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="a">Semimajor axis in AU</param>
|
||||
/// <param name="U">Standard gravitational parameter</param>
|
||||
/// <returns></returns>
|
||||
public static double GetPeriod(double a, double U)
|
||||
{
|
||||
var a3 = a * a * a;
|
||||
var T = TAU * Sqrt(a3 / U);
|
||||
|
||||
return T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the standard gravitational parameter of a mass
|
||||
/// </summary>
|
||||
/// <param name="M">Mass in kilograms</param>
|
||||
/// <returns>Standard gravitational parameter</returns>
|
||||
public static double U(double M) => M * G;
|
||||
/// <summary>
|
||||
/// Get the standard gravitational parameter of a mass
|
||||
/// </summary>
|
||||
/// <param name="M">Mass in kilograms</param>
|
||||
/// <returns>Standard gravitational parameter</returns>
|
||||
public static double GetStandardGravitationalParameter(double M) => M * G;
|
||||
|
||||
/// <summary>
|
||||
/// Get the eccentric anomaly for a given mean anomaly.
|
||||
/// </summary>
|
||||
/// <param name="e">Eccentricity</param>
|
||||
/// <param name="M">Mean anomaly</param>
|
||||
/// <returns>Eccentric anomaly</returns>
|
||||
public static double GetEccentricAnomaly(double e, double M) =>
|
||||
GetEccentricAnomalyBS(e, M);
|
||||
|
||||
/// <summary>
|
||||
/// Get the eccentric anomaly for a given mean anomaly
|
||||
/// using a binary search.
|
||||
/// </summary>
|
||||
/// <param name="e">Eccentricity</param>
|
||||
/// <param name="M">Mean anomaly</param>
|
||||
/// <returns>Eccentric anomaly</returns>
|
||||
private static double GetEccentricAnomalyBS(double e, double M)
|
||||
{
|
||||
// from Astronomical Algorithms, p. 206
|
||||
const int iterations = 33;
|
||||
double E0, D;
|
||||
|
||||
double F = Sign(M);
|
||||
M = Abs(M) / TAU;
|
||||
M = (M - (int)M) * TAU * F;
|
||||
if (M < 0)
|
||||
{
|
||||
M = M + TAU;
|
||||
}
|
||||
F = 1.0;
|
||||
if (M > PI)
|
||||
{
|
||||
F = -1.0;
|
||||
M = TAU - M;
|
||||
}
|
||||
E0 = PI / 2;
|
||||
D = PI / 4;
|
||||
for (int J = 0; J < iterations; J++)
|
||||
{
|
||||
double M1 = E0 - e * Sin(E0);
|
||||
E0 = E0 + D * Sign(M - M1);
|
||||
D *= 0.5;
|
||||
}
|
||||
E0 *= F;
|
||||
|
||||
return E0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Cartesian position on an orbit given its eccentricity,
|
||||
/// its semi-major axis and a given mean anomaly.
|
||||
/// </summary>
|
||||
/// <param name="e">Eccentricity</param>
|
||||
/// <param name="a">Semi-major axis</param>
|
||||
/// <param name="M">Mean anomaly</param>
|
||||
/// <returns>Position on the orbit</returns>
|
||||
public static DVector2 GetEccentricPosition2D(double e, double a, double M)
|
||||
{
|
||||
var E = Kepler.GetEccentricAnomaly(e, M);
|
||||
|
||||
var P = a * (Cos(E) - e);
|
||||
var Q = a * Sin(E) * Sqrt(1.0 - Pow(e, 2));
|
||||
|
||||
return new DVector2(P, Q);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue