Vaeb

Fragmentation

May 15th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. local Plrs = game:GetService("Players")
  2. local LP = Plrs.LocalPlayer
  3. local mouse = LP:GetMouse()
  4. local cam = workspace.CurrentCamera
  5.  
  6. --define *ideal* fragmentation sizing
  7. local fragX = 4
  8. local fragY = 8
  9. local fragZ = 1
  10. ------------------------------------
  11.  
  12. local num = 0
  13.  
  14. local offCFrames = {
  15.     X = {
  16.         ["Front"] = {true, false, false};
  17.         ["Back"] = {true, false, false};
  18.         ["Right"] = {false, false, true};
  19.         ["Left"] = {false, false, true};
  20.         ["Top"] = {false, false, true};
  21.         ["Bottom"] = {false, false, true};
  22.     };
  23.     Y = {
  24.         ["Front"] = {false, false, true};
  25.         ["Back"] = {false, false, true};
  26.         ["Right"] = {true, false, false};
  27.         ["Left"] = {false, true, false};
  28.         ["Top"] = {false, false, true};
  29.         ["Bottom"] = {false, false, true};
  30.     };
  31.     Z = {
  32.         ["Front"] = {false, false, true};
  33.         ["Back"] = {false, false, true};
  34.         ["Right"] = {true, false, false};
  35.         ["Left"] = {true, false, false};
  36.         ["Top"] = {false, true, false};
  37.         ["Bottom"] = {false, true, false};
  38.     }
  39. }
  40.  
  41. local offSizes = {
  42.     X = {
  43.         ["Front"] = "x";
  44.         ["Back"] = "x";
  45.         ["Right"] = "z";
  46.         ["Left"] = "z";
  47.         ["Top"] = "x";
  48.         ["Bottom"] = "x";
  49.     };
  50.     Y = {
  51.         ["Front"] = "y";
  52.         ["Back"] = "y";
  53.         ["Right"] = "y";
  54.         ["Left"] = "y";
  55.         ["Top"] = "z";
  56.         ["Bottom"] = "z";
  57.     };
  58.     Z = {
  59.         ["Front"] = "z";
  60.         ["Back"] = "z";
  61.         ["Right"] = "x";
  62.         ["Left"] = "x";
  63.         ["Top"] = "y";
  64.         ["Bottom"] = "y";
  65.     }
  66. }
  67.  
  68. local function makePart2(CF, Size, Obj)
  69.     local Part = Instance.new("Part")
  70.     Part.Name = "new" --Using real name causes object to act as a clone sometimes. May mess up proportions slight due to meshy stuff (E.g. in Arms)
  71.     Part.Anchored = Obj.Anchored
  72.     Part.CanCollide = Obj.CanCollide
  73.     Part.TopSurface = "SmoothNoOutlines"
  74.     Part.BottomSurface = "SmoothNoOutlines"
  75.     Part.FrontSurface = "SmoothNoOutlines"
  76.     Part.BackSurface = "SmoothNoOutlines"
  77.     Part.LeftSurface = "SmoothNoOutlines"
  78.     Part.RightSurface = "SmoothNoOutlines"
  79.     Part.BrickColor = Obj.BrickColor
  80.     Part.Transparency = Obj.Transparency
  81.     Part.Material = Obj.Material
  82.     Part.Size = Size
  83.     Part.CFrame = CF
  84.  
  85.     if Part.CanCollide == false and Part.Anchored == false then
  86.         Part.Anchored = true
  87.     end
  88.  
  89.     Part.Parent = Obj.Parent
  90.     return Part
  91. end
  92.  
  93.  
  94. local function getCF(Hit, Target, Surface, fromHit)
  95.     local offsetCFrame = Target.CFrame
  96.  
  97.     if fromHit then
  98.         local TargetAng = Target.CFrame - Target.CFrame.p
  99.         offsetCFrame = CFrame.new(Hit.p) * TargetAng
  100.     end
  101.  
  102.     if Surface == "Top" then
  103.         offsetCFrame = offsetCFrame * CFrame.Angles(math.rad(-270), 0, 0)
  104.     elseif Surface == "Bottom" then
  105.         offsetCFrame = offsetCFrame * CFrame.Angles(math.rad(270), 0, 0)
  106.     elseif Surface == "Front" then
  107.         offsetCFrame = offsetCFrame
  108.     elseif Surface == "Back" then
  109.         offsetCFrame = offsetCFrame * CFrame.Angles(0, math.rad(180), 0)
  110.     elseif Surface == "Left" then
  111.         offsetCFrame = offsetCFrame * CFrame.Angles(0, math.rad(-270), 0)
  112.     elseif Surface == "Right" then
  113.         offsetCFrame = offsetCFrame * CFrame.Angles(0, math.rad(270), 0)
  114.     end
  115.     return offsetCFrame
  116. end
  117.  
  118. mouse.Button1Down:connect(function()
  119.     if mouse.Target then
  120.         local hit = mouse.Hit
  121.         local targ = mouse.Target
  122.         local surf = mouse.TargetSurface
  123.  
  124.         surf = tostring(surf):sub(15)
  125.  
  126.         local hitCF = getCF(hit, targ, surf, true)
  127.         local angCF = getCF(hit, targ, surf)
  128.         local angPos = angCF.p
  129.  
  130.         local nowSizeX = targ.Size[offSizes.X[surf]]
  131.         local nowSizeY = targ.Size[offSizes.Y[surf]]
  132.         local nowSizeZ = targ.Size[offSizes.Z[surf]]
  133.  
  134.         local objSpace = angCF:toObjectSpace(hitCF)
  135.         local DistX = objSpace.x
  136.         local DistY = objSpace.y
  137.         local DistZ = objSpace.z
  138.  
  139.         local nowCenterX = angCF * CFrame.new(DistX, 0, 0) --Hit cframe with only hit X
  140.         local nowCenterY = angCF * CFrame.new(0, DistY, 0) --Hit cframe with only hit Y
  141.         local nowCenterXY = angCF * CFrame.new(DistX, DistY, 0) --Hit cframe with only hit X and Y
  142.  
  143.         local rightCFX
  144.         local rightSizeX
  145.         local leftCFX
  146.         local leftSizeX
  147.  
  148.         --RIGHT
  149.         do
  150.             local sizeX = math.max(((nowCenterX * CFrame.new((fragX/2), 0, 0)):toObjectSpace((angCF * CFrame.new((nowSizeX/2), 0, 0)))).x, 0.2)
  151.             local sizeY = nowSizeY
  152.            
  153.             local cfX = (angCF:toObjectSpace((angCF * CFrame.new((nowSizeX/2), 0, 0)))).x - (sizeX/2)
  154.             local cfY = 0
  155.  
  156.             rightCFX = cfX
  157.             rightSizeX = sizeX
  158.  
  159.             local objSize = Vector3.new(sizeX, sizeY, nowSizeZ)
  160.             local objCF = angCF * CFrame.new(cfX, cfY, 0)
  161.  
  162.             makePart2(objCF, objSize, targ)
  163.         end
  164.  
  165.         --LEFT
  166.         do
  167.             local sizeX = math.max(((angCF * CFrame.new(-(nowSizeX/2), 0, 0)):toObjectSpace((nowCenterX * CFrame.new(-(fragX/2), 0, 0)))).x, 0.2)
  168.             local sizeY = nowSizeY
  169.            
  170.             local cfX = (angCF:toObjectSpace((angCF * CFrame.new(-(nowSizeX/2), 0, 0)))).x + (sizeX/2)
  171.             local cfY = 0
  172.  
  173.             leftCFX = cfX
  174.             leftSizeX = sizeX
  175.  
  176.             local objSize = Vector3.new(sizeX, sizeY, nowSizeZ)
  177.             local objCF = angCF * CFrame.new(cfX, cfY, 0)
  178.  
  179.             makePart2(objCF, objSize, targ)
  180.         end
  181.  
  182.         local sideSizeX = (rightCFX - (rightSizeX/2)) - (leftCFX + (leftSizeX/2))
  183.         local sideCFX = ((rightCFX - (rightSizeX/2)) + (leftCFX + (leftSizeX/2)))/2
  184.  
  185.         --TOP
  186.         do
  187.             local sizeX = sideSizeX
  188.             local sizeY = math.max(((nowCenterY * CFrame.new(0, (fragY/2), 0)):toObjectSpace((angCF * CFrame.new(0, (nowSizeY/2), 0)))).y, 0.2)
  189.            
  190.             local cfX = sideCFX
  191.             local cfY = (angCF:toObjectSpace((angCF * CFrame.new(0, (nowSizeY/2), 0)))).y - (sizeY/2)
  192.  
  193.             local objSize = Vector3.new(sizeX, sizeY, nowSizeZ)
  194.             local objCF = angCF * CFrame.new(cfX, cfY, 0)
  195.  
  196.             makePart2(objCF, objSize, targ)
  197.         end
  198.  
  199.         --BOTTOM
  200.         do
  201.             local sizeX = sideSizeX
  202.             local sizeY = math.max(((angCF * CFrame.new(0, -(nowSizeY/2), 0)):toObjectSpace((nowCenterY * CFrame.new(0, -(fragY/2), 0)))).y, 0.2)
  203.            
  204.             local cfX = sideCFX
  205.             local cfY = (angCF:toObjectSpace((angCF * CFrame.new(0, -(nowSizeY/2), 0)))).y + (sizeY/2)
  206.  
  207.             local objSize = Vector3.new(sizeX, sizeY, nowSizeZ)
  208.             local objCF = angCF * CFrame.new(cfX, cfY, 0)
  209.  
  210.             makePart2(objCF, objSize, targ)
  211.         end
  212.  
  213.         targ:Destroy()
  214.     end
  215. end)
  216.  
  217. --Right
  218. --SizeX: (ObjCFX + 1/2ObjWidth) - (MouseCFX + 1/2FragX)
  219. --SizeY: ObjSizeY
  220. --CFX: ObjCFX + 1/2ObjWidth - 1/2RightHeight
  221. --CFY: ObjCFY
  222.  
  223. --Left
  224. --SizeX: (MouseCFX - 1/2FragX) - (ObjCFX - 1/2ObjWidth)
  225. --SizeY: ObjSizeY
  226. --CFX: ObjCFX - 1/2ObjWidth + 1/2RightHeight
  227. --CFY: ObjCFY
  228.  
  229. --Top
  230. --SizeX: FragX
  231. --SizeY: (ObjCFY + 1/2ObjHeight) - (MouseCFY + 1/2FragY)
  232. --CFX: MouseCFX
  233. --CFY: ObjCFY + 1/2ObjHeight - 1/2TopHeight
  234.  
  235. --Bottom
  236. --SizeX: FragX
  237. --SizeY: (MouseCFY - 1/2FragY) - (ObjCFY - 1/2ObjHeight)
  238. --CFX: MouseCFX
  239. --CFY: ObjCFY - 1/2ObjHeight + 1/2TopHeight
Add Comment
Please, Sign In to add comment