Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module("Vector3", package.seeall)
- local meta = {
- __index = {
- ObjectType = "Vector3";
- length = function(self)
- return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
- end;
- dot = function(self, vec3)
- return self.x * vec3.x + self.y * vec3.y + self.z * vec3.z
- end;
- cross = function(self, vec3)
- local x_ = self.y * vec3.z - self.z * vec3.y;
- local y_ = self.z * vec3.x - self.x * vec3.z;
- local z_ = self.x * vec3.y - self.y * vec3.x;
- return Vector3.new(x_, y_, z_);
- end;
- normalize = function(self)
- local length = self:length()
- return Vector3.new(self.x / length, self.y / length, self.z / length)
- end;
- inverse = function(self)
- return Vector3.new(self.x * -1, self.y * -1, self.z * -1)
- end;
- max = function(self)
- return math.max(self.x, math.max(self.y, self.z))
- end;
- lerp = function(self, vec3, factor)
- return vec3 - self * factor + self
- end;
- set = function(self, x, y, z)
- self.x = x
- self.y = y
- self.z = z
- return self
- end;
- setVec3 = function(self, vec3)
- self:set(vec3.x, vec3.y, vec3.z)
- return self
- end;
- };
- __newindex = function(self, k, v)
- if (k == "x" or k == "y" or k == "z") then
- self[k] = v
- else
- error("Vector3: The property '"..k.."' is locked.")
- end
- end;
- __add = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x + b, a.y + b, a.z + b)
- else
- return Vector3.new(a.x + b.x, a.y + b.y, a.z + b.z)
- end
- end;
- __sub = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x - b, a.y - b, a.z - b)
- else
- return Vector3.new(a.x - b.x, a.y - b.y, a.z - b.z)
- end
- end;
- __mul = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x * b, a.y * b, a.z * b)
- else
- return Vector3.new(a.x * b.x, a.y * b.y, a.z * b.z)
- end
- end;
- __div = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x / b, a.y / b, a.z / b)
- else
- return Vector3.new(a.x / b.x, a.y / b.y, a.z / b.z)
- end
- end;
- __mod = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x % b, a.y % b, a.z % b)
- else
- return Vector3.new(a.x % b.x, a.y % b.y, a.z % b.z)
- end
- end;
- __pow = function(a, b)
- if (type(b) == "number") then
- return Vector3.new(a.x ^ b, a.y ^ b, a.z ^ b)
- else
- return Vector3.new(a.x ^ b.x, a.y ^ b.y, a.z ^ b.z)
- end
- end;
- __tostring = function(self)
- return "("..self.x..", "..self.y..", "..self.z..")"
- end;
- __eq = function(a, b)
- if (a.x == b.x and a.y == b.y and a.z == b.z) then
- return true
- else
- return false
- end
- end;
- }
- function new(x, y, z)
- return setmetatable({x = x, y = y, z = z}, meta) --{} has public variables
- end
Advertisement
Add Comment
Please, Sign In to add comment