Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. -- cframe module
  2. do
  3. local cf = CFrame.new
  4. local angles = CFrame.Angles
  5. local fromaxisangle = CFrame.fromAxisAngle
  6. local v3 = Vector3.new
  7. local nc = cf()
  8. local nv = v3()
  9. local components = nc.components
  10. local tos = nc.toObjectSpace
  11.  
  12. local pi = math.pi
  13. local acos = math.acos
  14. local atan = math.atan
  15. local sin = math.sin
  16. local sqrt = math.sqrt
  17. local max = math.max
  18. local min = math.min
  19. local deg90 = pi/2
  20.  
  21. local forward = v3(0,0,-1) -- arb
  22.  
  23. function cframe.toquaternion(c)
  24. local x,y,z,xx,yx,zx,xy,yy,zy,xz,yz,zz = components(c)
  25. local tr = xx+yy+zz
  26. if tr>2.99999 then
  27. return x,y,z,0,0,0,1
  28. elseif tr>-0.99999 then
  29. local m = 2*(tr+1)^0.5
  30. return x,y,z,(yz-zy)/m,(zx-xz)/m,(xy-yx)/m,m/4
  31. else
  32. local qx = xx+yx+zx+1
  33. local qy = xy+yy+zy+1
  34. local qz = xz+yz+zz+1
  35. local m = (qx*qx+qy*qy+qz*qz)^0.5
  36. return x,y,z,qx/m,qy/m,qz/m,0
  37. end
  38. end
  39. local toquaternion = cframe.toquaternion
  40.  
  41. function cframe.power(c,t)
  42. local x,y,z,xx,yx,zx,xy,yy,zy,xz,yz,zz = components(c)
  43. local tr = xx+yy+zz
  44. if tr > 2.99999 then
  45. return cf(t*x,t*y,t*z)
  46. elseif tr > -0.99999 then
  47. local m = 2*(tr+1)^5
  48. local qw = m/4
  49. local th = acos(qw)
  50. local s = (1-qw*qw)^0.5
  51. local c = sin(th*t)/s
  52. return cf(t*x,t*y,t*z,c*(yz-zy)/m,c*(zx-xz)/m,c*(xy-yx)/m,c*qw+sin(th*(1-t))/s)
  53. else
  54. local qx = xx+yx+zx+1
  55. local qy = xy+yy+zy+1
  56. local qz = xz+yz+zz+1
  57. local c = sin(deg90*t)/(qx*qx+qy*qy+qz*qz)^0.5
  58. return cf(t*x,t*y,t*z,c*qx,c*qy,c*qz,sin(deg90*(1-t)))
  59. end
  60. end
  61. local power = cframe.power
  62.  
  63. function cframe.interpolate(c0,c1,t)
  64. return c0*power(tos(c0,c1),t)
  65. end
  66. local interpolate = cframe.interpolate
  67.  
  68. function cframe.interpolator(c0,c1)
  69. if c1 then
  70. local x0,y0,z0,qx0,qy0,qz0,qw0 = toquaternion(c0)
  71. local x1,y1,z1,qx1,qy1,qz1,qw1 = toquaternion(c1)
  72. local x,y,z = x1-x0,y1-y0,z1-z0
  73. local c = qx0*qx1+qy0*qy1+qz0*qz1+qw0*qw1
  74. if c < 0 then
  75. qx0,qy0,qz0,qw0=-qx0,-qy0,-qz0,-qw0
  76. end
  77. if c < 0.9999 then
  78. local s = (1-c*c)^0.5
  79. local th = acos(c)
  80. return function(t)
  81. local s0 = sin(th*(1-t))/s
  82. local s1 = sin(th*t)/s
  83. return cf(x0+t*x,y0+t*y,z0+t*z,s0*qx0+s1*qx1,s0*qy0+s1*qy1,s0*qz0+s1*qz1,s0*qw0+s1*qw1)
  84. end
  85. else
  86. return function(t)
  87. return cf(x0+t*x,y0+t*y,z0+t*z,qx1,qy1,qz1,qw1)
  88. end
  89. end
  90. else
  91. local x,y,z,qx,qy,qz,qw = cframe.toquaternion(c0)
  92. if qw < 0.9999 then
  93. local s = (1-qw*qw)^0.5
  94. local th = acos(qw)
  95. return function(t)
  96. local s1 = sin(th*t)/s
  97. return cf(t*x,t*y,t*z,s1*qx,s1*qy,s1*qz,sin(th*(1-t))/s+s1*qw)
  98. end
  99. else
  100. return function(t)
  101. return cf(t*x,t*y,t*z,qx,qy,qz,qw)
  102. end
  103. end
  104. end
  105. end
  106. local interpolator = cframe.interpolator
  107.  
  108. function cframe.pitch(c)
  109. local v = c.lookVector
  110. return atan(v.Y/sqrt(v.X^2+v.Z^2))
  111. end
  112. local pitch = cframe.pitch
  113.  
  114. function cframe.fromtool(t)
  115. local gp = t.GripPos
  116. local gr = t.GripRight
  117. local gu = t.GripUp
  118. local gf = t.GripForward
  119. local x,y,z = gp.x,gp.y,gp.z
  120. local xx,xy,xz = gr.x,gu.x,-gf.x
  121. local yx,yy,yz = gr.y,gu.y,-gf.y
  122. local zx,zy,zz = gr.z,gu.z,-gf.z
  123. return cf(x,y,z,xx,xy,xz,yx,yy,yz,zx,zy,zz)
  124. end
  125. local fromtool = cframe.fromtool
  126.  
  127. function cframe.totool(c,t)
  128. local x,y,z,xx,xy,xz,yx,yy,yz,zx,zy,zz = components(c)
  129. local gp = v3(x,y,z)
  130. local gr = v3(xx,yx,zx)
  131. local gu = v3(xy,yy,zy)
  132. local gf = v3(-xz,-yz,-zz)
  133. if t then
  134. t.GripPos = gp
  135. t.GripRight = gr
  136. t.GripUp = gu
  137. t.GripForward = gf
  138. end
  139. return {gp,gr,gu,gf}
  140. end
  141. local totool = cframe.totool
  142.  
  143. local function tableshift(t0,p)
  144. local t1 = {}
  145. for i=1,#t0 do
  146. table.insert(t1,i,t0[i+p > #t0 and (i+p)%#t0 or i+p])
  147. end
  148. return t1
  149. end
  150.  
  151. function cframe.shift(c,p)
  152. local x0,y0,z0,xx0,xy0,xz0,yx0,yy0,yz0,zx0,zy0,zz0 = components(c)
  153. local v = tableshift({x0,y0,z0},p)
  154. local x = tableshift({xx0,yx0,zx0},p)
  155. local y = tableshift({xy0,yy0,zy0},p)
  156. local z = tableshift({xz0,yz0,zz0},p)
  157. local x1,y1,z1 = unpack(v)
  158. local xx1,xy1,xz1 = unpack(x)
  159. local yx1,yy1,yz1 = unpack(y)
  160. local zx1,zy1,zz1 = unpack(z)
  161. return cf(x1,y1,z1,xx1,xy1,xz1,yx1,yy1,yz1,zx1,zy1,zz1)
  162. end
  163. local shift = cframe.shift
  164.  
  165. function cframe.solve(originCF,targetPos,l1,l2)
  166. local localized = originCF:pointToObjectSpace(targetPos)
  167. local localizedUnit = localized.unit
  168. local l3 = localized.magnitude
  169. local axis = forward:Cross(localizedUnit)
  170. local angle = acos(-localizedUnit.Z)
  171. local planeCF = originCF*fromaxisangle(axis, angle)
  172. if l3 < max(l2,l1)-min(l2,l1) then
  173. return planeCF*cf(0,0,max(l2,l1)-min(l2,l1)-l3),-pi/2,pi
  174. elseif l3 > l1+l2 then
  175. return planeCF*cf(0,0,l1+l2-l3),pi/2,0
  176. else
  177. local a1 = -acos((-(l2*l2)+(l1*l1)+(l3*l3))/(2*l1*l3))
  178. local a2 = acos(((l2 *l2)-(l1*l1)+(l3*l3))/(2*l2*l3))
  179. return planeCF,a1+pi/2,a2-a1
  180. end
  181. end
  182. local solve = cframe.solve
  183.  
  184. local function isnan(x)
  185. return x ~= x
  186. end
  187.  
  188. function cframe.checknan(c)
  189. local comp = {c:components()}
  190. local nan = false
  191. for c=1,#comp do
  192. local v = comp[c]
  193. if isnan(v) then
  194. print("value is nan")
  195. nan = true
  196. end
  197. end
  198. return nan
  199. end
  200. local checknan = cframe.checknan
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement