Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [ Blackhole Alpha Experimental ] --
- -- Spring-based gravity: Improved Stability, Performance, and Behavioral Control
- -- WARNING: Orbits are a bit whacky and INACCURATE!!! It's experimental, don't expect too much.
- local blackHole = script.Parent -- Reference to your black hole part
- local gravitationalStrength = 100 -- Controls the strength of the attraction
- local dampingFactor = 0 -- Controls oscillations (0 for no damping)
- local detectionRange = 500 -- Range within which objects are affected
- local innerEventHorizon = 1 -- Factor in which springs stop pulling, but are still present
- local visibleConstraints = true -- View active gravitations
- local absorption = true -- Absorb objects on contact when true
- local toggleMicroGravity = true -- Disable gravity manually
- -- Table to keep track of active springs and attachments
- local activeSprings = {}
- local blackHoleAttachment = Instance.new("Attachment")
- blackHoleAttachment.Parent = blackHole
- -- Function to apply gravity to an object
- local function ApplyGravity(object)
- -- Check if the object already has a spring attached
- if activeSprings[object] then return end
- -- Create an attachment on the object
- local objectAttachment = Instance.new("Attachment")
- objectAttachment.Parent = object
- -- Calculate distance from the black hole
- local distance = (blackHole.Position - object.Position).Magnitude
- -- Create a SpringConstraint between the black hole and the object
- local spring = Instance.new("SpringConstraint")
- spring.Attachment0 = blackHoleAttachment
- spring.Attachment1 = objectAttachment
- spring.FreeLength = 0 -- Desired length of the spring (zero for constant pull)
- spring.Stiffness = gravitationalStrength
- spring.Damping = dampingFactor
- spring.LimitsEnabled = true -- Enable limits on the spring's length
- spring.MaxLength = distance -- Set MaxLength to current displacement
- spring.MinLength = blackHole.Size.X * (innerEventHorizon * 0.1)
- if visibleConstraints then
- spring.Visible = true
- spring.Coils = 0
- spring.Color = BrickColor.new("Institutional white")
- end
- spring.Parent = blackHole
- -- Keep track of the spring and attachment
- activeSprings[object] = {
- spring = spring,
- attachment = objectAttachment
- }
- end
- -- Function to remove gravity from an object
- local function RemoveGravity(object)
- -- Remove the spring and attachment if they exist
- local data = activeSprings[object]
- if data then
- data.spring:Destroy()
- data.attachment:Destroy()
- activeSprings[object] = nil
- end
- end
- -- Function to absorb an object
- local function AbsorbObject(object)
- RemoveGravity(object)
- object:Destroy()
- -- Optional: Add any absorption effects or sounds here
- end
- -- Event listener for when an object touches the black hole
- if absorption then
- blackHole.Touched:Connect(function(hit)
- -- Ensure the object is a BasePart and not anchored
- if hit:IsA("BasePart") and not hit.Anchored and hit ~= blackHole then
- AbsorbObject(hit)
- end
- end)
- end
- if toggleMicroGravity then
- workspace.Gravity = 0
- end
- -- Heartbeat event to apply gravity to unanchored objects within range
- game:GetService("RunService").Heartbeat:Connect(function()
- for _, object in pairs(workspace:GetDescendants()) do
- if object:IsA("BasePart") and object ~= blackHole and not object.Anchored then
- local distance = (blackHole.Position - object.Position).Magnitude
- if distance <= detectionRange then
- ApplyGravity(object)
- else
- RemoveGravity(object)
- end
- elseif activeSprings[object] then
- -- Remove gravity from anchored objects that were previously unanchored
- RemoveGravity(object)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement