Advertisement
1m1m0

Atomic Charge (Velocity)

Feb 17th, 2024 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.00 KB | Source Code | 0 0
  1. -- Function to apply electromagnetic force
  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.         object1.Velocity = object1.Velocity - force
  21.         object2.Velocity = object2.Velocity + force
  22.         -- If the charges are different, they attract each other
  23.     else
  24.         object1.Velocity = object1.Velocity + force
  25.         object2.Velocity = object2.Velocity - force
  26.     end
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement