Fix folder case in src
This commit is contained in:
116
src/Testing/TestThruster.cs
Normal file
116
src/Testing/TestThruster.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using Godot;
|
||||
using ImGuiNET;
|
||||
using Quadratic.Carto.MathExt;
|
||||
|
||||
namespace Quadratic.Carto.Testing;
|
||||
|
||||
public partial class TestThruster : RigidBody3D
|
||||
{
|
||||
/// <summary>
|
||||
/// Throttle level between 0 and 1
|
||||
/// </summary>
|
||||
public float Throttle
|
||||
{
|
||||
get => _throttle; set
|
||||
{
|
||||
_throttle = Mathf.Clamp(value, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
float _throttle = 0.0f;
|
||||
|
||||
Vector3 torque = new Vector3();
|
||||
|
||||
bool stabilize = false;
|
||||
bool stabilizeDebounce = false;
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
float throttleStep = 0.2f * (float)delta;
|
||||
if (Input.IsKeyPressed(Key.Shift))
|
||||
{
|
||||
Throttle += throttleStep;
|
||||
}
|
||||
else if (Input.IsKeyPressed(Key.Ctrl))
|
||||
{
|
||||
Throttle -= throttleStep;
|
||||
}
|
||||
|
||||
var torque = Vector3.Zero;
|
||||
float torqueStep = 0.1f;
|
||||
if (Input.IsKeyPressed(Key.W))
|
||||
{
|
||||
torque += new Vector3(-1.0f, 0.0f, 0.0f); // Forward (X-)
|
||||
}
|
||||
if (Input.IsKeyPressed(Key.S))
|
||||
{
|
||||
torque += new Vector3(1.0f, 0.0f, 0.0f); // Backward (X+)
|
||||
}
|
||||
if (Input.IsKeyPressed(Key.A))
|
||||
{
|
||||
torque += new Vector3(0.0f, 0.0f, 1.0f); // Left (Z+)
|
||||
}
|
||||
if (Input.IsKeyPressed(Key.D))
|
||||
{
|
||||
torque += new Vector3(0.0f, 0.0f, -1.0f); // Right (Z-)
|
||||
}
|
||||
if (Input.IsKeyPressed(Key.Q))
|
||||
{
|
||||
torque += new Vector3(0.0f, 1.0f, 0.0f); // Up (Y+)
|
||||
}
|
||||
if (Input.IsKeyPressed(Key.E))
|
||||
{
|
||||
torque += new Vector3(0.0f, -1.0f, 0.0f); // Down (Y-)
|
||||
}
|
||||
torque *= torqueStep;
|
||||
this.torque = torque;
|
||||
|
||||
if (Input.IsKeyPressed(Key.T) && !stabilizeDebounce)
|
||||
{
|
||||
stabilizeDebounce = true;
|
||||
stabilize = !stabilize;
|
||||
}
|
||||
else if (!Input.IsKeyPressed(Key.T) && stabilizeDebounce)
|
||||
{
|
||||
stabilizeDebounce = false;
|
||||
}
|
||||
|
||||
// Debug window
|
||||
ImGui.Begin("Debug");
|
||||
ImGui.Text("Status");
|
||||
ImGui.LabelText("Position", this.GlobalPosition.ToString());
|
||||
var vel = this.LinearVelocity.AsSystem();
|
||||
ImGui.SliderFloat3("Velocity", ref vel, -100.0f, 100.0f);
|
||||
var angVel = this.AngularVelocity.AsSystem();
|
||||
ImGui.SliderFloat3("Angular Velocity", ref angVel, -100.0f, 100.0f);
|
||||
|
||||
ImGui.Text("Controls");
|
||||
ImGui.SliderFloat("Throttle", ref _throttle, 0.0f, 1.0f);
|
||||
ImGui.Checkbox("Stabilize", ref stabilize);
|
||||
var torqueVec = torque.AsSystem();
|
||||
ImGui.SliderFloat3("Torque", ref torqueVec, -1.0f, 1.0f);
|
||||
ImGui.End();
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var thrust = Throttle * 100.0f;
|
||||
var thrustVec = this.Transform.Basis * new Vector3(0.0f, thrust, 0f);
|
||||
this.ApplyCentralForce(thrustVec);
|
||||
|
||||
Vector3 torque;
|
||||
if (!this.torque.IsZeroApprox())
|
||||
{
|
||||
torque = this.Transform.Basis * this.torque;
|
||||
}
|
||||
else if (stabilize)
|
||||
{
|
||||
torque = -AngularVelocity * 0.1f;
|
||||
}
|
||||
else
|
||||
{
|
||||
torque = Vector3.Zero;
|
||||
}
|
||||
ApplyTorque(torque);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user