Guest User

Vector3

a guest
Aug 8th, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. module("Vector3", package.seeall)
  2.  
  3. local meta = {
  4.   __index = {
  5.     ObjectType = "Vector3";
  6.     length = function(self)
  7.       return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  8.     end;
  9.    
  10.     dot = function(self, vec3)
  11.       return self.x * vec3.x + self.y * vec3.y + self.z * vec3.z
  12.     end;
  13.    
  14.     cross = function(self, vec3)
  15.       local x_ = self.y * vec3.z - self.z * vec3.y;
  16.       local y_ = self.z * vec3.x - self.x * vec3.z;
  17.       local z_ = self.x * vec3.y - self.y * vec3.x;
  18.  
  19.       return Vector3.new(x_, y_, z_);
  20.     end;
  21.    
  22.     normalize = function(self)
  23.       local length = self:length()
  24.      
  25.       return Vector3.new(self.x / length, self.y / length, self.z / length)
  26.     end;
  27.    
  28.     inverse = function(self)
  29.       return Vector3.new(self.x * -1, self.y * -1, self.z * -1)
  30.     end;
  31.    
  32.     max = function(self)
  33.       return math.max(self.x, math.max(self.y, self.z))
  34.     end;
  35.    
  36.     lerp = function(self, vec3, factor)
  37.       return vec3 - self * factor + self
  38.     end;
  39.    
  40.     set = function(self, x, y, z)
  41.       self.x = x
  42.       self.y = y
  43.       self.z = z
  44.       return self
  45.     end;
  46.    
  47.     setVec3 = function(self, vec3)
  48.       self:set(vec3.x, vec3.y, vec3.z)
  49.       return self
  50.     end;
  51.   };
  52.   __newindex = function(self, k, v)
  53.     if (k == "x" or k == "y" or k == "z") then
  54.       self[k] = v
  55.     else
  56.       error("Vector3: The property '"..k.."' is locked.")
  57.     end
  58.   end;
  59.  
  60.   __add = function(a, b)
  61.     if (type(b) == "number") then
  62.       return Vector3.new(a.x + b, a.y + b, a.z + b)
  63.     else
  64.       return Vector3.new(a.x + b.x, a.y + b.y, a.z + b.z)
  65.     end
  66.   end;
  67.  
  68.   __sub = function(a, b)
  69.     if (type(b) == "number") then
  70.       return Vector3.new(a.x - b, a.y - b, a.z - b)
  71.     else
  72.       return Vector3.new(a.x - b.x, a.y - b.y, a.z - b.z)
  73.     end
  74.   end;
  75.  
  76.   __mul = function(a, b)
  77.     if (type(b) == "number") then
  78.       return Vector3.new(a.x * b, a.y * b, a.z * b)
  79.     else
  80.       return Vector3.new(a.x * b.x, a.y * b.y, a.z * b.z)
  81.     end
  82.   end;
  83.  
  84.   __div = function(a, b)
  85.     if (type(b) == "number") then
  86.       return Vector3.new(a.x / b, a.y / b, a.z / b)
  87.     else
  88.       return Vector3.new(a.x / b.x, a.y / b.y, a.z / b.z)
  89.     end
  90.   end;
  91.  
  92.   __mod = function(a, b)
  93.     if (type(b) == "number") then
  94.       return Vector3.new(a.x % b, a.y % b, a.z % b)
  95.     else
  96.       return Vector3.new(a.x % b.x, a.y % b.y, a.z % b.z)
  97.     end
  98.   end;
  99.  
  100.   __pow = function(a, b)
  101.     if (type(b) == "number") then
  102.       return Vector3.new(a.x ^ b, a.y ^ b, a.z ^ b)
  103.     else
  104.       return Vector3.new(a.x ^ b.x, a.y ^ b.y, a.z ^ b.z)
  105.     end
  106.   end;
  107.  
  108.   __tostring = function(self)
  109.     return "("..self.x..", "..self.y..", "..self.z..")"
  110.   end;
  111.  
  112.   __eq = function(a, b)
  113.     if (a.x == b.x and a.y == b.y and a.z == b.z) then
  114.       return true
  115.     else
  116.       return false
  117.     end
  118.   end;
  119. }
  120.  
  121. function new(x, y, z)
  122.   return setmetatable({x = x, y = y, z = z}, meta) --{} has public variables
  123. end
Advertisement
Add Comment
Please, Sign In to add comment