Compare commits
5 Commits
5197362280
...
cc3844f920
Author | SHA1 | Date |
---|---|---|
ktyl | cc3844f920 | |
ktyl | 694b2b576c | |
ktyl | 70c4cd8a92 | |
ktyl | 5ad70b3f9c | |
ktyl | bed71f2c4c |
|
@ -1,7 +1,7 @@
|
|||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3]
|
||||
[ext_resource path="res://scenes/orbits/OrbitSystem.tscn" type="PackedScene" id=4]
|
||||
[ext_resource path="res://scenes/orbits/Barycenter.tscn" type="PackedScene" id=4]
|
||||
|
||||
[node name="Main" type="Node2D"]
|
||||
|
||||
|
@ -10,9 +10,8 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461
|
|||
|
||||
[node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
|
||||
|
||||
[node name="Orbit System" parent="." instance=ExtResource( 4 )]
|
||||
[node name="Barycenter" 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,18 @@
|
|||
[gd_scene load_steps=3 format=2]
|
||||
|
||||
[ext_resource path="res://scripts/orbits/Barycenter.cs" type="Script" id=1]
|
||||
[ext_resource path="res://scenes/orbits/Planet.tscn" type="PackedScene" id=2]
|
||||
|
||||
[node name="Barycenter" type="Spatial"]
|
||||
script = ExtResource( 1 )
|
||||
SemiMajorAxis = 7.0
|
||||
Eccentricity = 0.752
|
||||
_pointMassPaths = [ NodePath("Planet A"), NodePath("Planet B") ]
|
||||
_speed = 1.0
|
||||
|
||||
[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, -2.55711, 0, -4.25519 )
|
||||
|
||||
[node name="Planet B" parent="." instance=ExtResource( 2 )]
|
||||
transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, -11.9998, 0, 1.25565 )
|
||||
Mass = 0.125
|
|
@ -1,22 +0,0 @@
|
|||
[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
|
|
@ -1,13 +1,13 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Vim.Math3d;
|
||||
|
||||
[Tool]
|
||||
public class OrbitSystem : Node, IMassive, ILocation
|
||||
public class Barycenter : Node, IPointMass
|
||||
{
|
||||
[Export] private NodePath _a;
|
||||
[Export] private NodePath _b;
|
||||
[Export] private NodePath _barycenter;
|
||||
[Export]
|
||||
private NodePath[] _pointMassPaths;
|
||||
|
||||
private double _semiMajorAxis;
|
||||
[Export]
|
||||
|
@ -40,79 +40,19 @@ public class OrbitSystem : Node, IMassive, ILocation
|
|||
}
|
||||
}
|
||||
|
||||
#region Point Masses
|
||||
private IPointMass Primary
|
||||
public double Mass
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pointMasses[0] == null)
|
||||
double mass = 0;
|
||||
foreach (var orbiter in Orbiters)
|
||||
{
|
||||
InitPointMasses();
|
||||
mass += orbiter.PointMass.Mass;
|
||||
}
|
||||
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;
|
||||
return mass;
|
||||
}
|
||||
}
|
||||
public DVector3 Position { get; set; }
|
||||
|
||||
private ImmediateGeometry _orbitGeometry = null;
|
||||
public ImmediateGeometry OrbitGeometry
|
||||
|
@ -132,6 +72,50 @@ public class OrbitSystem : Node, IMassive, ILocation
|
|||
}
|
||||
}
|
||||
|
||||
private struct Orbiter
|
||||
{
|
||||
public IPointMass PointMass { get; set; }
|
||||
public IOrbit Orbit { get; set; }
|
||||
}
|
||||
private Orbiter[] _orbiters = null;
|
||||
private Orbiter[] Orbiters
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_orbiters == null)
|
||||
{
|
||||
var length = _pointMassPaths.Length;
|
||||
_orbiters = new Orbiter[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var pointMass = GetNode<IPointMass>(_pointMassPaths[i]);
|
||||
var orbit = new Orbit(this, pointMass);
|
||||
|
||||
var orbiter = new Orbiter
|
||||
{
|
||||
PointMass = pointMass,
|
||||
Orbit = orbit
|
||||
};
|
||||
_orbiters[i] = orbiter;
|
||||
}
|
||||
}
|
||||
return _orbiters;
|
||||
}
|
||||
}
|
||||
|
||||
private IOrbit[] _orbits;
|
||||
private IOrbit[] Orbits
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_orbits == null)
|
||||
{
|
||||
|
||||
}
|
||||
return _orbits;
|
||||
}
|
||||
}
|
||||
|
||||
private float _time = 0;
|
||||
|
||||
[Export]
|
||||
|
@ -145,9 +129,15 @@ public class OrbitSystem : Node, IMassive, ILocation
|
|||
|
||||
private void InvalidateGeometry()
|
||||
{
|
||||
Orbit.Ellipse = new Ellipse(SemiMajorAxis, Eccentricity);
|
||||
Orbit.Draw(OrbitGeometry);
|
||||
for (int i = 0; i < Orbiters.Length; i++)
|
||||
{
|
||||
var orbiter = Orbiters[i];
|
||||
var orbit = orbiter.Orbit;
|
||||
orbit.Ellipse = new Ellipse(SemiMajorAxis, Eccentricity);
|
||||
orbit.Draw(OrbitGeometry);
|
||||
|
||||
Secondary.Position = Orbit.GetPosition(_time);
|
||||
var t = _time + i * Math.PI;
|
||||
orbiter.PointMass.Position = orbit.GetPosition(t);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,5 +3,5 @@ using System;
|
|||
|
||||
public interface IMassive
|
||||
{
|
||||
float Mass { get; }
|
||||
double Mass { get; }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
public static class IMassiveExtensions
|
||||
{
|
||||
public static double U(this IMassive m) => Kepler.U(m.Mass);
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -2,56 +2,42 @@ using Godot;
|
|||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
public class Orbit
|
||||
public class Orbit : IOrbit
|
||||
{
|
||||
// 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 IEllipse Ellipse { get; set; }
|
||||
|
||||
public Ellipse Ellipse { get; set; }
|
||||
private readonly IMassive _massive;
|
||||
private readonly ILocation _satellite;
|
||||
|
||||
public DVector3 GetPosition(float t)
|
||||
/// <summary>
|
||||
/// An orbit is treated as one stationary body at one focus of the orbit's ellipse.
|
||||
/// The satellite is treated as a massless point.
|
||||
/// </summary>
|
||||
/// <param name="massive">Mass of the object being orbited</param>
|
||||
/// <param name="satellite">The orbiting body</param>
|
||||
public Orbit(IMassive massive, ILocation satellite)
|
||||
{
|
||||
// TODO: determine period from mass of orbiting bodies
|
||||
var P = 10f;
|
||||
// TODO: determine ellipse better
|
||||
// semi-major axis is distance of satellite from origin
|
||||
var a = satellite.Position.Magnitude();
|
||||
var e = 0.8f;
|
||||
this.Ellipse = new Ellipse(a, e);
|
||||
|
||||
_massive = massive;
|
||||
_satellite = satellite;
|
||||
}
|
||||
|
||||
public DVector3 GetPosition(double t)
|
||||
{
|
||||
var a = Ellipse.a;
|
||||
var e = Ellipse.e;
|
||||
|
||||
var P = Kepler.GetPeriod(a, _massive.U());
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ using Vim.Math3d;
|
|||
public class Planet : Spatial, IPointMass
|
||||
{
|
||||
[Export]
|
||||
public float Mass { get; set; }
|
||||
public double Mass { get; set; }
|
||||
|
||||
private DVector3? _position;
|
||||
public DVector3 Position
|
||||
|
|
|
@ -2,7 +2,7 @@ using Godot;
|
|||
using System;
|
||||
using Vim.Math3d;
|
||||
|
||||
public struct Ellipse
|
||||
public struct Ellipse : IEllipse
|
||||
{
|
||||
public Ellipse(double a = 1, double e = 0)
|
||||
{
|
||||
|
@ -12,6 +12,16 @@ public struct Ellipse
|
|||
// 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>
|
||||
|
@ -29,8 +39,7 @@ public struct Ellipse
|
|||
/// <summary>
|
||||
/// Semi-minor axis
|
||||
/// </summary>
|
||||
public double b => SemiMinorAxis;
|
||||
public double SemiMinorAxis => Math.Sqrt(Apisides.max * Apisides.min);
|
||||
public double b { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get a position on the auxiliary circle
|
||||
|
@ -40,13 +49,10 @@ public struct Ellipse
|
|||
public DVector2 GetAuxiliaryPosition2D(double t = 0) =>
|
||||
new DVector2(Math.Cos(t), Math.Sin(t)) * a;
|
||||
|
||||
private Apisides Apisides => new Apisides(a, e);
|
||||
private Apisides Apisides { get; }
|
||||
|
||||
public DVector2 GetPosition(double t) =>
|
||||
new DVector2(Math.Cos(t) * a, Math.Sin(t) * b);
|
||||
public DVector2[] Foci { get; }
|
||||
|
||||
public DVector2 Focus0 => new DVector2(-GetFocusDistance(), 0);
|
||||
public DVector2 Focus1 => new DVector2(GetFocusDistance(), 0);
|
||||
private double GetFocusDistance() => Math.Sqrt(a * a - b * b);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue