Ship.Update reads Turn and feeds the rotation thrusters and aether drives; it no longer reads the look direction or snaps Direction toward it. Steering.Toward is the one law from heading to demand (a heading astern now turns at full demand). HelmInput owns the face-the-aim state. Accelerate, Combat and MoveTo write Turn through Steering.Toward. Entity.LookDirection is renamed Aim in the simulation, the Unity readers and the tests. Unity player input follows in the next commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
24 lines
1.1 KiB
C#
24 lines
1.1 KiB
C#
using System;
|
|
using CultMath;
|
|
using static CultMath.math;
|
|
|
|
// The one law that turns a wanted heading into a facing command. Ship.Update reads only Ship.Turn; whoever
|
|
// wants the hull to face somewhere (the player's face-the-aim state, an agent state) asks here and writes Turn.
|
|
public static class Steering
|
|
{
|
|
const float Deadband = .01f;
|
|
|
|
// Positive is clockwise. Lateral component of the heading with sqrt shaping and a deadband; a heading
|
|
// behind the hull turns at full demand (its lateral component alone would vanish at exactly astern).
|
|
// A zero or non-finite heading holds course.
|
|
public static float Toward(Ship ship, float2 heading)
|
|
{
|
|
if (!float.IsFinite(heading.x) || !float.IsFinite(heading.y) || lengthsq(heading) < 1e-12f) return 0f;
|
|
var dir = normalize(ship.Direction);
|
|
var wanted = normalize(heading);
|
|
var d = dot(wanted, dir.Rotate(ItemRotation.Clockwise));
|
|
if (dot(wanted, dir) < 0f) return d < 0f ? -1f : 1f;
|
|
if (abs(d) < Deadband) return 0f;
|
|
return sqrt(abs(d)) * sign(d);
|
|
}
|
|
}
|