Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FnBPosVelTrackerF
- {
- public Vector3D Position = Vector3D.Zero;
- public Vector3D Velocity = Vector3D.Zero;
- public bool Ready = false;
- public bool inFnB = false;
- const int _cadence = 2;//cadence from mod
- const double safeLimit = 250;//empirical under cap even under aggressive spin
- // ---- teleport-phase histogram (running, argmax-decided, NO latch) ----
- int[] _tpCounts = new int[_cadence]; // counts of excess events per tick%cadence
- int _tpPhase = -1; // argmax; recomputed each tick
- // ---- teleport-mode state ----
- bool _teleportObserved;
- int _misses = 0; // consecutive tp-phase ticks w/ no excess
- Vector3D _excess = Vector3D.Zero;
- // ---- position/vel carry-forward ----
- bool _lastPosValid;
- Vector3D _lastPos = Vector3D.Zero;
- Vector3D _predPos = Vector3D.Zero;
- const double DT = 1.0 / 60.0;
- double jitterChk = 0.1;
- int ticks = 0;
- const int MISS_LIMIT = 3;
- public FnBPosVelTrackerF()
- {
- _lastPosValid = false;
- _teleportObserved = false;
- }
- public void update(IMyShipController ctrl)//called every tick
- {
- Vector3D linVel = ctrl.GetShipVelocities().LinearVelocity; // clamped ~300
- Vector3D truePos = ctrl.GetPosition(); // always accurate
- Vector3D fnbVel = linVel;
- ticks++;
- if (_lastPosValid)
- {
- Vector3D deltaPos = truePos - _lastPos;
- Vector3D expected = linVel * DT;
- Vector3D excessTick = deltaPos - expected;
- bool hasExcess = excessTick.LengthSquared() > jitterChk * jitterChk;
- // ---- running histogram: count excess events by tick phase ----
- if (hasExcess)
- _tpCounts[ticks % _cadence]++;
- // decide the teleport phase as the argmax (continuous, self-correcting)
- _tpPhase = 0;
- for (int p = 1; p < _cadence; p++)
- if (_tpCounts[p] > _tpCounts[_tpPhase]) _tpPhase = p;
- bool tpParity = (ticks % _cadence) == _tpPhase;
- if (tpParity)
- {
- // THIS is a teleport tick (by argmax): always anchor to truth;
- // integrate the excess (eat rotation) only when actually present.
- _predPos = truePos;
- if (hasExcess)
- {
- _excess = excessTick * (60.0 / _cadence); // bounded |ω×r|/2
- _teleportObserved = true;
- _misses = 0;
- }
- else
- {
- _misses++; // teleport phase arrived empty -> heading out of FnB
- }
- }
- else
- {
- // non-teleport tick: never integrate; dead-reckon fitted position
- _predPos += fnbVel * DT;
- }
- // structural dropout: teleport phase keeps arriving with no excess
- if (tpParity && _misses > MISS_LIMIT)
- {
- _teleportObserved = false;
- _excess = Vector3D.Zero;
- _predPos = truePos; // hard snap on exit
- }
- }
- else
- {
- _predPos = truePos;
- }
- _lastPos = truePos;
- _lastPosValid = true;
- inFnB = _teleportObserved;
- bool underSafeLimit = linVel.LengthSquared() < safeLimit * safeLimit;
- Position = underSafeLimit ? _lastPos : _predPos;
- Velocity = underSafeLimit ? linVel : (_teleportObserved ? linVel + _excess : linVel);
- Ready = Ready || ticks > 10 || _teleportObserved;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment