Compare commits

...

5 Commits

Author SHA1 Message Date
ktyl cc3844f920 update main scene 2022-09-07 00:54:41 +01:00
ktyl 694b2b576c use IMassive to calculate period 2022-09-07 00:54:41 +01:00
ktyl 70c4cd8a92 OrbitSystem -> Barycenter 2022-09-07 00:54:41 +01:00
ktyl 5ad70b3f9c extract IOrbit and IOrbitExtensions 2022-09-07 00:54:22 +01:00
ktyl bed71f2c4c extract IEllipse and extensions 2022-09-07 00:53:56 +01:00
13 changed files with 191 additions and 146 deletions

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=2] [gd_scene load_steps=3 format=2]
[ext_resource path="res://scenes/OrbitCamera.tscn" type="PackedScene" id=3] [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"] [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="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 ) 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 SemiMajorAxis = 6.166
Eccentricity = 0.239 Eccentricity = 0.239
Period = 1.0
_speed = 0.877 _speed = 0.877

View File

@ -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

View File

@ -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

View File

@ -1,13 +1,13 @@
using Godot; using Godot;
using System; using System;
using System.Collections.Generic;
using Vim.Math3d; using Vim.Math3d;
[Tool] [Tool]
public class OrbitSystem : Node, IMassive, ILocation public class Barycenter : Node, IPointMass
{ {
[Export] private NodePath _a; [Export]
[Export] private NodePath _b; private NodePath[] _pointMassPaths;
[Export] private NodePath _barycenter;
private double _semiMajorAxis; private double _semiMajorAxis;
[Export] [Export]
@ -40,79 +40,19 @@ public class OrbitSystem : Node, IMassive, ILocation
} }
} }
#region Point Masses public double Mass
private IPointMass Primary
{ {
get get
{ {
if (_pointMasses[0] == null) double mass = 0;
foreach (var orbiter in Orbiters)
{ {
InitPointMasses(); mass += orbiter.PointMass.Mass;
} }
return _pointMasses[0]; return mass;
}
}
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;
} }
} }
public DVector3 Position { get; set; }
private ImmediateGeometry _orbitGeometry = null; private ImmediateGeometry _orbitGeometry = null;
public ImmediateGeometry OrbitGeometry 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; private float _time = 0;
[Export] [Export]
@ -145,9 +129,15 @@ public class OrbitSystem : Node, IMassive, ILocation
private void InvalidateGeometry() private void InvalidateGeometry()
{ {
Orbit.Ellipse = new Ellipse(SemiMajorAxis, Eccentricity); for (int i = 0; i < Orbiters.Length; i++)
Orbit.Draw(OrbitGeometry); {
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);
}
} }
} }

View File

@ -3,5 +3,5 @@ using System;
public interface IMassive public interface IMassive
{ {
float Mass { get; } double Mass { get; }
} }

View File

@ -0,0 +1,4 @@
public static class IMassiveExtensions
{
public static double U(this IMassive m) => Kepler.U(m.Mass);
}

7
scripts/orbits/IOrbit.cs Normal file
View File

@ -0,0 +1,7 @@
using Vim.Math3d;
public interface IOrbit
{
DVector3 GetPosition(double t);
IEllipse Ellipse { get; set; }
}

View File

@ -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();
}
}

View File

@ -2,56 +2,42 @@ using Godot;
using System; using System;
using Vim.Math3d; using Vim.Math3d;
public class Orbit public class Orbit : IOrbit
{ {
// The position of the primary body relative to the ellipse - the public IEllipse Ellipse { get; set; }
// 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; } 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 // TODO: determine ellipse better
var P = 10f; // 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 a = Ellipse.a;
var e = Ellipse.e; var e = Ellipse.e;
var P = Kepler.GetPeriod(a, _massive.U());
var M = Kepler.GetMeanAnomaly(t, P); var M = Kepler.GetMeanAnomaly(t, P);
var E = Kepler.GetEccentricAnomaly(e, M) + Math.PI; var E = Kepler.GetEccentricAnomaly(e, M) + Math.PI;
var p = Kepler.GetEccentricPosition2D(e, a, t); var p = Kepler.GetEccentricPosition2D(e, a, t);
return new DVector3(p.X, 0, p.Y); 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
});
}
}
} }

View File

@ -6,7 +6,7 @@ using Vim.Math3d;
public class Planet : Spatial, IPointMass public class Planet : Spatial, IPointMass
{ {
[Export] [Export]
public float Mass { get; set; } public double Mass { get; set; }
private DVector3? _position; private DVector3? _position;
public DVector3 Position public DVector3 Position

View File

@ -2,7 +2,7 @@ using Godot;
using System; using System;
using Vim.Math3d; using Vim.Math3d;
public struct Ellipse public struct Ellipse : IEllipse
{ {
public Ellipse(double a = 1, double e = 0) 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 // TODO: this is an immutable struct, so initialise everything else
// in the constructor to avoid recalculating properties whenever // in the constructor to avoid recalculating properties whenever
// they are accessed // 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> /// <summary>
@ -29,8 +39,7 @@ public struct Ellipse
/// <summary> /// <summary>
/// Semi-minor axis /// Semi-minor axis
/// </summary> /// </summary>
public double b => SemiMinorAxis; public double b { get; }
public double SemiMinorAxis => Math.Sqrt(Apisides.max * Apisides.min);
/// <summary> /// <summary>
/// Get a position on the auxiliary circle /// Get a position on the auxiliary circle
@ -40,13 +49,10 @@ public struct Ellipse
public DVector2 GetAuxiliaryPosition2D(double t = 0) => public DVector2 GetAuxiliaryPosition2D(double t = 0) =>
new DVector2(Math.Cos(t), Math.Sin(t)) * a; 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) => public DVector2[] Foci { get; }
new DVector2(Math.Cos(t) * a, Math.Sin(t) * b);
public DVector2 Focus0 => new DVector2(-GetFocusDistance(), 0);
public DVector2 Focus1 => new DVector2(GetFocusDistance(), 0);
private double GetFocusDistance() => Math.Sqrt(a * a - b * b); private double GetFocusDistance() => Math.Sqrt(a * a - b * b);
} }

View File

@ -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; }
}

View File

@ -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);
}