summaryrefslogtreecommitdiff
path: root/common/accel-motivity.hpp
blob: f88320b7c2f4d02efaf0112d1c4da8189793546b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once

#include <math.h>

#include "accel-base.hpp"

#define RA_LOOKUP

namespace rawaccel {

	constexpr size_t LUT_SIZE = 601;

	struct si_pair { 
		double slope = 0;
		double intercept = 0; 
	};

	/// <summary> Struct to hold sigmoid (s-shaped) gain implementation. </summary>
	struct motivity_impl {
		double rate;
		double limit;
		double midpoint;
		double subtractive_constant;

		motivity_impl(const accel_args& args) :
			rate(pow(10,args.accel)), limit(2*log10(args.limit)), midpoint(log10(args.midpoint))
		{
			subtractive_constant = limit / 2;
		}

		inline double operator()(double speed) const {
			double log_speed = log10(speed);
			return pow(10, limit / (exp(-rate * (log_speed - midpoint)) + 1) - subtractive_constant);

		}

		inline double legacy_offset(double speed) const { return operator()(speed); }

		inline double apply(si_pair* lookup, double speed) const
		{
			si_pair pair = lookup[map(speed)];
			return pair.slope + pair.intercept / speed;
		}

		inline int map(double speed) const
		{
			int index = speed > 0 ? (int)(100 * log10(speed) + 201) : 0;

			if (index < 0) return 0;
			if (index >= LUT_SIZE) return LUT_SIZE - 1;

			return index;
		}

		inline void fill(si_pair* lookup) const
		{
			double lookup_speed = 0;
			double gain_integral_speed = 0;
			double gain = 0;
			double intercept = 0;
			double output = 0;
			double x = -2;

			lookup[0] = {};

			for (size_t i = 1; i < LUT_SIZE; i++)
			{
				x += 0.01;

				lookup_speed = pow(10, x);

				while (gain_integral_speed < lookup_speed)
				{
					gain_integral_speed += 0.001;
					gain = operator()(gain_integral_speed);
					output += gain * 0.001;
				}

				intercept = output - gain * lookup_speed;

				lookup[i] = { gain, intercept };
			}

		}
	};

	using accel_motivity = nonadditive_accel<motivity_impl>;

}