Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- local pi =math.pi
- local halfpi =pi/2
- local cos =math.cos
- local sin =math.sin
- local acos =math.acos
- local v3 =Vector3.new
- local nv =v3()
- local cf =CFrame.new
- local nc =cf()
- local components =nc.components
- local tos =nc.toObjectSpace
- local vtos =nc.vectorToObjectSpace
- local ptos =nc.pointToObjectSpace
- local backcf =cf(0,0,0,-1,0,0,0,1,0,0,0,-1)
- local lerp =nc.lerp
- cframe.identity =nc
- cframe.new =cf
- cframe.vtws =nc.vectorToWorldSpace
- cframe.tos =nc.toObjectSpace
- cframe.ptos =nc.pointToObjectSpace
- cframe.vtos =nc.vectorToObjectSpace
- function cframe.fromaxisangle(x,y,z)
- if not y then
- x,y,z=x.x,x.y,x.z
- end
- local m=(x*x+y*y+z*z)^0.5
- if m>1e-5 then
- local si=sin(m/2)/m
- return cf(0,0,0,si*x,si*y,si*z,cos(m/2))
- else
- return nc
- end
- end
- function cframe.toaxisangle(c)
- local _,_,_,
- xx,yx,zx,
- xy,yy,zy,
- xz,yz,zz=components(c)
- local co=(xx+yy+zz-1)/2
- if co<-0.99999 then
- local x=xx+yx+zx+1
- local y=xy+yy+zy+1
- local z=xz+yz+zz+1
- local m=pi*(x*x+y*y+z*z)^-0.5
- return v3(m*x,m*y,m*z)
- elseif co<0.99999 then
- local x=yz-zy
- local y=zx-xz
- local z=xy-yx
- local m=acos(co)*(x*x+y*y+z*z)^-0.5
- return v3(m*x,m*y,m*z)
- else
- return nv
- end
- end
- function cframe.direct(c,look,newdir,t)
- local lx,ly,lz =look.x,look.y,look.z
- local rv =vtos(c,newdir)
- local rx,ry,rz =rv.x,rv.y,rv.z
- local rl =((rx*rx+ry*ry+rz*rz)*(lx*lx+ly*ly+lz*lz))^0.5
- local d =(lx*rx+ly*ry+lz*rz)/rl
- if d<-0.99999 then
- return c*backcf
- elseif d<0.99999 then
- if t then
- local th =t*acos(d)/2
- local qw =cos(th)
- local m =rl*((1-d*d)/(1-qw*qw))^0.5
- return c*cf(
- 0,0,0,
- (ly*rz-lz*ry)/m,
- (lz*rx-lx*rz)/m,
- (lx*ry-ly*rx)/m,
- qw
- )
- else
- local qw =((d+1)/2)^0.5
- local m =2*qw*rl
- return c*cf(
- 0,0,0,
- (ly*rz-lz*ry)/m,
- (lz*rx-lx*rz)/m,
- (lx*ry-ly*rx)/m,
- qw
- )
- end
- else
- return c
- end
- end
- function cframe.toquaternion(c)
- local x,y,z,
- xx,yx,zx,
- xy,yy,zy,
- xz,yz,zz =components(c)
- local tr =xx+yy+zz
- if tr>2.99999 then
- return x,y,z,0,0,0,1
- elseif tr>-0.99999 then
- local m =2*(tr+1)^0.5
- return x,y,z,
- (yz-zy)/m,
- (zx-xz)/m,
- (xy-yx)/m,
- m/4
- else
- local qx =xx+yx+zx+1
- local qy =xy+yy+zy+1
- local qz =xz+yz+zz+1
- local m =(qx*qx+qy*qy+qz*qz)^0.5
- return x,y,z,qx/m,qy/m,qz/m,0
- end
- end
- function cframe.power(c,t)
- --[[ local x,y,z,
- xx,yx,zx,
- xy,yy,zy,
- xz,yz,zz =components(c)
- local tr =xx+yy+zz
- if tr>2.99999 then
- return cf(t*x,t*y,t*z)
- elseif tr>-0.99999 then
- local m =2*(tr+1)^0.5
- local qw =m/4
- local th =acos(qw)
- local s =(1-qw*qw)^0.5
- local c =sin(th*t)/s
- 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
- )
- else
- local qx =xx+yx+zx+1
- local qy =xy+yy+zy+1
- local qz =xz+yz+zz+1
- local c =sin(halfpi*t)/(qx*qx+qy*qy+qz*qz)^0.5
- return cf(
- t*x,t*y,t*z,
- c*qx,
- c*qy,
- c*qz,
- sin(halfpi*(1-t))
- )
- end]]
- return lerp(nc,c,t)
- end
- --local power=cframe.power
- --[[function cframe.interpolate(c0,c1,t)
- --return c0*power(tos(c0,c1),t)
- end]]
- cframe.interpolate=lerp
- --local toquaternion=cframe.toquaternion
- function cframe.interpolator(c0,c1,c2)
- --[[if c1 then
- local x0,y0,z0,qx0,qy0,qz0,qw0=toquaternion(c0)
- local x1,y1,z1,qx1,qy1,qz1,qw1=toquaternion(c1)
- local x,y,z=x1-x0,y1-y0,z1-z0
- local c=qx0*qx1+qy0*qy1+qz0*qz1+qw0*qw1
- if c<0 then
- qx0,qy0,qz0,qw0=-qx0,-qy0,-qz0,-qw0
- end
- if c<0.9999 then
- local s=(1-c*c)^0.5
- local th=acos(c)
- return function(t)
- local s0=sin(th*(1-t))/s
- local s1=sin(th*t)/s
- 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
- )
- end
- else
- return function(t)
- return cf(x0+t*x,y0+t*y,z0+t*z,qx1,qy1,qz1,qw1)
- end
- end
- else
- local x,y,z,qx,qy,qz,qw=cframe.toquaternion(c0)
- if qw<0.9999 then
- local s=(1-qw*qw)^0.5
- local th=acos(qw)
- return function(t)
- local s1=sin(th*t)/s
- return cf(
- t*x,
- t*y,
- t*z,
- s1*qx,
- s1*qy,
- s1*qz,
- sin(th*(1-t))/s+s1*qw
- )
- end
- else
- return function(t)
- return cf(t*x,t*y,t*z,qx,qy,qz,qw)
- end
- end
- end]]
- if c2 then
- return function(t)
- return lerp(lerp(c0,c1,t),lerp(c1,c2,t),t)
- end
- elseif c1 then
- return function(t)
- return lerp(c0,c1,t)
- end
- else
- return function(t)
- return lerp(nc,c0,t)
- end
- end
- end
- function cframe.jointleg(r0,r1,c,p,a)
- local t=ptos(c,p)
- local tx,ty,tz=t.x,t.y,t.z
- --Calculate inverse kinemetics equation
- local d=(tx*tx+ty*ty+tz*tz)^0.5
- local nx,ny,nz=tx/d,ty/d,tz/d
- d=r0+r1<d and r0+r1 or d
- local l=(r1*r1-r0*r0-d*d)/(2*r0*d)
- local h=-(1-l*l)^0.5
- --Generate the natural quaternion for the shoulder.
- local m=(2*(1+h*ny+l*nz))^0.5
- local qw,qx,qy,qz=m/2,(h*nz-l*ny)/m,l*nx/m,-h*nx/m
- --If a, then rotate the natural quaternion by a.
- if a then
- local co,si=cos(a/2),sin(a/2)
- qw,qx,qy,qz=co*qw-si*(nx*qx+ny*qy+nz*qz),
- co*qx+si*(nx*qw-nz*qy+ny*qz),
- co*qy+si*(ny*qw+nz*qx-nx*qz),
- co*qz+si*(nz*qw-ny*qx+nx*qy)
- end
- --Generate the quaternion for the lower arm and return.
- local g=(d*l+r0)/(d*d+2*d*l*r0+r0*r0)^0.5
- local co=((1-g)/2)^0.5
- local si=-((1+g)/2)^0.5
- return c*cf(-r0*2*(qx*qz+qy*qw),
- r0*2*(qx*qw-qy*qz),
- r0*(2*(qx*qx+qy*qy)-1),
- co*qx+si*qw,
- co*qy+si*qz,
- co*qz-si*qy,
- co*qw-si*qx),
- c*cf(0,0,0,qx,qy,qz,qw)
- end
- function cframe.jointarm(r0,r1,c,p,a)
- local t=ptos(c,p)
- local tx,ty,tz=t.x,t.y,t.z
- --Calculate inverse kinemetics equation
- local d=(tx*tx+ty*ty+tz*tz)^0.5
- local nx,ny,nz=tx/d,ty/d,tz/d
- d=r0+r1<d and r0+r1 or d
- local l=(r1*r1-r0*r0-d*d)/(2*r0*d)
- local h=(1-l*l)^0.5
- --Generate the natural quaternion for the shoulder.
- local m=(2*(1+h*ny+l*nz))^0.5
- local qw,qx,qy,qz=m/2,(h*nz-l*ny)/m,l*nx/m,-h*nx/m
- --If a, then rotate the natural quaternion by a.
- if a then
- local co,si=cos(a/2),sin(a/2)
- qw,qx,qy,qz=co*qw-si*(nx*qx+ny*qy+nz*qz),
- co*qx+si*(nx*qw-nz*qy+ny*qz),
- co*qy+si*(ny*qw+nz*qx-nx*qz),
- co*qz+si*(nz*qw-ny*qx+nx*qy)
- end
- --Generate the quaternion for the lower arm and return.
- local g=(d*l+r0)/(d*d+2*d*l*r0+r0*r0)^0.5
- local co=((1-g)/2)^0.5
- local si=((1+g)/2)^0.5
- return c*cf(-r0*2*(qx*qz+qy*qw),
- r0*2*(qx*qw-qy*qz),
- r0*(2*(qx*qx+qy*qy)-1),
- co*qx+si*qw,
- co*qy+si*qz,
- co*qz-si*qy,
- co*qw-si*qx),
- c*cf(0,0,0,qx,qy,qz,qw)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement