Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector3 = {}
- function Vector3.IsVector(otherVec)
- local is = pcall(function ()
- -- If this is legitimate, it should concatenate without a problem.
- return otherVec.X .. otherVec.Y .. otherVec.Z .. otherVec.magnitude .. otherVec.unit
- end)
- return is
- end
- function Vector3.new(x,y,z)
- local x,y,z = tonumber(x) or 0, tonumber(y) or 0, tonumber(z) or 0
- local m = math.sqrt(x^2+y^2+z^2)
- local meta = {}
- local function insert(k,v)
- meta["__"..k] = v
- end
- local vec = {}
- vec.X = x;
- vec.Y = y;
- vec.Z = z;
- vec.magnitude = m;
- vec.unit = (vec.magnitude == 1 and vec or Vector3.new(x/m,y/m,z/m))
- --
- local vecStr = function ()
- return vec.X .. ", " .. vec.Y .. ", " .. vec.Z
- end
- insert("newindex",error)
- insert("tostring",vecStr)
- local operators = {add = "+"; sub = "-"; mul = "*"; div = "/"};
- for key,op in pairs(operators) do
- insert(key,function (_,v)
- local x,y,z do
- if type(v) == "number" then
- x,y,z = v,v,v
- else
- assert(Vector3.IsVector(v),"attempt to perform arithmetic on a Vector3 using a " .. type(v))
- x,y,z = v.X,v.Y,v.Z
- end
- end
- local xOp = vec.X .. op .. x
- local yOp = vec.Y .. op .. y
- local zOp = vec.Z .. op .. z
- return loadstring("return Vector3.new(" .. xOp .. "," .. yOp .. "," .. zOp .. ")")()
- end)
- end
- setmetatable(vec,meta)
- return vec
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement