Advertisement
jordan83221

Verlet Cape

Mar 25th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. local next=next
  2. local gravity=0.09
  3. local friction=0.7
  4. local bounce=0.8
  5. local player=game.Players.LocalPlayer
  6. local torso=player.Character.Torso
  7. local function dist(p0,p1)
  8.     local dx=p1.CFrame.x-p0.CFrame.x
  9.     local dy=p1.CFrame.y-p0.CFrame.y
  10.     local dz=p1.CFrame.z-p0.CFrame.z
  11.     return math.sqrt(dx*dx+dy*dy+dz*dz)
  12. end
  13. local points={}
  14. local joints={}
  15. local function createPoint(x,y,z,pinned)
  16.     local t = {x=x,y=y,z=z,oldx=x-0.4,oldy=y-0.4,oldz=z-0.4,pinned=pinned}
  17.     points[#points+1]=t
  18.     return t
  19. end
  20. local function createJoint(point1,point2,distance,line)
  21.     local t = {point1=point1,point2=point2,distance=distance,line=line}
  22.     table.insert(joints,t)
  23. end
  24. local function drawLine()
  25.     for i=1,#joints do
  26.         local j=joints[i]
  27.         local line=j.line
  28.         if not line then
  29.             line=Instance.new("Part",workspace.CurrentCamera)
  30.             line.BrickColor=BrickColor.new("Really black")
  31.             line.Anchored=true
  32.             line.CanCollide=false
  33.             line.BottomSurface="Smooth"
  34.             line.TopSurface="Smooth"
  35.             local mesh=Instance.new("BlockMesh",line)
  36.             mesh.Scale=Vector3.new(0.001,1.3,1.2)
  37.             j.line=line
  38.         end
  39.         local dx=j.point2.x-j.point1.x
  40.         local dy=j.point2.y-j.point1.y
  41.         local dz=j.point2.z-j.point1.z
  42.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  43.         line.Size=Vector3.new(0.1,0.4,distance)
  44.         line.CFrame=CFrame.new(Vector3.new(j.point1.x,j.point1.y,j.point1.z),Vector3.new(j.point2.x,j.point2.y,j.point2.z))
  45.         *CFrame.new(0,0,-distance/2)
  46.     end
  47. end
  48. local width=9
  49. local height=15
  50. local l=0.25
  51. for y=1,height do
  52.     for x=1,width do
  53.         createPoint(l*x,25-l*y,0,y==1)
  54.         if x>1 then
  55.             createJoint(points[width*(y-1)+x-1],points[width*(y-1)+x],l)
  56.         end
  57.         if y>1 then
  58.             createJoint(points[width*(y-2)+x],points[width*(y-1)+x],l)
  59.         end
  60.     end
  61. end
  62. local function updatePoints()
  63.     for i=1,#points do
  64.         local p=points[i]
  65.         if not p.pinned then
  66.             local vx=(p.x-p.oldx)*friction;
  67.             local vy=-((p.y-p.oldy)*friction);
  68.             local vz=(p.z-p.oldz)*friction;
  69.             p.oldx=p.x
  70.             p.oldy=p.y
  71.             p.oldz=p.z
  72.             p.x=p.x+vx
  73.             p.y=p.y-vy
  74.             p.z=p.z+vz
  75.             p.y=p.y-gravity
  76.             if p.y<-1 then
  77.                 p.y=0
  78.                 --p.oldy=p.y-vy*bounce;
  79.             end
  80.             local pos=torso.CFrame:inverse()*Vector3.new(p.x,p.y,p.z)
  81.             if pos.z<0 then
  82.                 pos=torso.CFrame*CFrame.new(p.x,p.y,0)
  83.                 p.x=pos.x
  84.                 p.y=pos.y
  85.                 p.z=pos.z
  86.                 p.oldx=p.x-vx*bounce;
  87.                 p.oldy=p.y-vy*bounce;
  88.                 p.oldz=p.z-vz*bounce;
  89.             end
  90.         end
  91.     end
  92. end
  93. local function updateSticks()
  94.     for i=1,#joints do
  95.         local j=joints[i]
  96.         local dx=j.point2.x-j.point1.x
  97.         local dy=j.point2.y-j.point1.y
  98.         local dz=j.point2.z-j.point1.z
  99.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  100.         local difference=j.distance-distance
  101.         local percent=difference/distance/2
  102.         local offsetX=dx*percent;
  103.         local offsetY=dy*percent;
  104.         local offsetZ=dz*percent;
  105.         if j.point1.pinned or j.point2.pinned then
  106.             offsetX=offsetX*2
  107.             offsetY=offsetY*2
  108.             offsetZ=offsetZ*2
  109.         end
  110.         if not j.point1.pinned then
  111.             j.point1.x=j.point1.x-offsetX
  112.             j.point1.y=j.point1.y-offsetY
  113.             j.point1.z=j.point1.z-offsetZ
  114.         end
  115.         if not j.point2.pinned then
  116.             j.point2.x=j.point2.x+offsetX
  117.             j.point2.y=j.point2.y+offsetY
  118.             j.point2.z=j.point2.z+offsetZ
  119.         end
  120.     end
  121. end
  122. game:service'RunService'.RenderStepped:connect(function()
  123.     for i=1,width do
  124.         local p=torso.CFrame*CFrame.new(-1+0.25*(i-1),1,0.75)
  125.         points[i].x=p.x
  126.         points[i].y=p.y
  127.         points[i].z=p.z
  128.     end
  129.     updatePoints()
  130.     for i=1,10 do
  131.         updateSticks()
  132.     end
  133.     drawLine()
  134. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement