-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
60 lines (52 loc) · 1.91 KB
/
init.luau
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
--!strict
--!native
--!optimize 2
--- ### CFrame.luau
---
--- extension of CFrame library
local CFrameK = {
--- returns `CFrame.fromOrientation(v.X, v.Y, v.Z)`
anglesfromvector = function(v: Vector3)
return CFrame.fromOrientation(v.X, v.Y, v.Z)
end;
--- returns `Vector3.new(c:ToOrientation())`
vectorfromangles = function(c: CFrame)
return Vector3.new(c:ToOrientation())
end;
}
--- applies selected axis from copyfrom to target \
--- if no axis is passed, then all rotational components will be applied
function CFrameK.applyaxis(target: CFrame | Vector3, copyfrom: CFrame, axis: Enum.Axis?)
if not axis then
return CFrame.new(target.X, target.Y, target.Z, select(4, copyfrom:GetComponents()))
end
local x, y, z = copyfrom:ToOrientation()
local cframe = CFrame.new(target.X, target.Y, target.Z)
return axis == Enum.Axis.X and cframe * CFrame.Angles(x, 0, 0)
or axis == Enum.Axis.Y and cframe * CFrame.Angles(0, y, 0)
or axis == Enum.Axis.Z and cframe * CFrame.Angles(0, 0, z)
or CFrame.identity
end
--- returns cframe rotated towards lookvector at uniform rate within `[0, 1]`
function CFrameK.rotatetolookvector(cframe: CFrame, lookvector: Vector3, rate: number)
local angle = cframe.LookVector:Angle(lookvector)
if angle < 5e-7 then
return cframe
elseif angle > math.pi - 5e-7 then
return cframe * CFrame.Angles(0, math.pi * rate, 0)
else
local axis = cframe.LookVector:Cross(lookvector)
return CFrame.fromAxisAngle(axis, angle * rate) * cframe.Rotation + cframe.Position
end
end
--- affixes numeric or vector3 volume to cframe; equvalent to: \
--- `number`: `cframe * CFrame.new(0, 0, volume * 0.5)` \
--- `Vector3`: `cframe * CFrame.new(0, 0, volume.Z * 0.5)`
function CFrameK.affix(cframe: CFrame, volume: number|Vector3)
if type(volume) == "number" then
return cframe * CFrame.new(0, 0, volume * 0.5)
else
return cframe * CFrame.new(0, 0, volume.Z * 0.5)
end
end
return table.freeze(CFrameK)