Advertisement
1m1m0

Atomic Charge (BodyForce)

Feb 17th, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | Source Code | 0 0
  1. -- Function to apply electromagnetic force using BodyForce
  2. function ApplyElectromagneticForce(object1, object2)
  3.     local direction = object1.Position - object2.Position
  4.     local distance = direction.Magnitude
  5.  
  6.     -- Do not apply the force if the objects are too close to each other
  7.     if distance <= 0.9 * object1.Size.X then
  8.         return
  9.     end
  10.  
  11.     local forceMagnitude = (attractionPower * object1.Size.X * object2.Size.X) / (distance * distance)
  12.     local force = direction.Unit * forceMagnitude
  13.  
  14.     -- Check the charges of the objects
  15.     local charge1 = object1:FindFirstChild("Charge") and object1.Charge.Value
  16.     local charge2 = object2:FindFirstChild("Charge") and object2.Charge.Value
  17.  
  18.     -- If the charges are the same, they repel each other
  19.     if charge1 == charge2 then
  20.         force = -force -- Reverse the direction of the force
  21.     end
  22.  
  23.     -- Apply the force using a BodyForce instance
  24.     local bodyForce = object1:FindFirstChild("ElectromagneticBodyForce")
  25.     if not bodyForce then
  26.         bodyForce = Instance.new("BodyForce")
  27.         bodyForce.Name = "ElectromagneticBodyForce"
  28.         bodyForce.Force = force
  29.         bodyForce.Parent = object1
  30.     else
  31.         bodyForce.Force = force
  32.     end
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement