Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "Utility"
- --// Functions
- --// Get Both Axes
- local function GetAxis(Vector)
- return Vector.X, Vector.Y
- end
- --// Get the length of the vector
- local function GetMagnitude(Vector)
- return (Vector.X^2 + Vector.Y^2)^.5
- end
- --// Get Vector's direction
- local function ToUnit(Vector)
- return Vector / Vector:GetMagnitude()
- end
- --// Linear interpolation between 2 vectors
- local function Lerp(A, B, Alpha)
- return A + (B-A) * Vector2.new(Alpha, Alpha) --// can be optimized by adding Vector * Number or manually creating it
- end
- --// Get the dot product of Vector's A and B
- local function Dot(A, B)
- return A.X * B.X + A.Y * B.Y
- end
- --// Get the Vector between A and B
- local function GetAngle(A, B)
- return math.atan2(B.Y - A.Y, B.X - A.X)
- end
- --// Meta Methods
- local Vector2MetaMethods = {}
- function Vector2MetaMethods.__add(A, B)
- if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
- error("Vector2 | Both must be Vectors")
- return
- end
- return Vector2.new(
- A.X + B.X,
- A.Y + B.Y
- )
- end
- function Vector2MetaMethods.__sub(A, B)
- if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
- error("Vector2 | Both must be Vectors")
- return
- end
- return Vector2.new(
- A.X - B.X,
- A.Y - B.Y
- )
- end
- function Vector2MetaMethods.__mul(A, B)
- if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
- error("Vector2 | Both must be Vectors")
- return
- end
- return Vector2.new(
- A.X * B.X,
- A.Y * B.Y
- )
- end
- function Vector2MetaMethods.__div(A, B)
- if not Utility.GetType(A) == "Vector2" or not Utility.GetType(B) == "Vector2" then
- error("Vector2 | Both must be Vectors")
- return
- end
- return Vector2.new(
- A.X / B.X,
- A.Y / B.Y
- )
- end
- Vector2 = {}
- function Vector2.new(X, Y)
- return setmetatable(
- {
- --// Properties
- Type = "Vector2",
- X = X or 0,
- Y = Y or 0,
- --// Methods
- GetAxis = GetAxis,
- GetMagnitude = GetMagnitude,
- GetLength = GetLength,
- ToUnit = ToUnit,
- Dot = Dot,
- Lerp = Lerp,
- GetAngle = GetAngle,
- },
- Vector2MetaMethods
- )
- end
Add Comment
Please, Sign In to add comment