use IMassive to calculate period

This commit is contained in:
ktyl 2022-09-07 00:49:57 +01:00
parent 70c4cd8a92
commit 694b2b576c
4 changed files with 30 additions and 40 deletions

View File

@ -3,5 +3,5 @@ using System;
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);
}

View File

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

View File

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