shmodot/scripts/orbits/Orbit.cs

44 lines
1.2 KiB
C#
Raw Normal View History

using Godot;
using System;
using Vim.Math3d;
2022-09-07 01:49:57 +02:00
public class Orbit : IOrbit
{
2022-09-07 01:49:57 +02:00
public IEllipse Ellipse { get; set; }
private readonly IMassive _massive;
private readonly ILocation _satellite;
/// <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)
{
2022-09-07 01:49:57 +02:00
// 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;
}
2022-09-05 23:12:56 +02:00
2022-09-07 01:49:57 +02:00
public DVector3 GetPosition(double t)
{
2022-09-05 23:12:56 +02:00
var a = Ellipse.a;
var e = Ellipse.e;
2022-09-07 01:49:57 +02:00
var P = Kepler.GetPeriod(a, _massive.U());
2022-09-05 23:12:56 +02:00
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);
}
}