Advertisement
1m1m0

Blackhole v5.0 Spring-based Grav Experiment

Feb 22nd, 2025
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | Source Code | 0 0
  1. -- [ Blackhole Alpha Experimental ] --
  2. -- Spring-based gravity: Improved Stability, Performance, and Behavioral Control
  3. -- WARNING: Orbits are a bit whacky and INACCURATE!!! It's experimental, don't expect too much.
  4.  
  5. local blackHole = script.Parent          -- Reference to your black hole part
  6. local gravitationalStrength = 100        -- Controls the strength of the attraction
  7. local dampingFactor = 0                  -- Controls oscillations (0 for no damping)
  8. local detectionRange = 500               -- Range within which objects are affected
  9. local innerEventHorizon = 1              -- Factor in which springs stop pulling, but are still present
  10. local visibleConstraints = true          -- View active gravitations
  11. local absorption = true                  -- Absorb objects on contact when true
  12. local toggleMicroGravity = true          -- Disable gravity manually
  13.  
  14. -- Table to keep track of active springs and attachments
  15. local activeSprings = {}
  16. local blackHoleAttachment = Instance.new("Attachment")
  17. blackHoleAttachment.Parent = blackHole
  18.  
  19. -- Function to apply gravity to an object
  20. local function ApplyGravity(object)
  21.     -- Check if the object already has a spring attached
  22.     if activeSprings[object] then return end
  23.  
  24.     -- Create an attachment on the object
  25.     local objectAttachment = Instance.new("Attachment")
  26.     objectAttachment.Parent = object
  27.  
  28.     -- Calculate distance from the black hole
  29.     local distance = (blackHole.Position - object.Position).Magnitude
  30.  
  31.     -- Create a SpringConstraint between the black hole and the object
  32.     local spring = Instance.new("SpringConstraint")
  33.     spring.Attachment0 = blackHoleAttachment
  34.     spring.Attachment1 = objectAttachment
  35.     spring.FreeLength = 0                    -- Desired length of the spring (zero for constant pull)
  36.     spring.Stiffness = gravitationalStrength
  37.     spring.Damping = dampingFactor
  38.     spring.LimitsEnabled = true              -- Enable limits on the spring's length
  39.     spring.MaxLength = distance              -- Set MaxLength to current displacement
  40.     spring.MinLength = blackHole.Size.X * (innerEventHorizon * 0.1)
  41.     if visibleConstraints then
  42.         spring.Visible = true
  43.         spring.Coils = 0
  44.         spring.Color = BrickColor.new("Institutional white")
  45.     end
  46.     spring.Parent = blackHole
  47.  
  48.     -- Keep track of the spring and attachment
  49.     activeSprings[object] = {
  50.         spring = spring,
  51.         attachment = objectAttachment
  52.     }
  53. end
  54.  
  55. -- Function to remove gravity from an object
  56. local function RemoveGravity(object)
  57.     -- Remove the spring and attachment if they exist
  58.     local data = activeSprings[object]
  59.     if data then
  60.         data.spring:Destroy()
  61.         data.attachment:Destroy()
  62.         activeSprings[object] = nil
  63.     end
  64. end
  65.  
  66. -- Function to absorb an object
  67. local function AbsorbObject(object)
  68.     RemoveGravity(object)
  69.     object:Destroy()
  70.     -- Optional: Add any absorption effects or sounds here
  71. end
  72.  
  73. -- Event listener for when an object touches the black hole
  74. if absorption then
  75.     blackHole.Touched:Connect(function(hit)
  76.         -- Ensure the object is a BasePart and not anchored
  77.         if hit:IsA("BasePart") and not hit.Anchored and hit ~= blackHole then
  78.             AbsorbObject(hit)
  79.         end
  80.     end)
  81. end
  82.  
  83. if toggleMicroGravity then
  84.     workspace.Gravity = 0
  85. end
  86.  
  87. -- Heartbeat event to apply gravity to unanchored objects within range
  88. game:GetService("RunService").Heartbeat:Connect(function()
  89.     for _, object in pairs(workspace:GetDescendants()) do
  90.         if object:IsA("BasePart") and object ~= blackHole and not object.Anchored then
  91.             local distance = (blackHole.Position - object.Position).Magnitude
  92.             if distance <= detectionRange then
  93.                 ApplyGravity(object)
  94.             else
  95.                 RemoveGravity(object)
  96.             end
  97.         elseif activeSprings[object] then
  98.             -- Remove gravity from anchored objects that were previously unanchored
  99.             RemoveGravity(object)
  100.         end
  101.     end
  102. end)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement