Advertisement
1m1m0

Atomics Emulator

Feb 17th, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.33 KB | Source Code | 0 0
  1. -- [ Charged Particles/Atoms Simulation Source Code Basics ] --
  2. -- For archived versions, visit my pastebin: https://pastebin.com/u/1m1m0
  3.  
  4. local particle = script.Parent
  5. local attractionPower = 5 -- Attraction strength between attracting particles
  6. local halfLife = 60 -- Time for a particle to flip it's charge
  7. local radiateThreshold = {1, 2, 3} -- Amount/s of particles a particle with an opposite charge is touching to eject itself
  8.  
  9. --[ VELOCITY PRESET ]
  10. -- Function to apply electromagnetic force
  11. function ApplyElectromagneticForce(object1, object2)
  12.     local direction = object1.Position - object2.Position
  13.     local distance = direction.Magnitude
  14.  
  15.     -- Do not apply the force if the objects are too close to each other
  16.     if distance <= 0.9 * object1.Size.X then
  17.         return -- This prevents velocities from approaching infinity
  18.     end
  19.  
  20.     local forceMagnitude = (attractionPower * object1.Size.X * object2.Size.X) / (distance * distance)
  21.     local force = direction.Unit * forceMagnitude
  22.  
  23.     -- Check the charges of the objects
  24.     local charge1 = object1:FindFirstChild("Charge") and object1.Charge.Value
  25.     local charge2 = object2:FindFirstChild("Charge") and object2.Charge.Value
  26.  
  27.     -- If the charges are the same, they attract eachother (I know, it violeates real world laws)
  28.     if charge1 == charge2 then
  29.         object1.Velocity = object1.Velocity - force
  30.         object2.Velocity = object2.Velocity + force
  31.         -- If the charges are different, they repel each other
  32.     else
  33.         object1.Velocity = object1.Velocity + force
  34.         object2.Velocity = object2.Velocity - force
  35.     end
  36. end
  37.  
  38. --]]
  39. --[[ [ BODYFORCE PRESET ]
  40.  
  41. -- Function to apply electromagnetic force using BodyForce
  42. function ApplyElectromagneticForce(object1, object2)
  43.     local direction = object1.Position - object2.Position
  44.     local distance = direction.Magnitude
  45.  
  46.     -- Do not apply the force if the objects are too close to each other
  47.     if distance <= 0.9 * object1.Size.X then
  48.         return
  49.     end
  50.  
  51.     local forceMagnitude = (attractionPower * object1.Size.X * object2.Size.X) / (distance * distance)
  52.     local force = direction.Unit * forceMagnitude
  53.  
  54.     -- Check the charges of the objects
  55.     local charge1 = object1:FindFirstChild("Charge") and object1.Charge.Value
  56.     local charge2 = object2:FindFirstChild("Charge") and object2.Charge.Value
  57.  
  58.     -- If the charges are the same, they repel each other
  59.     if charge1 == charge2 then
  60.         force = -force -- Reverse the direction of the force
  61.     end
  62.  
  63.     -- Apply the force using a BodyForce instance
  64.     local bodyForce = object1:FindFirstChild("ElectromagneticBodyForce")
  65.     if not bodyForce then
  66.         bodyForce = Instance.new("BodyForce")
  67.         bodyForce.Name = "ElectromagneticBodyForce"
  68.         bodyForce.Force = force
  69.         bodyForce.Parent = object1
  70.     else
  71.         bodyForce.Force = force
  72.     end
  73. end
  74.  
  75. --]]
  76.  
  77. -- Function to check and apply electromagnetic force
  78. local function CheckAndApplyElectromagneticForce(obj)
  79.     if not obj:IsA("BasePart") or obj:IsA("Model") or obj:IsA("Union") or obj:IsA("Tool") or obj:IsA("MeshPart") or obj:IsA("Mesh") then
  80.         return   -- BasePart means everything in Workspace, some may not be included and you may need to add "obj:IsA("")" with their classname in the list above.
  81.     end
  82.     if obj == particle or obj == obj:IsA("Player") then
  83.         return -- Excludes the players and particle itself from electromagnetic force, feel free to add more.
  84.     end
  85.     if obj.Anchored then
  86.         return -- Prevents particle from applying force to the anchored objects. (Saves resources and computation power)
  87.     end
  88.  
  89.     -- Randomly assign a charge to the object if it doesn't have one
  90.     if not obj:FindFirstChild("Charge") then
  91.         local boolValue = Instance.new("BoolValue")
  92.         boolValue.Name = "Charge"
  93.         boolValue.Value = math.random() > 0.5 -- Get rid of this line for identical xharges if you want
  94.         boolValue.Parent = obj
  95.  
  96.         -- Start a coroutine to flip the charge at a random interval
  97.         coroutine.wrap(function()
  98.             while obj do
  99.                 wait(math.random() * halfLife) -- Wait for a random time up to 60 seconds (Default half life)
  100.                 boolValue.Value = not boolValue.Value -- Flip the charge
  101.  
  102.                 -- Break any existing constraints when the charge flips
  103.                 for _, child in pairs(obj:GetChildren()) do
  104.                     if child:IsA("RodConstraint") then
  105.                         child:Destroy()
  106.                     end
  107.                 end
  108.             end
  109.         end)()
  110.     end
  111.  
  112.     ApplyElectromagneticForce(particle, obj)
  113. end
  114.  
  115. game:GetService("RunService").Heartbeat:Connect(function()
  116.     for _, object in pairs(game.Workspace:GetDescendants()) do
  117.         CheckAndApplyElectromagneticForce(object)
  118.     end
  119. end)
  120.  
  121. -- Function to create a RodConstraint between particles with the same charge
  122. local function CreateConstraint(part)
  123.     local otherPart = part:GetTouchingParts()[radiateThreshold] -- Get the first 1, 2 or 3 parts that is touching the current part
  124.     if otherPart then
  125.         local charge1 = part:FindFirstChild("Charge") and part.Charge.Value
  126.         local charge2 = otherPart:FindFirstChild("Charge") and otherPart.Charge.Value
  127.         if charge1 == charge2 then
  128.             local attachment0 = Instance.new("Attachment", part)
  129.             local attachment1 = Instance.new("Attachment", otherPart)
  130.             local constraint = Instance.new("RodConstraint")
  131.             constraint.Attachment0 = attachment0
  132.             constraint.Attachment1 = attachment1
  133.             constraint.Length = part.Size.X/2 + otherPart.Size.X/2 -- Set the length to the sum of the radii of the parts
  134.             constraint.Parent = part
  135.         end
  136.     end
  137. end
  138.  
  139. particle.Touched:Connect(CreateConstraint)
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement