From 88b3c4724c1d8ba941bef5a83811fda3a9beeb32 Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Sat, 25 Mar 2023 18:19:03 +0100 Subject: [PATCH] Fixed timing delta in physics calculation (Big Push) --- project/main.py | 8 ++++---- project/physics/PhysicsElementsHandler.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/project/main.py b/project/main.py index ea3cf10..4caa1b5 100644 --- a/project/main.py +++ b/project/main.py @@ -12,7 +12,7 @@ from sprite.Sprite import Sprite from sprite.StaticSprite import StaticSprite from ui_elements.TextLabel import TextLabel -what_to_run = 'textlabel' +what_to_run = 'physics' def apply_frame_rate(number: float): @@ -38,7 +38,7 @@ elif what_to_run == 'physics': screen = pygame.display.set_mode((600, 500)) pygame.display.set_caption("PE GAME") clock = pygame.time.Clock() - frame_rate = 80 + frame_rate = 120 spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json") @@ -50,7 +50,7 @@ elif what_to_run == 'physics': test_3_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1")) test_3_sprite.position_scale = PositionScale((130, 100), (1, 1)) - test_3_sprite.motion = (-4, -11) + test_3_sprite.motion = (-9, -12) physics_handler.add_sprite(test_3_sprite) test_2_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1")) @@ -61,7 +61,7 @@ elif what_to_run == 'physics': while True: clock.tick(frame_rate) - skip = True + skip = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index 78b4a70..da29ea3 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -40,12 +40,12 @@ class PhysicsElementsHandler: sorted_dynamic_sprites = sorted(dynamic_sprites, key=lambda spr: spr.position_scale.position[1]) for sprite in sorted_dynamic_sprites: - self.attempt_move(sprite, colliders, MOTION_STEPS) + self.attempt_move(dt, sprite, colliders, MOTION_STEPS) - def attempt_move(self, sprite: DynamicSprite, colliders: list[StaticSprite], motion_steps: int) -> bool: + def attempt_move(self, dt: float, sprite: DynamicSprite, colliders: list[StaticSprite], motion_steps: int) -> bool: total_motion = sprite.motion - motion_step = (total_motion[0] / motion_steps, total_motion[1] / motion_steps) - # print(motion_step) + motion_step = ((total_motion[0] * dt) / motion_steps, (total_motion[1] * dt) / motion_steps) + print(motion_step) for i in range(motion_steps): sprite.reset_touches()