44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
using Godot;
|
|
using System;
|
|
using Vim.Math3d;
|
|
|
|
public class Orbit : IOrbit
|
|
{
|
|
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)
|
|
{
|
|
// 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);
|
|
}
|
|
}
|