Prexxo

Vector2 Class

Jun 30th, 2021 (edited)
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. require "Utility"
  2.  
  3. --// Functions
  4.  
  5. --// Get Both Axes
  6. local function GetAxis(Vector)
  7.     return Vector.X, Vector.Y
  8. end
  9.  
  10. --// Get the length of the vector
  11. local function GetMagnitude(Vector)
  12.     return (Vector.X^2 + Vector.Y^2)^.5
  13. end
  14.  
  15. --// Get Vector's direction
  16. local function ToUnit(Vector)
  17.     return Vector / Vector:GetMagnitude()
  18. end
  19.  
  20. --// Linear interpolation between 2 vectors
  21. local function Lerp(A, B, Alpha)
  22.     return A + (B-A) * Vector2.new(Alpha, Alpha) --// can be optimized by adding Vector * Number or manually creating it
  23. end
  24.  
  25. --// Get the dot product of Vector's A and B
  26. local function Dot(A, B)
  27.     return A.X * B.X + A.Y * B.Y
  28. end
  29.  
  30. --// Get the Vector between A and B
  31. local function GetAngle(A, B)
  32.     return math.atan2(B.Y - A.Y, B.X - A.X)
  33. end
  34.  
  35. --// Meta Methods
  36. local Vector2MetaMethods = {}
  37.  
  38.  
  39. function Vector2MetaMethods.__add(A, B)
  40.     if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
  41.         error("Vector2 | Both must be Vectors")
  42.         return
  43.     end
  44.  
  45.     return Vector2.new(
  46.         A.X + B.X,
  47.         A.Y + B.Y
  48.     )
  49. end
  50.  
  51. function Vector2MetaMethods.__sub(A, B)
  52.     if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
  53.         error("Vector2 | Both must be Vectors")
  54.         return
  55.     end
  56.  
  57.     return Vector2.new(
  58.         A.X - B.X,
  59.         A.Y - B.Y
  60.     )
  61. end
  62.  
  63. function Vector2MetaMethods.__mul(A, B)
  64.     if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
  65.         error("Vector2 | Both must be Vectors")
  66.         return
  67.     end
  68.  
  69.     return Vector2.new(
  70.         A.X * B.X,
  71.         A.Y * B.Y
  72.     )
  73. end
  74.  
  75. function Vector2MetaMethods.__div(A, B)
  76.     if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
  77.         error("Vector2 | Both must be Vectors")
  78.         return
  79.     end
  80.  
  81.     return Vector2.new(
  82.         A.X / B.X,
  83.         A.Y / B.Y
  84.     )
  85. end
  86.  
  87. Vector2 = {}
  88.  
  89. function Vector2.new(X, Y)
  90.     return setmetatable(
  91.         {
  92.             --// Properties
  93.             Type = "Vector2",
  94.  
  95.             X = X or 0,
  96.             Y = Y or 0,
  97.  
  98.             --// Methods
  99.             GetAxis = GetAxis,
  100.             GetMagnitude = GetMagnitude,
  101.             GetLength = GetLength,
  102.             ToUnit = ToUnit,
  103.             Dot = Dot,
  104.             Lerp = Lerp,
  105.             GetAngle = GetAngle,
  106.         },
  107.         Vector2MetaMethods
  108.     )
  109. end
  110.  
Add Comment
Please, Sign In to add comment