fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799Added build files and stuffeefe2ce

GUI
4.0 KiB161 linesraw
1local VECTOR = {}
2VECTOR.__index = VECTOR
3
4function isvector(vec)
5	return vec and type(vec) == "table" and vec.x and vec.y and vec.z
6end
7
8function vector(x, y, z)
9	return setmetatable({ x = x or 0, y = y or 0, z = z or 0 }, VECTOR)
10end
11
12function VECTOR:__tostring()
13	return ("[vector][%d, %d, %d]"):format(self.x, self.y, self.z)
14end
15
16function VECTOR:__add(vec)
17	return vector(self.x + vec.x, self.y + vec.y, self.z + vec.z)
18end
19
20function VECTOR:__div(num)
21	return vector(self.x / num, self.y / num, self.z / num)
22end
23
24function VECTOR:__eq(vec)
25	return self.x == vec.x and self.y == vec.y and self.z == vec.z
26end
27
28function VECTOR:__mul(vec) --Multiply a vector by a number, or multiply a vector by a vector
29	if isvector(self) and isvector(vec) then
30		return vector(self.x * vec.x, self.y * vec.y, self.z * vec.z)
31	elseif type(self) == "number" then
32		return vector(self * vec.x, self * vec.y, self * vec.z)
33	else
34		return vector(self.x * vec, self.y * vec, self.z * vec)
35	end
36end
37
38function VECTOR:__sub(vec)
39    return vector(self.x - vec.x, self.y - vec.y,  self.z - vec.z)
40end
41
42function VECTOR:Cross(vec)
43    return vector(self.y * vec.z - self.z * vec.y, self.z * vec.x - self.x * vec.z, self.x * vec.y - self.y * vec.x)
44end
45
46function VECTOR:Distance(vec) --The distance between two vectors
47	return (self - vec):Length()
48end
49
50function VECTOR:DistToSqr(vec)
51	return (self.x - vec.x)^2 + (self.y - vec.y)^2 + (self.z - vec.z)^2
52end
53
54function VECTOR:Dot(vec) --The dot product of two vectors
55	return vec.x * self.x + vec.y * self.y + vec.z * self.z
56end
57
58function VECTOR:DotProduct(vec) --Duplicate of Dot
59	return vec.x * self.x + vec.y * self.y + vec.z * self.z
60end
61
62function VECTOR:GetNormalized() --Gets the normal of two vectors
63	local length = self:Length()
64	return vector(self.x / length, self.y / length, self.z / length)
65end
66
67function VECTOR:GetNormal() --Duplicate of GetNormalized
68	local length = self:Length()
69	return vector(self.x / length, self.y / length, self.z / length)
70end
71
72function VECTOR:WithinAABox(mins, maxs)
73	if self.x < mins.x or self.y < mins.y or self.z < mins.z or self.x > maxs.x or self.y > maxs.y or self.z > maxs.z then
74		return false
75	end
76	return true
77end
78
79--[[function VECTOR:IsEqualTol(vec, tol)
80	
81end]]
82
83function VECTOR:IsZero(vec)
84	return vec.x == 0 and vec.y == 0 and vec.z == 0
85end
86
87function VECTOR:Length2D()
88	return math.sqrt(self.x * self.x + self.y * self.y)
89end
90
91function VECTOR:Length() --The length of a vector
92	return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
93end
94
95function VECTOR:Normalize() --Directly modifies the vector you pass it
96	local length = self:Length()
97	self.x = self.x / length
98	self.y = self.y / length
99	self.z = self.z / length
100end
101
102--[[function VECTOR:Rotate(ang) --Rotate a vector by degrees
103	ang = math.rad(ang)
104	return vector(self.x * math.cos(ang) - self.y * math.sin(ang), self.x * math.sin(ang) + self.y * math.cos(ang))
105end]]
106
107function VECTOR:Add(vec)
108	self.x = self.x + vec.x
109	self.y = self.y + vec.y
110	self.z = self.z + vec.z
111end
112
113function VECTOR:Set(vec)
114	self.x = vec.x
115	self.y = vec.y
116	self.z = vec.z
117end
118
119function VECTOR:Mul(scale)
120	self.x = self.x * scale
121	self.y = self.y * scale
122	self.z = self.z * scale
123end
124
125function VECTOR:Zero() --Not sure why this would ever be needed, but hey who cares.. (taken from the garrysmod library)
126	self.x = 0
127	self.y = 0
128	self.z = 0
129end
130
131function VECTOR:RayQuadIntersect(vDirection, vPlane, vX, vY)
132	local vp = vDirection:Cross(vY)
133
134	local d = vX:DotProduct(vp)
135
136	if (d <= 0.0) then return end
137
138	local vt = self - vPlane
139	local u = vt:DotProduct(vp)
140	if (u < 0.0 or u > d) then return end
141
142	local v = vDirection:DotProduct(vt:Cross(vX))
143	if (v < 0.0 or v > d) then return end
144
145	return vector(u / d, v / d, 0)
146end
147
148function VECTOR:IsInFront(vec, normal)
149	return (normal:Dot((vec - self):GetNormalized()) < 0)
150end
151
152function VECTOR:Clamp(min, max)
153	self.x = math.Clamp(self.x, min, max)
154	self.y = math.Clamp(self.y, min, max)
155	self.z = math.Clamp(self.z, min, max)
156	return self
157end
158
159function VECTOR:Project(vec)
160	return self:Dot(vec) / vec:Dot(vec) * vec
161end