Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local next=next
- local gravity=0.09
- local friction=0.999
- local bounce=0.99
- local function dist(p0,p1)
- local dx=p1.CFrame.x-p0.CFrame.x
- local dy=p1.CFrame.y-p0.CFrame.y
- local dz=p1.CFrame.z-p0.CFrame.z
- return math.sqrt(dx*dx+dy*dy+dz*dz)
- end
- local points={}
- local joints={}
- local function createPoint(x,y,z,pinned)
- local ball=Instance.new("Part",workspace)
- ball.Shape="Ball"
- ball.Name=tostring(#points+1)
- ball.Size=Vector3.new(5,5,5)
- ball.CanCollide=false
- ball.Anchored=true
- ball.BottomSurface=Enum.SurfaceType.Smooth
- ball.TopSurface=Enum.SurfaceType.Smooth
- ball.BrickColor=BrickColor.new("Really red")
- ball.Material = "Neon"
- ball.CFrame=CFrame.new(x,y,z)
- local t = {x=x,y=y,z=z,oldx=x-0.4,oldy=y-0.4,oldz=z-0.4,ball=ball,pinned=pinned}
- points[#points+1]=t
- return t
- end
- local function createJoint(point1,point2,distance,line)
- local t = {point1=point1,point2=point2,distance=distance,line=line}
- table.insert(joints,t)
- end
- local function drawLine()
- for i=1,#joints do
- local j=joints[i]
- local line=j.line
- if not line then
- line=Instance.new("Part",workspace)
- line.BrickColor=BrickColor.new("Really red")
- line.Anchored=true
- line.CanCollide=false
- line.BottomSurface="Smooth"
- line.TopSurface="Smooth"
- line.Material="Neon"
- j.line=line
- end
- local dx=j.point2.x-j.point1.x
- local dy=j.point2.y-j.point1.y
- local dz=j.point2.z-j.point1.z
- local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
- line.Size=Vector3.new(3,3,distance)
- 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))
- *CFrame.new(0,0,-distance/2)
- end
- end
- --[[local p1=createPoint(39,145,57,false)
- local p2=createPoint(23,140,52,false)
- local p3=createPoint(10,160,50,false)
- local p4=createPoint(45,160,60,false)
- local p5=createPoint(45,160,60,false)
- local p6=createPoint(45,160,60,false)
- local p7=createPoint(1,151,70,true)
- createJoint(p1,p2,20)
- createJoint(p2,p3,20)
- createJoint(p3,p4,20)
- createJoint(p1,p3,20)
- createJoint(p2,p4,20)
- createJoint(p1,p4,20)
- createJoint(p4,p5,20)
- createJoint(p5,p6,20)
- createJoint(p6,p7,20)]]
- local width=25
- local height=25
- for x=1,width do
- for y=1,height do
- createPoint(5*x,50-5*y,0,y==1)
- if x>1 then
- createJoint(points[width*(y-1)+x-1],points[width*(y-1)+x])
- end
- if y>1 then
- print(x,y,points[width*(y-1)+x])
- createJoint(points[width*(y-2)+x],points[width*(y-1)+x])
- end
- end
- end
- local function updatePoints()
- for i=1,#points do
- local p=points[i]
- if not p.pinned then
- local vx=(p.x-p.oldx)*friction;
- local vy=-((p.y-p.oldy)*friction);
- local vz=(p.z-p.oldz)*friction;
- p.oldx=p.x
- p.oldy=p.y
- p.oldz=p.z
- p.x=p.x+vx
- p.y=p.y-vy
- p.z=p.z+vz
- p.y=p.y-gravity
- if p.y<4 then
- p.y=4
- p.oldy=p.y-vy*bounce;
- end
- end
- p.ball.CFrame=CFrame.new(p.x,p.y,p.z)
- end
- end
- local function updateSticks()
- for i=1,#joints do
- local j=joints[i]
- local dx=j.point2.x-j.point1.x
- local dy=j.point2.y-j.point1.y
- local dz=j.point2.z-j.point1.z
- local distance=math.sqrt(dx*dx+dy*dy+dz*dz)
- local difference=j.distance-distance
- local percent=difference/distance/2
- local offsetX=dx*percent;
- local offsetY=dy*percent;
- local offsetZ=dz*percent;
- if j.point1.pinned or j.point2.pinned then
- offsetX=offsetX*2
- offsetY=offsetY*2
- offsetZ=offsetZ*2
- end
- if not j.point1.pinned then
- j.point1.x=j.point1.x-offsetX
- j.point1.y=j.point1.y-offsetY
- j.point1.z=j.point1.z-offsetZ
- end
- if not j.point2.pinned then
- j.point2.x=j.point2.x+offsetX
- j.point2.y=j.point2.y+offsetY
- j.point2.z=j.point2.z+offsetZ
- end
- end
- end
- game:service'RunService'.RenderStepped:connect(function()
- updatePoints()
- for i=1,10 do
- updateSticks()
- end
- drawLine()
- end)
Advertisement
Add Comment
Please, Sign In to add comment