shmodot/scripts/orbits/Orbit.cs

49 lines
1.1 KiB
C#
Raw Normal View History

2022-09-04 12:35:13 +02:00
using Godot;
using System;
2022-09-05 01:46:18 +02:00
using Vim.Math3d;
2022-09-04 12:35:13 +02:00
public class Orbit
{
2022-09-05 01:46:18 +02:00
// 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)
2022-09-04 12:35:13 +02:00
{
2022-09-05 01:46:18 +02:00
var p = PrimaryPosition + Ellipse.GetPosition(t);
return new DVector3(p.X, 0, p.Y);
}
2022-09-04 12:35:13 +02:00
2022-09-05 01:46:18 +02:00
public void Draw(ImmediateGeometry geo)
{
geo.Clear();
geo.Begin(Mesh.PrimitiveType.LineLoop);
DrawEllipse(geo);
geo.End();
2022-09-04 12:35:13 +02:00
}
2022-09-05 01:46:18 +02:00
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
});
}
}
2022-09-04 12:35:13 +02:00
}