Advertisement
jordan83221

Balloon Verlet

Mar 25th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. local next=next
  2. local gravity=0.09
  3. local friction=0.85
  4. local bounce=0.8
  5. local drag=Instance.new("Part",workspace)
  6. drag.Name="Dragger"
  7. drag.Anchored=true
  8. drag.CanCollide=false
  9. drag.Locked=true
  10. deselected=true
  11. drag.Transparency=1
  12. drag.Position=Vector3.new(0,50,0)
  13. local player=game.Players.LocalPlayer
  14. local char=player.Character
  15. local torso=player.Character.Torso
  16. local humanoid=player.Character.Humanoid
  17. local head=player.Character.Head
  18. local camera=workspace.CurrentCamera
  19. local RA=char["Right Arm"]
  20. local LA=char["Left Arm"]
  21. local RL=char["Right Leg"]
  22. local LL=char["Left Leg"]
  23. local speed=100
  24. local function dist(p0,p1)
  25.     local dx=p1.CFrame.x-p0.CFrame.x
  26.     local dy=p1.CFrame.y-p0.CFrame.y
  27.     local dz=p1.CFrame.z-p0.CFrame.z
  28.     return math.sqrt(dx*dx+dy*dy+dz*dz)
  29. end
  30. local points={}
  31. local joints={}
  32. local function createPoint(x,y,z,pinned)
  33.     local t = {x=x,y=y,z=z,oldx=x-0.4,oldy=y-0.4,oldz=z-0.4,pinned=pinned}
  34.     points[#points+1]=t
  35.     return t
  36. end
  37. local function createJoint(point1,point2,distance,line)
  38.     local t = {point1=point1,point2=point2,distance=distance,line=line}
  39.     table.insert(joints,t)
  40. end
  41. local function drawLine()
  42.     for i=1,#joints do
  43.         local j=joints[i]
  44.         local line=j.line
  45.         if not line then
  46.             line=Instance.new("Part",player.Character)
  47.             line.Name=tostring(i)
  48.             line.Anchored=true
  49.             line.CanCollide=false
  50.             line.BottomSurface="Smooth"
  51.             line.TopSurface="Smooth"
  52.             j.line=line
  53.             if line.Name==tostring(29) then
  54.                 local mesh=Instance.new("SpecialMesh",line)
  55.                 mesh.Name="Mesh"
  56.                 mesh.MeshType="Sphere"
  57.             else
  58.                 local mesh=Instance.new("BlockMesh",line)
  59.                 mesh.Scale=Vector3.new(1,1,1)
  60.             end
  61.         end
  62.         local dx=j.point2.x-j.point1.x
  63.         local dy=j.point2.y-j.point1.y
  64.         local dz=j.point2.z-j.point1.z
  65.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  66.         if line.Name==tostring(29) then
  67.             line.BrickColor=BrickColor.new("Really red")
  68.             line.Size=Vector3.new(3, 3, 3.6)
  69.         else
  70.             line.BrickColor=BrickColor.new("Gold")
  71.             line.Size=Vector3.new(0.1,0.4,distance)
  72.         end
  73.         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))
  74.         *CFrame.new(0,0,-distance/2)
  75.     end
  76. end
  77. local width=1
  78. local height=30
  79. local l=0.25
  80. for y=1,height do
  81.     for x=1,width do
  82.         createPoint(l*x,25-l*y,0,y==1)
  83.         if x>1 then
  84.             createJoint(points[width*(y-1)+x-1],points[width*(y-1)+x],l)
  85.         end
  86.         if y>1 then
  87.             createJoint(points[width*(y-2)+x],points[width*(y-1)+x],l)
  88.         end
  89.     end
  90. end
  91. local function updatePoints()
  92.     for i=1,#points do
  93.         local p=points[i]
  94.         if not p.pinned then
  95.             local vx=(p.x-p.oldx)*friction;
  96.             local vy=(p.y-p.oldy)*friction;
  97.             local vz=(p.z-p.oldz)*friction;
  98.             p.oldx=p.x
  99.             p.oldy=p.y
  100.             p.oldz=p.z
  101.             p.x=p.x+vx
  102.             p.y=p.y+vy
  103.             p.z=p.z+vz
  104.             p.y=p.y+gravity
  105.         end
  106.     end
  107. end
  108. local function updateSticks()
  109.     for i=1,#joints do
  110.         local j=joints[i]
  111.         local dx=j.point2.x-j.point1.x
  112.         local dy=j.point2.y-j.point1.y
  113.         local dz=j.point2.z-j.point1.z
  114.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  115.         local difference=j.distance-distance
  116.         local percent=difference/distance/2
  117.         local offsetX=dx*percent;
  118.         local offsetY=dy*percent;
  119.         local offsetZ=dz*percent;
  120.         if j.point1.pinned or j.point2.pinned then
  121.             offsetX=offsetX*2
  122.             offsetY=offsetY*2
  123.             offsetZ=offsetZ*2
  124.         end
  125.         if not j.point1.pinned then
  126.             j.point1.x=j.point1.x-offsetX
  127.             j.point1.y=j.point1.y-offsetY
  128.             j.point1.z=j.point1.z-offsetZ
  129.         end
  130.         if not j.point2.pinned then
  131.             j.point2.x=j.point2.x+offsetX
  132.             j.point2.y=j.point2.y+offsetY
  133.             j.point2.z=j.point2.z+offsetZ
  134.         end
  135.     end
  136. end
  137. game:service'RunService'.Stepped:connect(function()
  138.     for i=1,width do
  139.         local p=player.Character["Right Arm"].CFrame*CFrame.new(0,0,0)
  140.         points[i].x=p.x
  141.         points[i].y=p.y
  142.         points[i].z=p.z
  143.     end
  144.     updatePoints()
  145.     for i=1,10 do
  146.         updateSticks()
  147.     end
  148.     drawLine()
  149.     coroutine.resume(coroutine.create(function()
  150.         if deselected then
  151.             drag.CFrame=drag.CFrame*CFrame.new(0,math.sin(tick()*2)/15,0)
  152.         end
  153.     end))
  154. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement