Advertisement
1m1m0

Blackhole v5.0 Constraint grav Experiment

Feb 22nd, 2025 (edited)
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.00 KB | Source Code | 0 0
  1. -- [ Blackhole Alpha Experimental ] --
  2. -- Spring-based, Rope-based, Line-based, VectorForce-based, and LinearVelocity-based (velocity) gravity:
  3. -- Improved Stability, Performance, and Behavioral Control
  4. -- WARNING: Orbits are a bit whacky and INACCURATE!!! It's experimental, don't expect too much.
  5. -- More details in 'v5A_Documentation_READ' section V --
  6. local blackHole = script.Parent         -- Reference
  7. local gravitationalStrength = 100       -- Controls the strength of the attraction
  8. local dampingFactor = 0                 -- Controls oscillations/elasticity
  9. local detectionRange = 500              -- Range within which objects are affected
  10. local innerEventHorizon = 0             -- Factor in which springs stop pulling, but are still present
  11. local forceCap = 2000                   -- The maximum force in which object cap at using line (when `realisticGravity` is true) and linear
  12. local visibleConstraints = false        -- View active gravitations (forces and velocities do not support visual properties in all cases)
  13. local centerOfMassBased = true          -- Toggle to apply forces to the center of masses
  14. local absorption = true                 -- Absorb objects on contact when true
  15. local realisticGravity = false          -- Toggle inverse square law (weaker gravity the further it is)
  16. local toggleMicroGravity = true         -- Disable gravity manually
  17. local cameraDistortion = true           -- Toggle camera effects whenever within `detectionRange` (Only works with Run, may cause nausea!)
  18. local distortThreshold = 0.01           -- Factor (percentage) of the range to distort FOV
  19. -- "spring" (or "1"), "rope" (or "2"), "line" (or "3"), "vector" (or "4"), "linear" (or "5")
  20. -- linear subgroup - "linear1" or "5.1" for line velocity, and "linear2" or "5.2" for vector velocity}
  21. local constraintMode = "linear1" -- "linear1" is velocity-based gravity with MODEL SUPPORT! :D
  22. -- ADVANCED EXPERIMENTAL --
  23. local gravitationMode = "universal"     -- Choose between "selective" (specific objects) and "universal" (everything) to be pulled in
  24. local attractionTag = "attractable"     -- The tag that objects must have when in selective mode
  25. -- Disables other gravitational scripts to prevent conflictions
  26. if script.Enabled then
  27.     blackHole.BHv5_ControlPanel.Enabled = false
  28.     blackHole.BHv5_Alpha_MAIN.Enabled = false
  29. end
  30. local camera = workspace.Camera -- Reference for `cameraDistortion`
  31. -- Table to keep track of active constraints and attachments
  32. local activeConstraints = {}
  33. local blackHoleAttachment = Instance.new("Attachment")
  34. blackHoleAttachment.Parent = blackHole
  35. -- Function to apply gravity to an object
  36. local function ApplyGravity(object)
  37.     -- Check if the object already has a constraint attached
  38.     if activeConstraints[object] then return end
  39.     -- Create an attachment on the object
  40.     local objectAttachment = Instance.new("Attachment")
  41.     objectAttachment.Parent = object
  42.     -- Calculate distance from the black hole
  43.     local distance = (blackHole.Position - object.Position).Magnitude
  44.     if constraintMode == "spring" or constraintMode == "1" then
  45.         local spring = Instance.new("SpringConstraint")
  46.         spring.Attachment0 = blackHoleAttachment
  47.         spring.Attachment1 = objectAttachment
  48.         spring.FreeLength = 0
  49.         spring.Stiffness = gravitationalStrength
  50.         spring.Damping = dampingFactor
  51.         spring.LimitsEnabled = true
  52.         spring.MaxLength = detectionRange
  53.         spring.MinLength = blackHole.Size.X * (innerEventHorizon * 0.1)
  54.         if visibleConstraints then
  55.             spring.Visible = true
  56.             spring.Coils = 8
  57.             spring.Color = BrickColor.new("Institutional white")
  58.         end
  59.         spring.Parent = script.ConstraintStorage
  60.         activeConstraints[object] = { constraint = spring, attachment = objectAttachment }
  61.     elseif constraintMode == "rope" or constraintMode == "2" then
  62.         local rope = Instance.new("RopeConstraint")
  63.         rope.Attachment0 = blackHoleAttachment
  64.         rope.Attachment1 = objectAttachment
  65.         rope.Length = distance
  66.         rope.Visible = visibleConstraints
  67.         rope.Color = BrickColor.new("Institutional white")
  68.         rope.WinchEnabled = true
  69.         rope.WinchForce = gravitationalStrength * 10
  70.         rope.WinchSpeed = math.huge
  71.         rope.WinchTarget = 0
  72.         if dampingFactor >= 1 then
  73.             rope.Restitution = 1
  74.         elseif dampingFactor <= 0 then
  75.             rope.Restitution = 0
  76.         end
  77.         rope.Parent = script.ConstraintStorage
  78.         activeConstraints[object] = { constraint = rope, attachment = objectAttachment }
  79.     elseif constraintMode == "line" or constraintMode == "3" then
  80.         local lineForce = Instance.new("LineForce")
  81.         lineForce.Attachment0 = objectAttachment
  82.         lineForce.Attachment1 = blackHoleAttachment
  83.         lineForce.ApplyAtCenterOfMass = centerOfMassBased
  84.         lineForce.Magnitude = gravitationalStrength  -- initial value
  85.         if realisticGravity then
  86.             lineForce.InverseSquareLaw = true
  87.             lineForce.Magnitude = gravitationalStrength ^ 3
  88.             lineForce.MaxForce = forceCap ^ 2
  89.         end
  90.         if visibleConstraints then
  91.             lineForce.Visible = true
  92.             lineForce.Color = BrickColor.new("Institutional white")
  93.         end
  94.         lineForce.Parent = object
  95.         activeConstraints[object] = { constraint = lineForce, attachment = objectAttachment }
  96.     elseif constraintMode == "vector" or constraintMode == "4" then
  97.         -- Use VectorForce (force-based velocity)
  98.         local vectorForce = Instance.new("VectorForce")
  99.         vectorForce.Attachment0 = objectAttachment
  100.         vectorForce.RelativeTo = Enum.ActuatorRelativeTo.Attachment1
  101.         local initialDir = (blackHole.Position - object.Position).Unit
  102.         if realisticGravity then
  103.             vectorForce.Force = initialDir * (gravitationalStrength ^ 3)
  104.         else
  105.             vectorForce.Force = initialDir * gravitationalStrength
  106.         end
  107.         vectorForce.ApplyAtCenterOfMass = centerOfMassBased
  108.         vectorForce.Parent = object
  109.         activeConstraints[object] = { constraint = vectorForce, attachment = objectAttachment }
  110.     elseif constraintMode == "linear1" or constraintMode == "5.1"
  111.         or constraintMode == "linear2" or constraintMode == "5.2" then
  112.         local linearVel = Instance.new("LinearVelocity")
  113.         linearVel.Attachment0 = objectAttachment
  114.         linearVel.RelativeTo = Enum.ActuatorRelativeTo.World
  115.         linearVel.MaxForce = forceCap
  116.         if constraintMode == "linear1" or constraintMode == "5.1" then
  117.             -- Line velocity mode
  118.             linearVel.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
  119.             local lineDir = (blackHole.Position - object.Position).Unit
  120.             linearVel.LineDirection = lineDir    -- Initial line direction
  121.             -- Set the line velocity (magnitude) to gravitationalStrength
  122.             linearVel.LineVelocity = (gravitationalStrength * 10) / (distance ^ 2) -- Distance squared
  123.         elseif constraintMode == "linear2" or constraintMode == "5.2" then
  124.             -- Vector velocity mode
  125.             linearVel.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
  126.             local velocityMagnitude = (distance > 0) and (gravitationalStrength / distance) or 0
  127.             local initialDir = (blackHole.Position - object.Position).Unit
  128.             linearVel.VectorVelocity = initialDir * velocityMagnitude
  129.         end
  130.         linearVel.Parent = object
  131.         activeConstraints[object] = { constraint = linearVel, attachment = objectAttachment }
  132.     end
  133. end
  134. -- Function to remove gravity from an object
  135. local function RemoveGravity(object)
  136.     local data = activeConstraints[object]
  137.     if data then
  138.         data.constraint:Destroy()
  139.         data.attachment:Destroy()
  140.         activeConstraints[object] = nil
  141.     end
  142. end
  143. -- Function to absorb an object
  144. local function AbsorbObject(object)
  145.     RemoveGravity(object)
  146.     object:Destroy()
  147. end
  148. -- Event listener for when an object touches the black hole
  149. if absorption then
  150.     blackHole.Touched:Connect(function(hit)
  151.         if hit:IsA("BasePart") and not hit.Anchored and hit ~= blackHole then
  152.             AbsorbObject(hit)
  153.         end
  154.     end)
  155. end
  156. if toggleMicroGravity then
  157.     workspace.Gravity = 0
  158. end
  159. -- Heartbeat event to enforce gravity on unanchored objects within range
  160. local CollectionService = game:GetService("CollectionService")
  161. game:GetService("RunService").Heartbeat:Connect(function()
  162.     -- Existing gravity logic:
  163.     for _, object in pairs(workspace:GetDescendants()) do
  164.         if object:IsA("BasePart") and object ~= blackHole and not object.Anchored then
  165.             local distance = (blackHole.Position - object.Position).Magnitude
  166.             -- Check if the object has the required tag
  167.             local hasTag = CollectionService:HasTag(object, attractionTag)
  168.             if gravitationMode == "selective" and hasTag and distance <= detectionRange then
  169.                 ApplyGravity(object)
  170.             elseif gravitationMode == "universal" and distance <= detectionRange then
  171.                 ApplyGravity(object)
  172.             else
  173.                 RemoveGravity(object)
  174.             end
  175.         elseif activeConstraints[object] then
  176.             RemoveGravity(object)
  177.         end
  178.     end
  179.     -- Update any active velocity-based constraints so the effect always points toward the black hole
  180.     for object, data in pairs(activeConstraints) do
  181.         if object:IsDescendantOf(workspace) then
  182.             local currentDistance = (blackHole.Position - object.Position).Magnitude
  183.             if currentDistance <= detectionRange then
  184.                 local newDir = (blackHole.Position - object.Position).Unit
  185.                 if data.constraint:IsA("VectorForce") then
  186.                     if realisticGravity then
  187.                         data.constraint.Force = newDir * (gravitationalStrength ^ 3)
  188.                     else
  189.                         data.constraint.Force = newDir * gravitationalStrength
  190.                     end
  191.                 elseif data.constraint:IsA("LinearVelocity") then
  192.                     if data.constraint.VelocityConstraintMode == Enum.VelocityConstraintMode.Line then
  193.                         -- "linear1" / "5.1": update LineDirection and set constant LineVelocity
  194.                         data.constraint.LineDirection = newDir
  195.                         data.constraint.LineVelocity = gravitationalStrength
  196.                     else
  197.                         -- "linear2" / "5.2": update VectorVelocity based on gravity divided by distance
  198.                         local newVelocityMag = (currentDistance > 0) and (gravitationalStrength / currentDistance) or 0
  199.                         data.constraint.VectorVelocity = newDir * newVelocityMag
  200.                     end
  201.                 elseif data.constraint:IsA("LineForce") then
  202.                     if realisticGravity then
  203.                         data.constraint.Magnitude = gravitationalStrength ^ 3
  204.                     else
  205.                         data.constraint.Magnitude = gravitationalStrength
  206.                     end
  207.                 end
  208.             end
  209.         end
  210.     end
  211.     -- Camera Distortion Update:
  212.     if cameraDistortion and camera then
  213.         local camPos = camera.CFrame.Position
  214.         local distance = (blackHole.Position - camPos).Magnitude
  215.         local newFOV
  216.         if distance >= detectionRange then
  217.             newFOV = 70 -- Default FOV, but feel free to change it
  218.         elseif distance <= detectionRange * distortThreshold then
  219.             newFOV = 120 -- Maximum FOV, roblox does not allow higher
  220.         else
  221.             local t = (detectionRange - distance) / (detectionRange - distortThreshold)
  222.             newFOV = 70 + t * (120 - 70)
  223.         end
  224.         camera.FieldOfView = newFOV
  225.     end
  226. end)
  227.  
  228. --[[
  229. -- SNIPPET OF SECTION V --
  230. ---------------
  231. V EXPERIMENTALS
  232.    
  233.     Automatically disables other BH scripts to prevent conflicts.
  234.     For `Constraint-based gravity`: `gravitationalStrength` = `gravitationalConstant`, `absorption`, `toggleMicroGravity`, `constraintMode` = `gravityEngine`, and
  235.     `detectionRange` = `rangeFactor` are the same as their main script counterparts EXCEPT:
  236.        
  237.         `dampingFactor` is the elasticity of constraints, 0 means no bouncy and 1 is bouncy. `damping` is reversed in logic.
  238.         `innerEventHorizon` is similar to `infinityHandler` but the range is in studs in which constraints stop applying forces. more on `infinityHandler` next section.
  239.         `visibleConstraints` simply allows you to see active constraints on objects for testing. this does not affect performance.
  240.         `centerOfMassBased` sets if forces are applied at the center of MASS. force attachments are placed at the center of GEOMETRY.
  241.         `forceCap` sets the maximum force for objects when using linear and realistic LineForce. this prevents infinite speed at singularity.
  242.         `cameraDistortion` is a toggleable parameter that allows the camera to warp for realism (Warning! May be nausea-inducing. This only works with Run)
  243.         `distortThreshold` is the factor (x100 for percentage) of `detectionRange` where the maximum FOV is achieved (min: 70 [default], max: 120 [absolute])
  244.         `gravitationMode` toggles between universal and selective gravitation for specifc objects. requires tags in desired objects with the same value as `attractionTag`
  245.         `attractionTag` is the specific id for the blackhole to pick out and apply gravity while leaving anything without the tag alone.
  246.        
  247.         Constraint modes:
  248.         `spring` or `1` - pretty self-explanatory, uses elasticity to reel in objects, causing them to get jiggy with it.
  249.         `rope` or `2` - just like spring, but with less jiggy, but both can have their jiggy-ness controlled with `dampingFactor`.
  250.         `line` or `3` - a "constraint", but a force like BodyForce. this version may replace bodyforce-based gravity entirely, but I'll still keep the legacy mode.
  251.         `vector` or `4` - similar to lineforces, but uses xyz distribution to apply gravity. generally how bodyforce-based gravity works.
  252.         `linear1` or `5.1` - line-based linear velocity. the old velocity-based gravity and mat replace it, but it can also be accessed. THIS HAS MODEL SUPPORT! RAAAAAH
  253.         `linear2` or `5.2` - vector-based linear velocity. does not support orbits and boring in my opinion.
  254.    
  255.     Experimentals doesn't have much freedom for parameters, only when they're fully integrated into the main scripts. a complete revamp of `gravityEngine` is underway!
  256. ---------------
  257. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement