summaryrefslogtreecommitdiff
path: root/common/accel-jump.hpp
blob: e3d798e8c55e728d45d307ad9a3e8e5a7d803574 (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
#pragma once

#include "rawaccel-base.hpp"

namespace rawaccel {

	struct jump_base {
		static constexpr double smooth_scale = 2 * M_PI;

		vec2d step;
		double smooth_rate;

		// requirements: args.smooth in range [0, 1]
		jump_base(const accel_args& args) :
			step({ args.cap.x, args.cap.y - 1 })
		{
			double rate_inverse = args.smooth * step.x;

			if (rate_inverse < 1) {
				smooth_rate = 0;
			}
			else {
				smooth_rate = smooth_scale / rate_inverse;
			}
		}

		bool is_smooth() const
		{
			return smooth_rate != 0;
		}

		double decay(double x) const
		{
			return exp(smooth_rate * (step.x - x));
		}

		double smooth(double x) const
		{
			return step.y / (1 + decay(x));
		}

		double smooth_antideriv(double x) const
		{
			return step.y * (x + log(1 + decay(x)) / smooth_rate);
		}

	};

	template <bool Gain> struct jump;

	template<>
	struct jump<LEGACY> : jump_base {
		using jump_base::jump_base;

		double operator()(double x, const accel_args&) const
		{
			if (is_smooth()) return smooth(x) + 1;
			else if (x < step.x) return 1;
			else return 1 + step.y;
		}
	};

	template<>
	struct jump<GAIN> : jump_base {
		double C;

		jump(const accel_args& args) :
			jump_base(args),
			C(-smooth_antideriv(0)) {}

		double operator()(double x, const accel_args&) const
		{
			if (x <= 0) return 1;

			if (is_smooth()) return 1 + (smooth_antideriv(x) + C) / x;

			if (x < step.x) return 1;
			else return 1 + step.y * (x - step.x) / x;
		}

	};

}