Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Vector = {}
- Vector.__index = Vector
- local NormalId = Enum and Enum.NormalId
- local Axis = Enum and Enum.Axis
- if not Enum then
- Enum = {}
- Enum.NormalId = {
- Right = 0,
- Top = 1,
- Back = 2,
- Left = 3,
- Bottom = 4,
- Front = 5
- }
- Enum.Axis = Enum.NormalId
- Enum.Axis.X = 0
- Enum.Axis.Y = 1
- Enum.Axis.Z = 2
- NormalId = Enum.NormalId
- Axis = Enum.Axis
- end
- function Vector.new(x, y, z)
- x = x or 0
- y = y or 0
- z = z or 0
- local self = nil
- self = setmetatable({}, {
- __index = function(_, value)
- if value == "Unit" then
- self.Unit = setmetatable(self:Normalize(), {
- __tostring = function()
- local unitPosition = self.unit
- return ("%s, %s, %s"):format(unitPosition.X, unitPosition.Y, unitPosition.Z)
- end,
- })
- return self.Unit
- end
- if Vector[value] then
- return Vector[value]
- end
- end,
- __tostring = function(pos)
- return ("%s, %s, %s"):format(pos.X, pos.Y, pos.Z)
- end,
- __add = function(a, b)
- return Vector.new(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
- end,
- __sub = function(a, b)
- return Vector.new(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
- end,
- __mul = function(a, b)
- if type(a) == "number" then
- return Vector.new(a * b.X, a * b.Y, a * b.Z)
- elseif type(b) == "number" then
- return Vector.new(a.X * b, a.X * b, a.X * b)
- end
- return Vector.new(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
- end,
- __div = function(a, b)
- if type(a) == "number" then
- return Vector.new(a / b.X, a / b.Y, a / b.Z)
- elseif type(b) == "number" then
- return Vector.new(a.X / b, a.X / b, a.X / b)
- end
- return Vector.new(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
- end
- })
- self.X = x
- self.Y = y
- self.Z = z
- self.Magnitude = self:Magnitude()
- return self
- end
- function Vector:Normalize()
- local m = self:Magnitude()
- local nx = self.X / m
- local ny = self.Y / m
- local nz = self.Z / m
- return Vector.new(nx, ny, nz)
- end
- function Vector:Magnitude()
- return math.sqrt(self:Dot(self, self))
- end
- function Vector:Dot(a, b)
- if self then
- return self.X * a.X + self.Y * a.Y + self.Z * a.Z
- end
- return a.X * b.X + a.Y * b.Y + a.Z * b.Z
- end
- function Vector:Cross(a, b)
- if self then
- return Vector.new(
- self.Y * a.Z - a.Y * self.Z,
- self.Z * a.X - a.Z * self.X,
- self.X * a.Y - a.X * self.Y
- )
- end
- return Vector.new(
- a.Y * b.Z - b.Y * a.Z,
- a.Z * b.X - b.Z * a.X,
- a.X * b.Y - b.X * a.Y
- )
- end
- function Vector:_Lerp(a, b, t)
- return a * (1 - t) + b * t
- end
- function Vector:Lerp(goal, alpha)
- local x = self:_Lerp(self.X, goal.X, alpha)
- local y = self:_Lerp(self.Y, goal.Y, alpha)
- local z = self:_Lerp(self.Z, goal.Z, alpha)
- return Vector.new(x, y, z)
- end
- function Vector:FuzzyEq(other, epsilon)
- return (self.Magnitude - other.Magnitude) <= (epsilon or 1e-5)
- end
- function Vector.FromNormalId(Face)
- if Face == NormalId.Right then
- return Vector.new(1, 0, 0)
- elseif Face == NormalId.Left then
- return Vector.new(-1, 0, 0)
- elseif Face == NormalId.Top then
- return Vector.new(0, 1, 0)
- elseif Face == NormalId.Bottom then
- return Vector.new(0, -1, 0)
- elseif Face == NormalId.Back then
- return Vector.new(0, 0, 1)
- elseif Face == NormalId.Front then
- return Vector.new(0, 0, -1)
- end
- end
- function Vector.FromAxis(axis)
- if axis == Axis.X then
- return Vector.new(1, 0, 0)
- elseif axis == Axis.Y then
- return Vector.new(0, 1, 0)
- elseif axis == Axis.Z then
- return Vector.new(0, 0, 1)
- elseif axis == Axis.Right then
- return Vector.new(1, 0, 0)
- elseif axis == Axis.Left then
- return Vector.new(-1, 0, 0)
- elseif axis == Axis.Top then
- return Vector.new(0, 1, 0)
- elseif axis == Axis.Bottom then
- return Vector.new(0, -1, 0)
- elseif axis == Axis.Back then
- return Vector.new(0, 0, 1)
- elseif axis == Axis.Front then
- return Vector.new(0, 0, -1)
- end
- end
- function Vector:Real()
- if Vector3 then
- return Vector3.new(self.X, self.Y, self.Z)
- else
- return self
- end
- end
- return Vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement