-
Notifications
You must be signed in to change notification settings - Fork 3
/
Interpolator.angelscript
133 lines (112 loc) · 2.67 KB
/
Interpolator.angelscript
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
funcdef float INTERPOLATION_FILTER(const float v);
float defaultFilter(const float v) { return v; }
float smoothEnd(const float v) { return sin(v * (PIb)); }
float smoothBeginning(const float v) { return (1.0f - smoothEnd(1.0f - v)); }
float smoothBothSides(const float v) { return smoothBeginning(smoothEnd(v)); }
float elastic(const float n)
{
if (n == 0.0f || n == 1.0f)
{
return n;
}
double p = 0.3f, s = p / 4.0f;
return pow(2.0f, -10.0f * n) * sin((n - s) * (2 * PI) / p) + 1;
}
float interpolate(const float a, const float b, const float bias)
{
return a + ((b - a) * bias);
}
vector2 interpolate(const vector2 &in a, const vector2 &in b, const float bias)
{
return a + ((b - a) * bias);
}
vector3 interpolate(const vector3 &in a, const vector3 &in b, const float bias)
{
return a + ((b - a) * bias);
}
FloatColor interpolate(const FloatColor@ a, const FloatColor@ b, const float bias)
{
return FloatColor(interpolate(a.getAlpha(), b.getAlpha(), bias),
interpolate(a.getVector3(), b.getVector3(), bias));
}
class InterpolationTimer
{
InterpolationTimer(const uint _millisecs, const bool dontPause)
{
reset(_millisecs);
@m_filter = @smoothEnd;
m_dontPause = dontPause;
}
void update()
{
if (m_dontPause)
m_elapsedTime += GetLastFrameElapsedTime();
else
m_elapsedTime += g_timeManager.getLastFrameElapsedTime();
}
float getBias() const
{
return (!isOver()) ? m_filter(min(max(float(m_elapsedTime) / float(m_time), 0.0f), 1.0f)) : 1.0f;
}
float getUnfilteredBias() const
{
return (!isOver()) ? (min(max(float(m_elapsedTime) / float(m_time), 0.0f), 1.0f)) : 1.0f;
}
void reset(const uint _millisecs)
{
m_time = _millisecs;
m_elapsedTime = 0;
}
bool isOver() const
{
return (m_elapsedTime > m_time);
}
void setFilter(INTERPOLATION_FILTER@ filter)
{
@m_filter = @filter;
}
uint m_elapsedTime;
uint m_time;
INTERPOLATION_FILTER@ m_filter;
bool m_dontPause;
}
class PositionInterpolator : InterpolationTimer
{
private vector2 m_a;
private vector2 m_b;
PositionInterpolator()
{
super(0, false);
}
PositionInterpolator(const vector2 &in _a, const vector2 &in _b, const uint _millisecs, const bool dontPause = false)
{
super(_millisecs, dontPause);
reset(_a, _b, _millisecs);
}
void reset(const vector2 &in _a, const vector2 &in _b, const uint _millisecs)
{
InterpolationTimer::reset(_millisecs);
m_a = _a;
m_b = _b;
}
void forcePointA(const vector2 &in a)
{
m_a = a;
}
void forcePointB(const vector2 &in b)
{
m_b = b;
}
vector2 getCurrentPos() const
{
if (m_elapsedTime > m_time)
{
return m_b;
}
else
{
const float bias = getBias();
return interpolate(m_a, m_b, bias);
}
}
}