I am trying to make an animation where two magnets (that are repelling each other) are falling in a rotating tube. I have the falling (gravity) bit and rotation down but I am having trouble with magnetic forces. The equation for the force that I am using is (magnetic strength of magnet 1 x magnetic strength of magnet 2)/(distance between the magnets)^2. Basically, the strength of the force decreases with the square of the distance between the magnets. The goal is to have magnets repel each other as the same poles are facing each other. I believe I am not using the "apply_force_at_local_point" command properly. I am especially unsure about updating the x and y directions of the forces in the "apply_force_at_local_point" command
You really don't need to know much physics for this. Thanks for the help in advance
import pymunk
import pymunk.pygame_util
import pygame
import numpy as np
GRAY = (220, 220, 220)
width_mass=48
height_mass=48
charges=[10000,-10000] #magnet strengths
pygame.init()
size = 800,600
screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
draw_options = pymunk.pygame_util.DrawOptions(screen)
space = pymunk.Space()
space.gravity = (0,-900)
pts = [(-27, -238.5), (27,-238.5), (27,238.5), (-27,238.5)] #enpoints of the endlessly rotating rectangle
body_type=pymunk.Body(body_type=pymunk.Body.KINEMATIC)
body_type.position = (400, 263.5)
space.add(body_type)
for i in range(4):
segment = pymunk.Segment(body_type, pts[i], pts[(i+1)%4], 2)
space.add(segment)
body_type.angular_velocity=1 #rotation speed
class Rectangle:
def __init__(self, rect_mass, pos,size=(80, 50)):
self.body = pymunk.Body(mass=rect_mass)
self.body.position = pos
shape = pymunk.Poly.create_box(self.body, size)
shape.density = 0.1
space.add(self.body, shape)
mass_1 = Rectangle(rect_mass=1,pos=(400,473),size=(50,50))
mass_2 = Rectangle(rect_mass=1,pos=(400,420),size=(50,50))
masses=[mass_1,mass_2]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(GRAY)
space.debug_draw(draw_options)
pygame.display.update()
space.step(0.01)
temp=[] #collecting the positions of all masses in one place
for mass in masses:
temp.append(mass.body.position)
if len(masses)==2:
rel_dist=np.sqrt((temp[1][1]-temp[0][1])**2+(temp[1][0]-temp[0][0])**2) #euclidean distance between magnets
mag_force=charges[0]*charges[1]/(rel_dist**2 + 0.00001) #force = magnet1*magnet2/dist of the magnets^2
masses[0].body.apply_force_at_local_point(
(mag_force*(temp[1][0]-temp[0][0]),
mag_force*(temp[1][1]-temp[0][1])),
(masses[0].body.position.x,masses[0].body.position.y)) #this needs to be fixed
masses[1].body.apply_force_at_local_point(
-1*(mag_force*(temp[1][0]-temp[0][0]),
mag_force*(temp[1][1]-temp[0][1])),
(masses[1].body.position.x,masses[1].body.position.y)) #this needs to be fixed
pygame.quit()