Advertisement
ohhhhhhshdhashdahsd

RainCape

May 21st, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. local verlet = {}
  2. verlet.step_time = 1 / 50
  3. verlet.gravity = Vector3.new(0, -10, 0)
  4.  
  5. local char = game.Players.LocalPlayer.Character
  6. local torso = char:WaitForChild("Torso")
  7. local parts = {}
  8. local render = game:GetService("RunService").RenderStepped
  9.  
  10. wait(2)
  11.  
  12. local point = {}
  13. local link = {}
  14. local rope = {}
  15.  
  16. local function ccw(A,B,C)
  17. return (C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x)
  18. end
  19.  
  20. local function intersect(A,B,C,D)
  21. return ccw(A,C,D) ~= ccw(B,C,D) and ccw(A,B,C) ~= ccw(A,B,D)
  22. end
  23.  
  24. local function vec2(v)
  25. return Vector2.new(v.x, v.z)
  26. end
  27.  
  28. function point:step()
  29. if not self.fixed then
  30. local derivative = (self.position - self.last_position) * 0.95
  31. self.last_position = self.position
  32. self.position = self.position + derivative + (self.velocity * verlet.step_time ^ 2)
  33. --[[local torsoP = torso.CFrame * CFrame.new(-1, 0, 0.5)
  34. local torsoE = torso.CFrame * CFrame.new(1, 0, 0.5)
  35. local pointE = self.position + torso.CFrame.lookVector * 100
  36. local doIntersect = intersect(vec2(torsoP.p), vec2(torsoE.p), vec2(self.position), vec2(pointE))
  37. if not doIntersect then
  38. self.postition = self.position - torso.CFrame.lookVector * 10
  39. end]]
  40. end
  41. end
  42.  
  43. function link:step()
  44. for i = 1, 1 do
  45. local distance = self.point1.position - self.point2.position
  46. local magnitude = distance.magnitude
  47. local differance = (self.length - magnitude) / magnitude
  48. local translation = ((self.point1.fixed or self.point2.fixed) and 1 or 0.6) * distance * differance
  49. if not self.point1.fixed then
  50. self.point1.position = self.point1.position + translation
  51. end
  52. if not self.point2.fixed then
  53. self.point2.position = self.point2.position - translation
  54. end
  55. end
  56. end
  57.  
  58. function verlet.new(class, a, b, c)
  59. if class == "Point" then
  60. local new = {}
  61. setmetatable(new, {__index = point})
  62. new.class = class
  63. new.position = a or Vector3.new()
  64. new.last_position = new.position
  65. new.velocity = verlet.gravity
  66. new.fixed = false
  67. return new
  68. elseif class == "Link" then
  69. local new = {}
  70. setmetatable(new, {__index = link})
  71. new.class = class
  72. new.point1 = a
  73. new.point2 = b
  74. new.length = c or (a.position - b.position).magnitude
  75. return new
  76. elseif class == "Rope" then
  77. local new = {}
  78. setmetatable(new, {__index = link})
  79. new.class = class
  80. new.start_point = a
  81. new.finish_point = b
  82. new.points = {}
  83. new.links = {}
  84. local inc = (b - a) / 10
  85. for i = 0, 10 do
  86. table.insert(new.points, verlet.new("Point", a + (i * inc)))
  87. end
  88. for i = 2, #new.points do
  89. table.insert(new.links, verlet.new("Link", new.points[i - 1], new.points[i]))
  90. end
  91. return new
  92. end
  93. end
  94.  
  95. local tris = {}
  96. local triParts = {}
  97.  
  98. local function GetDiscoColor(hue)
  99. local section = hue % 1 * 3
  100. local secondary = 0.5 * math.pi * (section % 1)
  101. if section < 1 then
  102. return Color3.new(1, 1 - math.cos(secondary), 1 - math.sin(secondary))
  103. elseif section < 2 then
  104. return Color3.new(1 - math.sin(secondary), 1, 1 - math.cos(secondary))
  105. else
  106. return Color3.new(1 - math.cos(secondary), 1 - math.sin(secondary), 1)
  107. end
  108. end
  109.  
  110. local function setupPart(part)
  111. part.Anchored = true
  112. part.FormFactor = 3
  113. part.CanCollide = false
  114. part.TopSurface = 10
  115. part.BottomSurface = 10
  116. part.LeftSurface = 10
  117. part.RightSurface = 10
  118. part.FrontSurface = 10
  119. part.BackSurface = 10
  120. part.Material = "Neon"
  121. local m = Instance.new("SpecialMesh", part)
  122. m.MeshType = "Wedge"
  123. m.Scale = Vector3.new(0.2, 1, 1)
  124. return part
  125. end
  126.  
  127. local function CFrameFromTopBack(at, top, back)
  128. local right = top:Cross(back)
  129. return CFrame.new(at.x, at.y, at.z, right.x, top.x, back.x, right.y, top.y, back.y, right.z, top.z, back.z)
  130. end
  131.  
  132. local function drawTri(parent, a, b, c)
  133. local this = {}
  134. local mPart1 = table.remove(triParts, 1) or setupPart(Instance.new("Part"))
  135. local mPart2 = table.remove(triParts, 1) or setupPart(Instance.new("Part"))
  136. function this:Set(a, b, c)
  137. local ab, bc, ca = b-a, c-b, a-c
  138. local abm, bcm, cam = ab.magnitude, bc.magnitude, ca.magnitude
  139. local edg1 = math.abs(0.5 + ca:Dot(ab)/(abm*abm))
  140. local edg2 = math.abs(0.5 + ab:Dot(bc)/(bcm*bcm))
  141. local edg3 = math.abs(0.5 + bc:Dot(ca)/(cam*cam))
  142. if edg1 < edg2 then
  143. if edg1 >= edg3 then
  144. a, b, c = c, a, b
  145. ab, bc, ca = ca, ab, bc
  146. abm = cam
  147. end
  148. else
  149. if edg2 < edg3 then
  150. a, b, c = b, c, a
  151. ab, bc, ca = bc, ca, ab
  152. abm = bcm
  153. else
  154. a, b, c = c, a, b
  155. ab, bc, ca = ca, ab, bc
  156. abm = cam
  157. end
  158. end
  159.  
  160. local len1 = -ca:Dot(ab)/abm
  161. local len2 = abm - len1
  162. local width = (ca + ab.unit*len1).magnitude
  163.  
  164. local maincf = CFrameFromTopBack(a, ab:Cross(bc).unit, -ab.unit)
  165.  
  166. if len1 > 0.2 then
  167. mPart1.Parent = parent
  168. mPart1.Size = Vector3.new(0.2, width, len1)
  169. mPart1.CFrame = maincf*CFrame.Angles(math.pi,0,math.pi/2)*CFrame.new(0,width/2,len1/2)
  170. else
  171. mPart1.Parent = nil
  172. end
  173.  
  174. if len2 > 0.2 then
  175. mPart2.Parent = parent
  176. mPart2.Size = Vector3.new(0.2, width, len2)
  177. mPart2.CFrame = maincf*CFrame.Angles(math.pi,math.pi,-math.pi/2)*CFrame.new(0,width/2,-len1 - len2/2)
  178. else
  179. mPart2.Parent = nil
  180. end
  181. end
  182. function this:SetProperty(prop, value)
  183. mPart1[prop] = value
  184. mPart2[prop] = value
  185. end
  186. this:Set(a, b, c)
  187. function this:Destroy()
  188. mPart1:Destroy()
  189. mPart2:Destroy()
  190. end
  191. this.p1 = mPart1
  192. this.p2 = mPart2
  193. this.p1.BrickColor = BrickColor.new(GetDiscoColor(math.noise(0.5, 0.5, this.p1.CFrame.Y * 0.5 + time())))
  194. this.p2.BrickColor = BrickColor.new(GetDiscoColor(math.noise(0.5, 0.5, this.p2.CFrame.Y * 0.5 + time())))
  195. return this
  196. end
  197.  
  198. function verlet.draw(object, id)
  199. if object.class == "Point" then
  200. local part = parts[id]
  201. part.BrickColor = BrickColor.new(1, 1, 1)
  202. part.Transparency = 0
  203. part.formFactor = 3
  204. part.Anchored = true
  205. part.CanCollide = true
  206. part.TopSurface = 0
  207. part.BottomSurface = 0
  208. part.Size = Vector3.new(0.35, 0.35, 0.35)
  209. part.Material = "Neon"
  210. part.CFrame = CFrame.new(object.position)
  211. part.Parent = torso
  212. return part
  213. elseif object.class == "Link" then
  214. local part = parts[id]
  215. local dist = (object.point1.position - object.point2.position).magnitude
  216. part.Size = Vector3.new(0.2, 0.2, dist)
  217. part.CFrame = CFrame.new(object.point1.position, object.point2.position) * CFrame.new(0, 0, dist * -0.5)
  218. part.Parent = torso
  219. return part
  220. end
  221. end
  222.  
  223. function verlet.clear()
  224. for _, v in pairs(workspace:GetChildren()) do
  225. if v.Name == "Part" then
  226. v:Destroy()
  227. end
  228. end
  229. end
  230.  
  231. local points = {}
  232. local links = {}
  233.  
  234. for x = 0, 2 do
  235. points[x] = {}
  236. for y = 0, 3 do
  237. points[x][y] = verlet.new("Point", torso.Position + Vector3.new(x * 0.8 - 2, 2 - y * 0.8, 5 + y * 0.4))
  238. points[x][y].fixed = y == 0
  239. end
  240. end
  241.  
  242. for x = 1, 2 do
  243. for y = 0, 3 do
  244. links[#links + 1] = verlet.new("Link", points[x][y], points[x - 1][y], 1 + y * 0.08)
  245. end
  246. end
  247.  
  248. for x = 0, 2 do
  249. for y = 1, 3 do
  250. links[#links + 1] = verlet.new("Link", points[x][y], points[x][y - 1], 1.2 + y * 0.03)
  251. end
  252. end
  253.  
  254. render:connect(function()
  255. for x = 0, 2 do
  256. for y = 0, 3 do
  257. if y == 0 then
  258. points[x][y].position = (torso.CFrame * CFrame.new(x * 1 - 1, 1, 0.5)).p
  259. else
  260. points[x][y]:step()
  261. end
  262. end
  263. end
  264. for i = 1, #links do
  265. links[i]:step()
  266. end
  267. for i = 1, #tris do
  268. triParts[#triParts + 1] = tris[i].p1
  269. triParts[#triParts + 1] = tris[i].p2
  270. end
  271. tris = {}
  272. for x = 1, 2 do
  273. for y = 1, 3 do
  274. tris[#tris + 1] = drawTri(torso, points[x - 1][y - 1].position, points[x - 1][y].position, points[x][y - 1].position)
  275. tris[#tris + 1] = drawTri(torso, points[x][y].position, points[x - 1][y].position, points[x][y - 1].position)
  276. end
  277. end
  278. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement