shmodot/scripts/orbits/Orbit.cs

49 lines
1.1 KiB
C#

using Godot;
using System;
using Vim.Math3d;
public class Orbit
{
// 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 ellipse stuff
private DVector2 PrimaryPosition => Ellipse.Focus0;
public Ellipse Ellipse { get; set; }
public DVector3 GetPosition(float t)
{
var p = PrimaryPosition + Ellipse.GetPosition(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
});
}
}
}