From 1d84976c995022d30f5b9854fe61b374fe488278 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 15 Nov 2020 12:48:07 +0400 Subject: [PATCH] Allow changing polygon sides in SAT demo --- geom/demos/aabb.py | 6 ++++++ geom/demos/sat.py | 26 ++++++++++++++++++++------ geom/demos/vec_proj.py | 3 +++ geom/screen.py | 2 ++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/geom/demos/aabb.py b/geom/demos/aabb.py index 2ac8c15..c2b2dbf 100644 --- a/geom/demos/aabb.py +++ b/geom/demos/aabb.py @@ -9,6 +9,9 @@ class AABBDistance: title = "AABB Distance" + def handle_key_down(self, key): + pass + def render(self, surface): rect_a = AxisAlignedBB( -(WIDTH / 8), @@ -51,6 +54,9 @@ class PointAABBDistance: title = "Point-AABB Distance" + def handle_key_down(self, key): + pass + def render(self, surface): rect_a = AxisAlignedBB( -(WIDTH / 8), diff --git a/geom/demos/sat.py b/geom/demos/sat.py index c2f8bfd..a0361cd 100644 --- a/geom/demos/sat.py +++ b/geom/demos/sat.py @@ -1,3 +1,4 @@ +import pygame from ..geom import Polygon, generate_polygon from .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs from ..colors import * @@ -12,12 +13,25 @@ class SeparatingAxisTheorem: def __init__(self): self.shape1 = generate_polygon(0, 0, sides=4, radius=60) - self.shape2 = Polygon(*get_cursor_pos(), [ - (60, 60), - (60, -60), - (-60, -60), - (-60, 60) - ]) + self.shape2 = None + self.shape2_sides = 4 + self.shape2_radius = 60 + self.generate_shape2() + + def generate_shape2(self): + rotation = 45 if self.shape2 is None else self.shape2.rotation + self.shape2 = generate_polygon(*get_cursor_pos(), + rotation, + sides=self.shape2_sides, + radius=self.shape2_radius) + + def handle_key_down(self, key): + if key == pygame.K_a: + self.shape2_sides = max(self.shape2_sides - 1, 3) + self.generate_shape2() + if key == pygame.K_d: + self.shape2_sides = min(self.shape2_sides + 1, 31) + self.generate_shape2() def handle_input(self): self.shape2.set_position(*get_cursor_pos()) diff --git a/geom/demos/vec_proj.py b/geom/demos/vec_proj.py index e266092..252a27b 100644 --- a/geom/demos/vec_proj.py +++ b/geom/demos/vec_proj.py @@ -7,6 +7,9 @@ class VecProj: title = "Vector Projection" + def handle_key_down(self, key): + pass + def render(self, surface): x1 = -(WIDTH / 4) y1 = 100 diff --git a/geom/screen.py b/geom/screen.py index 5017ce8..d3bc3f6 100644 --- a/geom/screen.py +++ b/geom/screen.py @@ -17,6 +17,8 @@ class Screen: def handle_input(self): for event in pygame.event.get(pygame.KEYDOWN): + self.demos[self.demo].handle_key_down(event.key) + if event.key == pygame.K_DOWN: self.demo += 1 if self.demo >= len(self.demos):