From 9b9c6101d36ef828fc23dcd896782d11e21faa75 Mon Sep 17 00:00:00 2001 From: A-Sverdrup Date: Fri, 16 Jan 2026 12:47:32 +0300 Subject: [PATCH] Modify imports to match python package structure, add __round__ to vector, add ability to turn on/off rotation in Space --- src/{utils.py => __init__.py} | 0 src/body.py | 2 +- src/collision.py | 7 +++---- src/space.py | 14 +++++++------- src/vector.py | 2 ++ 5 files changed, 13 insertions(+), 12 deletions(-) rename src/{utils.py => __init__.py} (100%) diff --git a/src/utils.py b/src/__init__.py similarity index 100% rename from src/utils.py rename to src/__init__.py diff --git a/src/body.py b/src/body.py index 83d3e6e..bc695ba 100644 --- a/src/body.py +++ b/src/body.py @@ -1,5 +1,5 @@ import math -from vector import Vector2D +from .vector import Vector2D class Body(): diff --git a/src/collision.py b/src/collision.py index 57172ae..0a7cb87 100644 --- a/src/collision.py +++ b/src/collision.py @@ -1,7 +1,6 @@ -from vector import Vector2D -from body import Body, Polygon, Rectangle, Circle - - +from .vector import Vector2D +from .body import Body, Polygon, Rectangle, Circle + def collide(body_1: Body, body_2: Body, include_rotation = True): if body_1.shape_type == "Polygon" and body_2.shape_type == "Polygon": normal, depth = polygons_collision(body_1, body_2) #if include_rotation else aabbs_collision(body_1, body_2) diff --git a/src/space.py b/src/space.py index 56d618c..765d790 100644 --- a/src/space.py +++ b/src/space.py @@ -1,5 +1,5 @@ -from body import Body -from collision import collide +from .body import Body +from .collision import collide def intersect_aabb(body_a, body_b): @@ -15,10 +15,10 @@ def intersect_aabb(body_a, body_b): class Space: - def __init__(self, bodies: list[Body], gravity = 9.8): + def __init__(self, bodies: list[Body], gravity = 9.8, rotation=True): self.bodies: list[Body] = bodies self.gravity = gravity - + self.rotation = rotation self._contact_points = [] def add(self, body: Body): @@ -33,7 +33,8 @@ def update_position(self, dt): for body in self.bodies: if body.is_static == False: body.pos += body.velocity * dt - body.angle += body.angular_velocity * dt + + if self.rotation: body.angle += body.angular_velocity * dt def handle_collisions(self): self._contact_points = [] @@ -44,8 +45,7 @@ def handle_collisions(self): if not intersect_aabb(self.bodies[i], self.bodies[j]): continue - - contact_points = collide(self.bodies[i], self.bodies[j]) + contact_points = collide(self.bodies[i], self.bodies[j], self.rotation) if contact_points is None: continue diff --git a/src/vector.py b/src/vector.py index da54537..b300187 100644 --- a/src/vector.py +++ b/src/vector.py @@ -48,6 +48,8 @@ def div(self, other): def __iter__(self): yield self.x yield self.y + def __round__(self, ndigits): + return Vector2D(round(self.x, ndigits), round(self.y, ndigits)) def __getitem__(self, index): if index == 0: