Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to apply electromagnetic force
- 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
- object1.Velocity = object1.Velocity - force
- object2.Velocity = object2.Velocity + force
- -- If the charges are different, they attract each other
- else
- object1.Velocity = object1.Velocity + force
- object2.Velocity = object2.Velocity - force
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement