jordan83221

Code

Mar 25th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. local next=next
  2. local gravity=0.09
  3. local friction=0.999
  4. local bounce=0.99
  5. local function dist(p0,p1)
  6.     local dx=p1.CFrame.x-p0.CFrame.x
  7.     local dy=p1.CFrame.y-p0.CFrame.y
  8.     local dz=p1.CFrame.z-p0.CFrame.z
  9.     return math.sqrt(dx*dx+dy*dy+dz*dz)
  10. end
  11. local points={}
  12. local joints={}
  13. local function createPoint(x,y,z,pinned)
  14.     local ball=Instance.new("Part",workspace)
  15.     ball.Shape="Ball"
  16.     ball.Name=tostring(#points+1)
  17.     ball.Size=Vector3.new(5,5,5)
  18.     ball.CanCollide=false
  19.     ball.Anchored=true
  20.     ball.BottomSurface=Enum.SurfaceType.Smooth
  21.     ball.TopSurface=Enum.SurfaceType.Smooth
  22.     ball.BrickColor=BrickColor.new("Really red")
  23.     ball.Material = "Neon"
  24.     ball.CFrame=CFrame.new(x,y,z)
  25.     local t = {x=x,y=y,z=z,oldx=x-0.4,oldy=y-0.4,oldz=z-0.4,ball=ball,pinned=pinned}
  26.     points[#points+1]=t
  27.     return t
  28. end
  29. local function createJoint(point1,point2,distance,line)
  30.     local t = {point1=point1,point2=point2,distance=distance,line=line}
  31.     table.insert(joints,t)
  32. end
  33. local function drawLine()
  34.     for i=1,#joints do
  35.         local j=joints[i]
  36.         local line=j.line
  37.         if not line then
  38.             line=Instance.new("Part",workspace)
  39.             line.BrickColor=BrickColor.new("Really red")
  40.             line.Anchored=true
  41.             line.CanCollide=false
  42.             line.BottomSurface="Smooth"
  43.             line.TopSurface="Smooth"
  44.             line.Material="Neon"
  45.             j.line=line
  46.         end
  47.         local dx=j.point2.x-j.point1.x
  48.         local dy=j.point2.y-j.point1.y
  49.         local dz=j.point2.z-j.point1.z
  50.         local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
  51.         line.Size=Vector3.new(3,3,distance)
  52.         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))
  53.         *CFrame.new(0,0,-distance/2)
  54.     end
  55. end
  56. --[[local p1=createPoint(39,145,57,false)
  57. local p2=createPoint(23,140,52,false)
  58. local p3=createPoint(10,160,50,false)
  59. local p4=createPoint(45,160,60,false)
  60. local p5=createPoint(45,160,60,false)
  61. local p6=createPoint(45,160,60,false)
  62. local p7=createPoint(1,151,70,true)
  63. createJoint(p1,p2,20)
  64. createJoint(p2,p3,20)
  65. createJoint(p3,p4,20)
  66. createJoint(p1,p3,20)
  67. createJoint(p2,p4,20)
  68. createJoint(p1,p4,20)
  69. createJoint(p4,p5,20)
  70. createJoint(p5,p6,20)
  71. createJoint(p6,p7,20)]]
  72. local width=25
  73. local height=25
  74. for x=1,width do
  75.     for y=1,height do
  76.         createPoint(5*x,50-5*y,0,y==1)
  77.         if x>1 then
  78.             createJoint(points[width*(y-1)+x-1],points[width*(y-1)+x])
  79.         end
  80.         if y>1 then
  81.             print(x,y,points[width*(y-1)+x])
  82.             createJoint(points[width*(y-2)+x],points[width*(y-1)+x])
  83.         end
  84.     end
  85. end
  86. local function updatePoints()
  87.     for i=1,#points do
  88.         local p=points[i]
  89.         if not p.pinned then
  90.             local vx=(p.x-p.oldx)*friction;
  91.             local vy=-((p.y-p.oldy)*friction);
  92.             local vz=(p.z-p.oldz)*friction;
  93.             p.oldx=p.x
  94.             p.oldy=p.y
  95.             p.oldz=p.z
  96.             p.x=p.x+vx
  97.             p.y=p.y-vy
  98.             p.z=p.z+vz
  99.             p.y=p.y-gravity
  100.             if p.y<4 then
  101.                 p.y=4
  102.                 p.oldy=p.y-vy*bounce;
  103.             end
  104.         end
  105.         p.ball.CFrame=CFrame.new(p.x,p.y,p.z)
  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'.RenderStepped:connect(function()
  138.     updatePoints()
  139.     for i=1,10 do
  140.         updateSticks()
  141.     end
  142.     drawLine()
  143. end)
Advertisement
Add Comment
Please, Sign In to add comment