Advertisement
Kurokku

Untitled

Jul 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. do
  2. local pi =math.pi
  3. local halfpi =pi/2
  4. local cos =math.cos
  5. local sin =math.sin
  6. local acos =math.acos
  7. local v3 =Vector3.new
  8. local nv =v3()
  9. local cf =CFrame.new
  10. local nc =cf()
  11. local components =nc.components
  12. local tos =nc.toObjectSpace
  13. local vtos =nc.vectorToObjectSpace
  14. local ptos =nc.pointToObjectSpace
  15. local backcf =cf(0,0,0,-1,0,0,0,1,0,0,0,-1)
  16. local lerp =nc.lerp
  17.  
  18. cframe.identity =nc
  19. cframe.new =cf
  20. cframe.vtws =nc.vectorToWorldSpace
  21. cframe.tos =nc.toObjectSpace
  22. cframe.ptos =nc.pointToObjectSpace
  23. cframe.vtos =nc.vectorToObjectSpace
  24.  
  25.  
  26. function cframe.fromaxisangle(x,y,z)
  27. if not y then
  28. x,y,z=x.x,x.y,x.z
  29. end
  30. local m=(x*x+y*y+z*z)^0.5
  31. if m>1e-5 then
  32. local si=sin(m/2)/m
  33. return cf(0,0,0,si*x,si*y,si*z,cos(m/2))
  34. else
  35. return nc
  36. end
  37. end
  38.  
  39. function cframe.toaxisangle(c)
  40. local _,_,_,
  41. xx,yx,zx,
  42. xy,yy,zy,
  43. xz,yz,zz=components(c)
  44. local co=(xx+yy+zz-1)/2
  45. if co<-0.99999 then
  46. local x=xx+yx+zx+1
  47. local y=xy+yy+zy+1
  48. local z=xz+yz+zz+1
  49. local m=pi*(x*x+y*y+z*z)^-0.5
  50. return v3(m*x,m*y,m*z)
  51. elseif co<0.99999 then
  52. local x=yz-zy
  53. local y=zx-xz
  54. local z=xy-yx
  55. local m=acos(co)*(x*x+y*y+z*z)^-0.5
  56. return v3(m*x,m*y,m*z)
  57. else
  58. return nv
  59. end
  60. end
  61.  
  62. function cframe.direct(c,look,newdir,t)
  63. local lx,ly,lz =look.x,look.y,look.z
  64. local rv =vtos(c,newdir)
  65. local rx,ry,rz =rv.x,rv.y,rv.z
  66. local rl =((rx*rx+ry*ry+rz*rz)*(lx*lx+ly*ly+lz*lz))^0.5
  67. local d =(lx*rx+ly*ry+lz*rz)/rl
  68. if d<-0.99999 then
  69. return c*backcf
  70. elseif d<0.99999 then
  71. if t then
  72. local th =t*acos(d)/2
  73. local qw =cos(th)
  74. local m =rl*((1-d*d)/(1-qw*qw))^0.5
  75. return c*cf(
  76. 0,0,0,
  77. (ly*rz-lz*ry)/m,
  78. (lz*rx-lx*rz)/m,
  79. (lx*ry-ly*rx)/m,
  80. qw
  81. )
  82. else
  83. local qw =((d+1)/2)^0.5
  84. local m =2*qw*rl
  85. return c*cf(
  86. 0,0,0,
  87. (ly*rz-lz*ry)/m,
  88. (lz*rx-lx*rz)/m,
  89. (lx*ry-ly*rx)/m,
  90. qw
  91. )
  92. end
  93. else
  94. return c
  95. end
  96. end
  97.  
  98. function cframe.toquaternion(c)
  99. local x,y,z,
  100. xx,yx,zx,
  101. xy,yy,zy,
  102. xz,yz,zz =components(c)
  103. local tr =xx+yy+zz
  104. if tr>2.99999 then
  105. return x,y,z,0,0,0,1
  106. elseif tr>-0.99999 then
  107. local m =2*(tr+1)^0.5
  108. return x,y,z,
  109. (yz-zy)/m,
  110. (zx-xz)/m,
  111. (xy-yx)/m,
  112. m/4
  113. else
  114. local qx =xx+yx+zx+1
  115. local qy =xy+yy+zy+1
  116. local qz =xz+yz+zz+1
  117. local m =(qx*qx+qy*qy+qz*qz)^0.5
  118. return x,y,z,qx/m,qy/m,qz/m,0
  119. end
  120. end
  121.  
  122. function cframe.power(c,t)
  123. --[[ local x,y,z,
  124. xx,yx,zx,
  125. xy,yy,zy,
  126. xz,yz,zz =components(c)
  127. local tr =xx+yy+zz
  128. if tr>2.99999 then
  129. return cf(t*x,t*y,t*z)
  130. elseif tr>-0.99999 then
  131. local m =2*(tr+1)^0.5
  132. local qw =m/4
  133. local th =acos(qw)
  134. local s =(1-qw*qw)^0.5
  135. local c =sin(th*t)/s
  136. return cf(
  137. t*x,t*y,t*z,
  138. c*(yz-zy)/m,
  139. c*(zx-xz)/m,
  140. c*(xy-yx)/m,
  141. c*qw+sin(th*(1-t))/s
  142. )
  143. else
  144. local qx =xx+yx+zx+1
  145. local qy =xy+yy+zy+1
  146. local qz =xz+yz+zz+1
  147. local c =sin(halfpi*t)/(qx*qx+qy*qy+qz*qz)^0.5
  148. return cf(
  149. t*x,t*y,t*z,
  150. c*qx,
  151. c*qy,
  152. c*qz,
  153. sin(halfpi*(1-t))
  154. )
  155. end]]
  156. return lerp(nc,c,t)
  157. end
  158.  
  159. --local power=cframe.power
  160. --[[function cframe.interpolate(c0,c1,t)
  161. --return c0*power(tos(c0,c1),t)
  162. end]]
  163. cframe.interpolate=lerp
  164.  
  165. --local toquaternion=cframe.toquaternion
  166. function cframe.interpolator(c0,c1,c2)
  167. --[[if c1 then
  168. local x0,y0,z0,qx0,qy0,qz0,qw0=toquaternion(c0)
  169. local x1,y1,z1,qx1,qy1,qz1,qw1=toquaternion(c1)
  170. local x,y,z=x1-x0,y1-y0,z1-z0
  171. local c=qx0*qx1+qy0*qy1+qz0*qz1+qw0*qw1
  172. if c<0 then
  173. qx0,qy0,qz0,qw0=-qx0,-qy0,-qz0,-qw0
  174. end
  175. if c<0.9999 then
  176. local s=(1-c*c)^0.5
  177. local th=acos(c)
  178. return function(t)
  179. local s0=sin(th*(1-t))/s
  180. local s1=sin(th*t)/s
  181. return cf(
  182. x0+t*x,
  183. y0+t*y,
  184. z0+t*z,
  185. s0*qx0+s1*qx1,
  186. s0*qy0+s1*qy1,
  187. s0*qz0+s1*qz1,
  188. s0*qw0+s1*qw1
  189. )
  190. end
  191. else
  192. return function(t)
  193. return cf(x0+t*x,y0+t*y,z0+t*z,qx1,qy1,qz1,qw1)
  194. end
  195. end
  196. else
  197. local x,y,z,qx,qy,qz,qw=cframe.toquaternion(c0)
  198. if qw<0.9999 then
  199. local s=(1-qw*qw)^0.5
  200. local th=acos(qw)
  201. return function(t)
  202. local s1=sin(th*t)/s
  203. return cf(
  204. t*x,
  205. t*y,
  206. t*z,
  207. s1*qx,
  208. s1*qy,
  209. s1*qz,
  210. sin(th*(1-t))/s+s1*qw
  211. )
  212. end
  213. else
  214. return function(t)
  215. return cf(t*x,t*y,t*z,qx,qy,qz,qw)
  216. end
  217. end
  218. end]]
  219. if c2 then
  220. return function(t)
  221. return lerp(lerp(c0,c1,t),lerp(c1,c2,t),t)
  222. end
  223. elseif c1 then
  224. return function(t)
  225. return lerp(c0,c1,t)
  226. end
  227. else
  228. return function(t)
  229. return lerp(nc,c0,t)
  230. end
  231. end
  232. end
  233.  
  234. function cframe.jointleg(r0,r1,c,p,a)
  235. local t=ptos(c,p)
  236. local tx,ty,tz=t.x,t.y,t.z
  237.  
  238. --Calculate inverse kinemetics equation
  239. local d=(tx*tx+ty*ty+tz*tz)^0.5
  240. local nx,ny,nz=tx/d,ty/d,tz/d
  241. d=r0+r1<d and r0+r1 or d
  242. local l=(r1*r1-r0*r0-d*d)/(2*r0*d)
  243. local h=-(1-l*l)^0.5
  244.  
  245. --Generate the natural quaternion for the shoulder.
  246. local m=(2*(1+h*ny+l*nz))^0.5
  247. local qw,qx,qy,qz=m/2,(h*nz-l*ny)/m,l*nx/m,-h*nx/m
  248.  
  249. --If a, then rotate the natural quaternion by a.
  250. if a then
  251. local co,si=cos(a/2),sin(a/2)
  252. qw,qx,qy,qz=co*qw-si*(nx*qx+ny*qy+nz*qz),
  253. co*qx+si*(nx*qw-nz*qy+ny*qz),
  254. co*qy+si*(ny*qw+nz*qx-nx*qz),
  255. co*qz+si*(nz*qw-ny*qx+nx*qy)
  256. end
  257.  
  258. --Generate the quaternion for the lower arm and return.
  259. local g=(d*l+r0)/(d*d+2*d*l*r0+r0*r0)^0.5
  260. local co=((1-g)/2)^0.5
  261. local si=-((1+g)/2)^0.5
  262. return c*cf(-r0*2*(qx*qz+qy*qw),
  263. r0*2*(qx*qw-qy*qz),
  264. r0*(2*(qx*qx+qy*qy)-1),
  265. co*qx+si*qw,
  266. co*qy+si*qz,
  267. co*qz-si*qy,
  268. co*qw-si*qx),
  269. c*cf(0,0,0,qx,qy,qz,qw)
  270. end
  271.  
  272. function cframe.jointarm(r0,r1,c,p,a)
  273. local t=ptos(c,p)
  274. local tx,ty,tz=t.x,t.y,t.z
  275.  
  276. --Calculate inverse kinemetics equation
  277. local d=(tx*tx+ty*ty+tz*tz)^0.5
  278. local nx,ny,nz=tx/d,ty/d,tz/d
  279. d=r0+r1<d and r0+r1 or d
  280. local l=(r1*r1-r0*r0-d*d)/(2*r0*d)
  281. local h=(1-l*l)^0.5
  282.  
  283. --Generate the natural quaternion for the shoulder.
  284. local m=(2*(1+h*ny+l*nz))^0.5
  285. local qw,qx,qy,qz=m/2,(h*nz-l*ny)/m,l*nx/m,-h*nx/m
  286.  
  287. --If a, then rotate the natural quaternion by a.
  288. if a then
  289. local co,si=cos(a/2),sin(a/2)
  290. qw,qx,qy,qz=co*qw-si*(nx*qx+ny*qy+nz*qz),
  291. co*qx+si*(nx*qw-nz*qy+ny*qz),
  292. co*qy+si*(ny*qw+nz*qx-nx*qz),
  293. co*qz+si*(nz*qw-ny*qx+nx*qy)
  294. end
  295.  
  296. --Generate the quaternion for the lower arm and return.
  297. local g=(d*l+r0)/(d*d+2*d*l*r0+r0*r0)^0.5
  298. local co=((1-g)/2)^0.5
  299. local si=((1+g)/2)^0.5
  300. return c*cf(-r0*2*(qx*qz+qy*qw),
  301. r0*2*(qx*qw-qy*qz),
  302. r0*(2*(qx*qx+qy*qy)-1),
  303. co*qx+si*qw,
  304. co*qy+si*qz,
  305. co*qz-si*qy,
  306. co*qw-si*qx),
  307. c*cf(0,0,0,qx,qy,qz,qw)
  308. end
  309. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement