-
Notifications
You must be signed in to change notification settings - Fork 8
/
NanosMath.lua
123 lines (86 loc) · 3.18 KB
/
NanosMath.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
123
--[[ Nanos Math Library --]]
NanosMath = {}
function NanosMath.Round(value, decimals)
local decimal_shift = (10 ^ (decimals or 0))
return (math.floor((value * decimal_shift) + 0.5) / decimal_shift)
end
function NanosMath.Clamp(value, low, high)
return math.min(math.max(value, low), high)
end
function NanosMath.ClampAxis(angle)
local new_angle = angle % 360
if (new_angle < 0) then
new_angle = new_angle + 360
end
return new_angle
end
function NanosMath.NormalizeAxis(angle)
local new_angle = NanosMath.ClampAxis(angle)
if (new_angle > 180) then
new_angle = new_angle - 360
end
return new_angle
end
function NanosMath.FInterpTo(current, target, delta_time, interp_speed)
if (interp_speed <= 0) then return target end
local delta = target - current
if (math.sqrt(delta) < 0.00001) then return target end
local delta_move = delta * NanosMath.Clamp(delta_time * interp_speed, 0, 1)
return current + delta_move
end
function NanosMath.RInterpTo(current, target, delta_time, interp_speed)
if (interp_speed <= 0) then return target end
local delta = (target - current):GetNormalized()
if (delta:IsNearlyZero()) then return target end
local delta_move = delta * NanosMath.Clamp(delta_time * interp_speed, 0, 1)
return (current + delta_move):GetNormalized()
end
function NanosMath.VInterpTo(current, target, delta_time, interp_speed)
if (interp_speed <= 0) then return target end
local delta = target - current
if (delta:IsNearlyZero()) then return target end
local delta_move = delta * NanosMath.Clamp(delta_time * interp_speed, 0, 1)
return current + delta_move
end
function NanosMath.VInterpConstantTo(current, target, delta_time, interp_speed)
local delta = target - current
local delta_m = delta:Size()
local max_step = interp_speed * delta_time
if (delta_m > max_step) then
if (max_step > 0) then
local delta_n = delta / delta_m
return current + delta_n * max_step
end
else
return current
end
return target
end
function NanosMath.RInterpConstantTo(current, target, delta_time, interp_speed)
if (delta_time == 0 or current == target) then return current end
if (interp_speed <= 0) then return target end
local delta_interp_speed = interp_speed * delta_time
local delta_move = (target - current):GetNormalized()
local result = Rotator(
NanosMath.Clamp(delta_move.Pitch, -delta_interp_speed, delta_interp_speed),
NanosMath.Clamp(delta_move.Yaw, -delta_interp_speed, delta_interp_speed),
NanosMath.Clamp(delta_move.Roll, -delta_interp_speed, delta_interp_speed)
)
result:Normalize()
return result
end
function NanosMath.RelativeTo(location, rotation, actor)
local actor_location = actor:GetLocation()
local actor_rotation = actor:GetRotation()
local relative_location = actor_rotation:RotateVector(location - actor_location) / actor:GetScale()
-- Converts the Rotations to Quaterions
local quaternion = rotation:Quaternion()
local quaternion_attached = actor_rotation:Quaternion()
-- Gets the Relative rotation
local inverse = quaternion_attached:Inverse()
local relative_rotation = (inverse * quaternion):Rotator()
return relative_location, relative_rotation
end
function NanosMath.RandomFloat(min, max)
return min + math.random() * (max - min)
end