From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:28:50 +0000 (-0500) Subject: electric force X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.css?a=commitdiff_plain;h=01f0437b2f804fdd13406308491b2f75a5ef2154;p=Electric-Dipoles.git electric force --- diff --git a/main.py b/main.py index a78e403..7f6ac5e 100644 --- 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) diff --git a/particle.py b/particle.py index 134a90b..f5241c6 100644 --- a/particle.py +++ b/particle.py @@ -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) diff --git a/setup.py b/setup.py index 66336fa..96d7245 100644 --- 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)