Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion src/body.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
from vector import Vector2D
from .vector import Vector2D


class Body():
Expand Down
7 changes: 3 additions & 4 deletions src/collision.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/space.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand All @@ -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 = []
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down