Advertisement
Vzurxy

vector class lua

Aug 23rd, 2021 (edited)
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. local Vector = {}
  2. Vector.__index = Vector
  3.  
  4. local NormalId = Enum and Enum.NormalId
  5. local Axis = Enum and Enum.Axis
  6.  
  7. if not Enum then
  8.     Enum = {}
  9.     Enum.NormalId = {
  10.         Right = 0,
  11.         Top = 1,
  12.         Back = 2,
  13.         Left = 3,
  14.         Bottom = 4,
  15.         Front = 5
  16.     }
  17.     Enum.Axis = Enum.NormalId
  18.     Enum.Axis.X = 0
  19.     Enum.Axis.Y = 1
  20.     Enum.Axis.Z = 2
  21.  
  22.     NormalId = Enum.NormalId
  23.     Axis = Enum.Axis
  24. end
  25.  
  26. function Vector.new(x, y, z)
  27.     x = x or 0
  28.     y = y or 0
  29.     z = z or 0
  30.  
  31.     local self = nil
  32.     self = setmetatable({}, {
  33.         __index = function(_, value)
  34.             if value == "Unit" then
  35.                 self.Unit = setmetatable(self:Normalize(), {
  36.                     __tostring = function()
  37.                         local unitPosition = self.unit
  38.                         return ("%s, %s, %s"):format(unitPosition.X, unitPosition.Y, unitPosition.Z)
  39.                     end,
  40.                 })
  41.  
  42.                 return self.Unit
  43.             end
  44.             if Vector[value] then
  45.                 return Vector[value]
  46.             end
  47.         end,
  48.         __tostring = function(pos)
  49.             return ("%s, %s, %s"):format(pos.X, pos.Y, pos.Z)
  50.         end,
  51.         __add = function(a, b)
  52.             return Vector.new(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
  53.         end,
  54.         __sub = function(a, b)
  55.             return Vector.new(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
  56.         end,
  57.         __mul = function(a, b)
  58.             if type(a) == "number" then
  59.                 return Vector.new(a * b.X, a * b.Y, a * b.Z)
  60.             elseif type(b) == "number" then
  61.                 return Vector.new(a.X * b, a.X * b, a.X * b)
  62.             end
  63.             return Vector.new(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
  64.         end,
  65.         __div = function(a, b)
  66.             if type(a) == "number" then
  67.                 return Vector.new(a / b.X, a / b.Y, a / b.Z)
  68.             elseif type(b) == "number" then
  69.                 return Vector.new(a.X / b, a.X / b, a.X / b)
  70.             end
  71.             return Vector.new(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
  72.         end
  73.     })
  74.  
  75.     self.X = x
  76.     self.Y = y
  77.     self.Z = z
  78.  
  79.     self.Magnitude = self:Magnitude()
  80.  
  81.     return self
  82. end
  83.  
  84. function Vector:Normalize()
  85.     local m = self:Magnitude()
  86.     local nx = self.X / m
  87.     local ny = self.Y / m
  88.     local nz = self.Z / m
  89.  
  90.     return Vector.new(nx, ny, nz)
  91. end
  92.  
  93. function Vector:Magnitude()
  94.     return math.sqrt(self:Dot(self, self))
  95. end
  96.  
  97. function Vector:Dot(a, b)
  98.     if self then
  99.         return self.X * a.X + self.Y * a.Y + self.Z * a.Z
  100.     end
  101.  
  102.     return a.X * b.X + a.Y * b.Y + a.Z * b.Z
  103. end
  104.  
  105. function Vector:Cross(a, b)
  106.     if self then
  107.         return Vector.new(
  108.             self.Y * a.Z - a.Y * self.Z,
  109.             self.Z * a.X - a.Z * self.X,
  110.             self.X * a.Y - a.X * self.Y
  111.         )
  112.     end
  113.  
  114.     return Vector.new(
  115.         a.Y * b.Z - b.Y * a.Z,
  116.         a.Z * b.X - b.Z * a.X,
  117.         a.X * b.Y - b.X * a.Y
  118.     )
  119. end
  120.  
  121. function Vector:_Lerp(a, b, t)
  122.     return a * (1 - t) + b * t
  123. end
  124.  
  125. function Vector:Lerp(goal, alpha)
  126.     local x = self:_Lerp(self.X, goal.X, alpha)
  127.     local y = self:_Lerp(self.Y, goal.Y, alpha)
  128.     local z = self:_Lerp(self.Z, goal.Z, alpha)
  129.  
  130.     return Vector.new(x, y, z)
  131. end
  132.  
  133. function Vector:FuzzyEq(other, epsilon)
  134.     return (self.Magnitude - other.Magnitude) <= (epsilon or 1e-5)
  135. end
  136.  
  137. function Vector.FromNormalId(Face)
  138.     if Face == NormalId.Right then
  139.         return Vector.new(1, 0, 0)
  140.     elseif Face == NormalId.Left then
  141.         return Vector.new(-1, 0, 0)
  142.     elseif Face == NormalId.Top then
  143.         return Vector.new(0, 1, 0)
  144.     elseif Face == NormalId.Bottom then
  145.         return Vector.new(0, -1, 0)
  146.     elseif Face == NormalId.Back then
  147.         return Vector.new(0, 0, 1)
  148.     elseif Face == NormalId.Front then
  149.         return Vector.new(0, 0, -1)
  150.     end
  151. end
  152.  
  153. function Vector.FromAxis(axis)
  154.     if axis == Axis.X then
  155.         return Vector.new(1, 0, 0)
  156.     elseif axis == Axis.Y then
  157.         return Vector.new(0, 1, 0)
  158.     elseif axis == Axis.Z then
  159.         return Vector.new(0, 0, 1)
  160.     elseif axis == Axis.Right then
  161.         return Vector.new(1, 0, 0)
  162.     elseif axis == Axis.Left then
  163.         return Vector.new(-1, 0, 0)
  164.     elseif axis == Axis.Top then
  165.         return Vector.new(0, 1, 0)
  166.     elseif axis == Axis.Bottom then
  167.         return Vector.new(0, -1, 0)
  168.     elseif axis == Axis.Back then
  169.         return Vector.new(0, 0, 1)
  170.     elseif axis == Axis.Front then
  171.         return Vector.new(0, 0, -1)
  172.     end
  173. end
  174.  
  175. function Vector:Real()
  176.     if Vector3 then
  177.         return Vector3.new(self.X, self.Y, self.Z)
  178.     else
  179.         return self
  180.     end
  181. end
  182.  
  183. return Vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement