Initial orbiting craft

This commit is contained in:
2024-11-06 22:16:43 +08:00
parent a6e116c77f
commit 518ee9d429
6 changed files with 194 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ using System.Diagnostics;
using Godot;
using ImGuiNET;
using Quadratic.Carto.MathExt;
using Vim.Math3d;
namespace Quadratic.Carto.Craft;
@@ -20,8 +21,24 @@ public sealed partial class Krakensbane : Node3D
public Node3D? FocusedVessel { get; set; }
const float maxDistance = 500.0f;
const float planetRadius = 6372.0f;
DVector3 originPosition = DVector3.Zero;
DVector3 originPosition = DVector3.UnitY * planetRadius;
/// <summary>
/// Something to manage the non-inertial frame of reference.
/// </summary>
/// <para>
/// It's good to have an inertial frame of reference when you're close to
/// a lot of static stuff, like on the ground. However, a non-inertial frame
/// of reference might be more appropriate when you're in deep space.
/// </para>
/// <para>
/// This provider field is null if we're using an inertial frame of reference.
/// When it's not null, it should be responsible for managing the acceleration,
/// angular acceleration, and even positions and/or rotations of children
/// objects.
INonInertialFrameProvider? nonInertialFrameProvider;
public override void _PhysicsProcess(double delta)
{
@@ -52,6 +69,27 @@ public sealed partial class Krakensbane : Node3D
originPosition += vesselPosition.AsVim().AsDouble();
}
// Apply gravity
foreach (var child in GetChildren())
{
if (child is RigidBody3D rb)
{
var localPosition = rb.Transform.Origin.AsVim().AsDouble();
var globalPosition = originPosition + localPosition;
var gravityVector = -globalPosition.Normalize();
var floatGravityVector = gravityVector.AsSingle().AsGodot();
var distance = globalPosition.Length();
var gravity = 9.81f * planetRadius * planetRadius / (distance * distance);
rb.ApplyCentralForce(floatGravityVector * (float)gravity);
}
}
}
public DVector3 GetPositionOf(Node3D node)
{
var localPosition = node.Transform.Origin.AsVim().AsDouble();
return originPosition + localPosition;
}
public override void _Process(double delta)
@@ -61,3 +99,9 @@ public sealed partial class Krakensbane : Node3D
ImGui.End();
}
}
public interface INonInertialFrameProvider
{
public DVector3 GetCurrentAcceleration();
public DVector3 GetCurrentAngularAcceleration();
}