Advertisement
Thefrozen106

Rainbow Cape

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