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();
}

14
src/Craft/Vessel.cs Normal file
View File

@@ -0,0 +1,14 @@
using Godot;
namespace Quadratic.Carto.Craft;
/// <summary>
/// A Vessel is the smallest unit of object in the game that is considered a
/// whole on its own, such as a space station or an astronaut. Connections can
/// be made between vessels, but each vessel is still a separate entity.
/// </summary>
/// <remarks>
/// Vessel is an abstract class, and is meant to be extended by concrete
/// implementations.
/// </remarks>
public abstract class Vessel { }

9
src/Craft/VesselNode.cs Normal file
View File

@@ -0,0 +1,9 @@
using Godot;
namespace Quadratic.Carto.Craft;
/// <summary>
/// A VesselNode is the node that represents a <see cref="Vessel"/> currently
/// loaded into the game world.
/// </summary>
public abstract partial class VesselNode : Node3D { }