2022-08-31 22:18:47 +02:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class OrbitSystem : Node, IMassive, ILocation
|
|
|
|
{
|
|
|
|
[Export] private NodePath _a;
|
|
|
|
[Export] private NodePath _b;
|
|
|
|
[Export] private NodePath _barycenter;
|
|
|
|
|
|
|
|
private readonly PointMass[] _pointMasses = new PointMass[2];
|
|
|
|
|
|
|
|
public Vector3 Position => Barycenter;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public Vector3 Barycenter
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var p0 = _pointMasses[0].Position;
|
|
|
|
var p1 = _pointMasses[1].Position;
|
|
|
|
|
|
|
|
return p0.LinearInterpolate(p1, .5f);
|
|
|
|
}
|
|
|
|
set => _ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float Mass { get; } = 1;
|
|
|
|
|
2022-09-04 12:35:13 +02:00
|
|
|
private Orbit _orbit;
|
|
|
|
|
|
|
|
private ImmediateGeometry _orbitGeometry;
|
2022-08-31 22:18:47 +02:00
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
InitPointMasses();
|
|
|
|
InitGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
|
|
|
DrawOrbit();
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:35:13 +02:00
|
|
|
private void InitOrbit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:18:47 +02:00
|
|
|
private void DrawOrbit()
|
|
|
|
{
|
|
|
|
int steps = 100;
|
|
|
|
|
2022-09-04 12:35:13 +02:00
|
|
|
_orbitGeometry.Clear();
|
|
|
|
_orbitGeometry.Begin(Mesh.PrimitiveType.LineLoop);
|
|
|
|
_orbitGeometry.SetColor(new Color(1, 0, 0));
|
|
|
|
|
|
|
|
var ellipse = new Ellipse(1, .5);
|
|
|
|
|
2022-08-31 22:18:47 +02:00
|
|
|
for (int i = 0; i < steps; i++)
|
|
|
|
{
|
|
|
|
float t = i / (float)steps;
|
|
|
|
float a = t * Mathf.Tau;
|
|
|
|
|
2022-09-04 12:35:13 +02:00
|
|
|
var v2 = ellipse.Focus0 + ellipse.GetPosition(a);
|
|
|
|
|
|
|
|
_orbitGeometry.AddVertex(new Vector3
|
2022-08-31 22:18:47 +02:00
|
|
|
{
|
2022-09-04 12:35:13 +02:00
|
|
|
x = (float)v2.X,
|
|
|
|
z = (float)v2.Y
|
2022-08-31 22:18:47 +02:00
|
|
|
});
|
|
|
|
}
|
2022-09-04 12:35:13 +02:00
|
|
|
_orbitGeometry.End();
|
2022-08-31 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InitGeometry()
|
|
|
|
{
|
2022-09-04 12:35:13 +02:00
|
|
|
_orbitGeometry = new ImmediateGeometry();
|
2022-08-31 22:18:47 +02:00
|
|
|
var m = new SpatialMaterial();
|
|
|
|
m.VertexColorUseAsAlbedo = true;
|
|
|
|
m.FlagsUnshaded = true;
|
2022-09-04 12:35:13 +02:00
|
|
|
_orbitGeometry.MaterialOverride = m;
|
|
|
|
AddChild(_orbitGeometry);
|
2022-08-31 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InitPointMasses()
|
|
|
|
{
|
|
|
|
var a = GetPointMass(_a);
|
|
|
|
var b = GetPointMass(_b);
|
|
|
|
|
|
|
|
if (a.Mass > b.Mass)
|
|
|
|
{
|
|
|
|
_pointMasses[0] = a;
|
|
|
|
_pointMasses[1] = b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_pointMasses[0] = b;
|
|
|
|
_pointMasses[1] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private PointMass GetPointMass(NodePath path)
|
|
|
|
{
|
|
|
|
var spatial = GetNode<Spatial>(path);
|
|
|
|
//var massive = node.GetChild<IMassive>(0);
|
|
|
|
|
|
|
|
return new PointMass(spatial, 1f);
|
|
|
|
}
|
|
|
|
}
|