Advertisement
denissini15

Untitled

Feb 18th, 2022
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. --custom angles (just like v3 but for math with radian orientation)
  2. --StaliniumDev
  3. --who ever reads it ur free to use it lol
  4. --10 Jun 2021
  5.  
  6.  
  7. local allowed = {'x', 'y', 'z', 'tocf'}
  8. local pi, tpi = math.pi, math.pi*2
  9. local sin, cos = math.sin, math.cos
  10.  
  11. local och = function(ang)
  12.     return ang%tpi
  13. end
  14.  
  15.  
  16.  
  17. local gcl = function(tar, orig)
  18.     local a = tar - orig
  19.     a = (a+pi)%tpi-pi
  20.     return a
  21. end
  22.  
  23.  
  24. local lerp = function(self:angles, target:angles, step:number)
  25.     return self+(target-self)*step
  26. end
  27.  
  28. local tocf = function(self)
  29.     return CFrame.fromOrientation(self.x, self.y, self.z)
  30. end
  31.  
  32. angles = {
  33.     new = function(x,y,z, raw)
  34.         x = x or 0
  35.         y = y or 0
  36.         z = z or 0
  37.         if not raw  and false then
  38.             return setmetatable ( { -- this never runs lol
  39.                 x = och(x);
  40.                 y = och(y);
  41.                 z = och(z);
  42.                 tocf = tocf;
  43.             }, metafun )
  44.         else
  45.             return setmetatable ( {
  46.                 x = x;
  47.                 y = y;
  48.                 z = z;
  49.                 tocf = tocf;
  50.                 lerp = lerp;
  51.                 Lerp = lerp;
  52.             }, metafun )
  53.         end
  54.     end,
  55.     fromcf = function(cf)
  56.         return angles.new(cf:ToOrientation())
  57.     end;
  58. }
  59.  
  60. local tdeg = function(a)
  61.     return math.floor(math.deg(a)*100)/100
  62. end
  63.  
  64. metafun = {
  65.     __tostring = function(self)
  66.         return tdeg(self.x)..', '..tdeg (self.y)..', '.. tdeg(self.z)
  67.     end,
  68.     __index = function(self, index)
  69.         local low = string.lower(index)
  70.  
  71.         if low == 'unit' then
  72.             return self/self.magnitude
  73.         elseif low == 'magnitude' then
  74.             return (self.x^2+self.y^2+self.z^2)^0.5
  75.         end
  76.  
  77.         if table.find(allowed, low) then
  78.             return rawget(self, low)
  79.         else
  80.             error(tostring(index)..' is not a valid member of angles')
  81.         end
  82.     end,
  83.     __newindex = function(self, index)
  84.         error('unable to set a new index in angles')
  85.     end,
  86.     __add = function(self, a2)
  87.         return angles.new(self.x+a2.x, self.y+a2.y, self.z+a2.z )
  88.     end,
  89.     __sub = function(self, a2)
  90.        
  91.         return angles.new(
  92.             gcl( self.x, a2.x ),
  93.             gcl( self.y, a2.y ),
  94.             gcl( self.z, a2.z )
  95.             , true)
  96.     end,
  97.     __unm = function(self)
  98.         return angles.new(-self.x, -self.y, -self.z)
  99.     end,
  100.     __mul = function(self, a2)
  101.         if typeof(a2) == 'number' then
  102.             return angles.new(self.x*a2, self.y*a2, self.z*a2)
  103.         else
  104.             return angles.new(self.x*a2.x, self.y*a2.y, self.z*a2.z )
  105.         end
  106.     end,
  107.     __div = function(self, a2)
  108.         if typeof(a2) == 'number' then
  109.             return angles.new(self.x/a2, self.y/a2, self.z/a2)
  110.         else
  111.             return angles.new(self.x/a2.x, self.y/a2.y, self.z/a2.z )
  112.         end
  113.     end,
  114. }
  115.  
  116.  
  117.  
  118. return angles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement