Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to apply electromagnetic force using BodyForce
- function ApplyElectromagneticForce(object1, object2)
- local direction = object1.Position - object2.Position
- local distance = direction.Magnitude
- -- Do not apply the force if the objects are too close to each other
- if distance <= 0.9 * object1.Size.X then
- return
- end
- local forceMagnitude = (attractionPower * object1.Size.X * object2.Size.X) / (distance * distance)
- local force = direction.Unit * forceMagnitude
- -- Check the charges of the objects
- local charge1 = object1:FindFirstChild("Charge") and object1.Charge.Value
- local charge2 = object2:FindFirstChild("Charge") and object2.Charge.Value
- -- If the charges are the same, they repel each other
- if charge1 == charge2 then
- force = -force -- Reverse the direction of the force
- end
- -- Apply the force using a BodyForce instance
- local bodyForce = object1:FindFirstChild("ElectromagneticBodyForce")
- if not bodyForce then
- bodyForce = Instance.new("BodyForce")
- bodyForce.Name = "ElectromagneticBodyForce"
- bodyForce.Force = force
- bodyForce.Parent = object1
- else
- bodyForce.Force = force
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement