-
Notifications
You must be signed in to change notification settings - Fork 116
/
MathUtil.lua
122 lines (95 loc) · 2.55 KB
/
MathUtil.lua
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
---------------
--NOTE - Please do not change this section without understanding the full implications of the secure environment
local _, tbl = ...;
if tbl then
tbl.SecureCapsuleGet = SecureCapsuleGet;
local function Import(name)
tbl[name] = tbl.SecureCapsuleGet(name);
end
Import("IsOnGlueScreen");
if ( tbl.IsOnGlueScreen() ) then
tbl._G = _G; --Allow us to explicitly access the global environment at the glue screens
end
setfenv(1, tbl);
Import("math");
Import("GetTickTime");
end
----------------
MathUtil =
{
Epsilon = .000001;
};
function Lerp(startValue, endValue, amount)
return (1 - amount) * startValue + amount * endValue;
end
function Clamp(value, min, max)
if value > max then
return max;
elseif value < min then
return min;
end
return value;
end
function Saturate(value)
return Clamp(value, 0.0, 1.0);
end
function Wrap(value, max)
return (value - 1) % max + 1;
end
function ClampDegrees(value)
return ClampMod(value, 360);
end
function ClampMod(value, mod)
return ((value % mod) + mod) % mod;
end
function NegateIf(value, condition)
return condition and -value or value;
end
function PercentageBetween(value, startValue, endValue)
if startValue == endValue then
return 0.0;
end
return (value - startValue) / (endValue - startValue);
end
function ClampedPercentageBetween(value, startValue, endValue)
return Saturate(PercentageBetween(value, startValue, endValue));
end
local TARGET_FRAME_PER_SEC = 60.0;
function DeltaLerp(startValue, endValue, amount, timeSec)
return Lerp(startValue, endValue, Saturate(amount * timeSec * TARGET_FRAME_PER_SEC));
end
function FrameDeltaLerp(startValue, endValue, amount)
return DeltaLerp(startValue, endValue, amount, GetTickTime());
end
function RandomFloatInRange(minValue, maxValue)
return Lerp(minValue, maxValue, math.random());
end
function Round(value)
if value < 0.0 then
return math.ceil(value - .5);
end
return math.floor(value + .5);
end
function Square(value)
return value * value;
end
function WithinRange(value, min, max)
return value >= min and value <= max;
end
function WithinRangeExclusive(value, min, max)
return value > min and value < max;
end
function ApproximatelyEqual(v1, v2, epsilon)
return math.abs(v1 - v2) < (epsilon or MathUtil.Epsilon);
end
function CalculateDistanceSq(x1, y1, x2, y2)
local dx = x2 - x1;
local dy = y2 - y1;
return (dx * dx) + (dy * dy);
end
function CalculateDistance(x1, y1, x2, y2)
return math.sqrt(CalculateDistanceSq(x1, y1, x2, y2));
end
function CalculateAngleBetween(x1, y1, x2, y2)
return math.atan2(y2 - y1, x2 - x1);
end