Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Vector3 = {}
- local mt = {__index = Vector3}
- function Vector3.new(x, y, z)
- local self = setmetatable({}, mt)
- self.coords = {}
- self.coords.x = x or 0
- self.coords.y = y or 0
- self.coords.z = z or 0
- self.magnitude = (self.coords.x^2+self.coords.y^2+self.coords.z^2)^0.5
- return self
- end
- function mt.__div(a, b)
- local at, bt = type(a), type(b)
- if at == 'table' and bt == 'table' then
- return Vector3.new(a.x / b.x, a.y / b.y, a.z / b.z)
- elseif at == 'table' and bt == 'number' then
- return Vector3.new(a.x / b, a.y / b, a.z / b)
- elseif at == 'number' and bt == 'table' then
- return Vector3.new(a / b.x, a / b.y, a / b.z)
- end
- end
- function mt.__mul(a, b)
- local at, bt = type(a), type(b)
- if at == 'table' and bt == 'table' then
- return Vector3.new(a.x * b.x, a.y * b.y, a.z * b.z)
- elseif at == 'table' and bt == 'number' then
- return Vector3.new(a.x * b, a.y * b, a.z * b)
- elseif at == 'number' and bt == 'table' then
- return Vector3.new(a * b.x, a * b.y, a * b.z)
- end
- end
- function mt.__add(a, b)
- return Vector3.new(a.x + b.x, a.y + b.y, a.z + b.z)
- end
- function mt.__sub(a, b)
- return Vector3.new(a.x - b.x, a.y - b.y, a.z - b.z)
- end
- function mt:__unm()
- return Vector3.new(-self.x, -self.y, -self.z)
- end
- function mt:__eq(v)
- return ((self.x == v.x) and (self.y == v.y) and (self.z == v.z))
- end
- function mt:__lt(v)
- local isTable = (type(v) == 'table')
- if isTable then
- return (self.magnitude < v.magnitude)
- else
- return (self.magnitude < v)
- end
- end
- function mt:__le(v)
- local isTable = (type(v) == 'table')
- if isTable then
- return (self.magnitude <= v.magnitude)
- else
- return (self.magnitude <= v)
- end
- end
- function mt:__index(i)
- if rawget(self.coords, i:lower()) then
- return rawget(self.coords, i:lower())
- elseif rawget(self, i:lower()) then
- return rawget(self, i:lower())
- elseif i:lower() == 'unit' then
- return self / self.magnitude
- end
- end
- function mt:__tostring()
- return self.x .. ', ' .. self.y .. ', ' .. self.z
- end
- function Vector3:Lerp(b, a)
- return self + (b - self) * a
- end
- local a = Vector3.new(6, 4, 2)
- local b = Vector3.new(3, 2, 1)
- print(a:Lerp())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement