Rise
The Vieneo Province
Loading...
Searching...
No Matches
HeadAcceleration.h
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3
5{
6 constexpr float MaximumForceG = 16.f;
7 constexpr float MaximumPitchRadians = .785398163f; // 45 degrees
8 constexpr float MaximumRollRadians = .610865238f; // 35 degrees
9
10 inline float Bound(float value, float limit)
11 {
12 if (!std::isfinite(value)) return 0.f;
13 return value > limit ? limit : value < -limit ? -limit : value;
14 }
15
16 inline float Blend(float dt, float rate)
17 {
18 if (!std::isfinite(dt) || dt <= 0) return 0.f;
19 return dt >= 1.f / rate ? 1.f : dt * rate;
20 }
21
22 inline float AdvanceCounter(float previous, float force, float dt)
23 {
24 const float start = Bound(previous, MaximumForceG);
25 const float recovery = std::fabs(start) > 3.f ? std::fabs(start) / 3.f : 1.f;
26 return Bound(start + (Bound(force, MaximumForceG) - start) * Blend(dt, 3.f / recovery), MaximumForceG);
27 }
28
29 inline float AdvanceLean(float previous, float target, float dt, float limit)
30 {
31 const float start = Bound(previous, limit);
32 return Bound(start + (Bound(target, limit) - start) * Blend(dt, 1.f), limit);
33 }
34
35 // Head effects already saturate at +/-16 G. A larger relocation impulse
36 // must settle before it can enter the slowly recovering compensation.
37 inline bool SettledForces(float x, float y, float z)
38 {
39 return std::isfinite(x) && std::isfinite(y) && std::isfinite(z) &&
40 std::fabs(x) <= MaximumForceG && std::fabs(y) <= MaximumForceG && std::fabs(z) <= MaximumForceG;
41 }
42
44 {
45 float remaining = 0;
46 bool active = false, waitForTerrain = false;
47
48 public:
49 void Reset(bool terrainRequired)
50 {
51 remaining = 5.f;
52 active = true;
53 waitForTerrain = terrainRequired;
54 }
55
56 bool Active() const { return active; }
57
58 bool Suppress(float dt, bool terrainReady, float x, float y, float z)
59 {
60 if (!active) return false;
61 if (std::isfinite(dt) && dt > 0)
62 remaining -= dt > 1.f ? 1.f : dt;
63 if (remaining > 0 || (waitForTerrain && !terrainReady) || !SettledForces(x, y, z))
64 return true;
65 active = false;
66 return false;
67 }
68 };
69}
void Reset(bool terrainRequired)
bool Suppress(float dt, bool terrainReady, float x, float y, float z)
constexpr float MaximumForceG
float Bound(float value, float limit)
constexpr float MaximumPitchRadians
bool SettledForces(float x, float y, float z)
float Blend(float dt, float rate)
float AdvanceCounter(float previous, float force, float dt)
constexpr float MaximumRollRadians
float AdvanceLean(float previous, float target, float dt, float limit)