]> Skullheadx's Git Forge - Electric-Dipoles.git/commitdiff
electric force
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 27 Nov 2023 20:28:50 +0000 (15:28 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 27 Nov 2023 20:28:50 +0000 (15:28 -0500)
main.py
particle.py
setup.py

diff --git a/main.py b/main.py
index a78e4032d991c3a7a754a6142256896e8338b959..7f6ac5ef0951554c8f2043d53c95ebb553e68c3a 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,12 +1,13 @@
-from setup import *
 from particle import PointParticle
+from setup import *
 
 is_running = True
 clock = pygame.Clock()
 delta = 0
 
 particles = [PointParticle((SCREEN_WIDTH / 3, SCREEN_HEIGHT / 2), 1.6e-19),
-             PointParticle((SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT / 2), -1.6e-19)]
+             PointParticle((SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT / 2), -1.6e-19),
+             PointParticle((SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), 1.6e-19)]
 
 while is_running:
     for event in pygame.event.get():
@@ -17,8 +18,10 @@ while is_running:
         particle.update(delta, particles)
 
     screen.fill((255, 255, 255))
+    # for particle in particles:
+    #     particle.draw_field_lines(screen)
     for particle in particles:
-        particle.draw_field_lines(screen)
+        particle.draw_forces(screen)
     for particle in particles:
         particle.draw(screen)
 
index 134a90b8a869c891113382c581f1506c6379e602..f5241c6e26ee0d138263c828e17584741896d769 100644 (file)
@@ -16,6 +16,8 @@ class PointParticle:
     default_num_field_lines = 64
     screen_rect = pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
 
+    electric_force_magnitude_factor = 1.5e34
+
     def __init__(self, position, charge=1.6e-19):
         self.position = pygame.Vector2(position)
         self.mouse_offset = pygame.Vector2(0, 0)
@@ -31,6 +33,9 @@ class PointParticle:
         self.line_step = pygame.Vector2(self.step_amount, 0) if self.charge > 0 else pygame.Vector2(-self.step_amount,
                                                                                                     0)
 
+        self.forces = []
+        self.net_electric_force = pygame.Vector2(0, 0)
+
     def update(self, delta, particles):
         if self.dragging or not (True in [p.dragging for p in particles]):
             if pygame.mouse.get_pressed(3)[0]:
@@ -98,11 +103,31 @@ class PointParticle:
                 if end:
                     break
 
+        self.forces.clear()
+        self.net_electric_force = pygame.Vector2(0, 0)
+        for particle in particles:
+            if particle is self:
+                continue
+            diff = particle.position - self.position
+            angle = math.atan2(diff.y, diff.x)
+            electric_force = 8.99e9 * abs(particle.charge) * abs(self.charge) / pow(diff.length(), 2) * (
+                -1 if particle.charge * self.charge > 0 else 1)
+            self.forces.append(pygame.Vector2(electric_force * math.cos(angle), electric_force * math.sin(angle)))
+            self.net_electric_force += self.forces[-1]
+
     def draw_field_lines(self, surf):
         for line in self.field_lines:
             if len(line) > 1:
                 pygame.draw.lines(surf, self.field_line_colour, False, line, 3)
 
+    def draw_forces(self, surf):
+        for force in self.forces:
+            draw_arrow(surf, (30, 30, 30), self.position, self.position + force * self.electric_force_magnitude_factor,
+                       3, head_angle=math.pi * 3 / 4)
+        draw_arrow(surf, (0, 0, 0), self.position,
+                   self.position + self.net_electric_force * self.electric_force_magnitude_factor, 6,
+                   head_angle=math.pi * 3 / 4)
+
     def draw(self, surf):
         pygame.draw.circle(surf, self.colour, self.position, self.radius)
         pygame.draw.circle(surf, (0, 0, 0), self.position, self.radius, 3)
index 66336faaf524da2ba175352404a7a83bcd3af2c6..96d72459e2974564365e83407f1ba58010528d43 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -1,10 +1,18 @@
-import pygame
 import math
 
+import pygame
 
 pygame.init()
 
-
 SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
-pygame.display.set_caption("Electric Dipoles")
\ No newline at end of file
+pygame.display.set_caption("Electric Dipoles")
+
+
+def draw_arrow(surface, color, start_pos, end_pos, width=1, head_length=10, head_angle=math.pi / 4):
+    pygame.draw.line(surface, color, start_pos, end_pos, width)
+    direction = (end_pos - start_pos).normalize()
+    left = direction.rotate(math.degrees(head_angle))
+    right = direction.rotate(-math.degrees(head_angle))
+    pygame.draw.line(surface, color, end_pos, end_pos + left * head_length, width)
+    pygame.draw.line(surface, color, end_pos, end_pos + right * head_length, width)