Fixed timing delta in physics calculation

(Big Push)
main
Yan Wittmann 2023-03-25 18:19:03 +01:00
parent 3e622e272b
commit 88b3c4724c
2 changed files with 8 additions and 8 deletions

View File

@ -12,7 +12,7 @@ from sprite.Sprite import Sprite
from sprite.StaticSprite import StaticSprite from sprite.StaticSprite import StaticSprite
from ui_elements.TextLabel import TextLabel from ui_elements.TextLabel import TextLabel
what_to_run = 'textlabel' what_to_run = 'physics'
def apply_frame_rate(number: float): def apply_frame_rate(number: float):
@ -38,7 +38,7 @@ elif what_to_run == 'physics':
screen = pygame.display.set_mode((600, 500)) screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("PE GAME") pygame.display.set_caption("PE GAME")
clock = pygame.time.Clock() clock = pygame.time.Clock()
frame_rate = 80 frame_rate = 120
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json") 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 = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
test_3_sprite.position_scale = PositionScale((130, 100), (1, 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) physics_handler.add_sprite(test_3_sprite)
test_2_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1")) test_2_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
@ -61,7 +61,7 @@ elif what_to_run == 'physics':
while True: while True:
clock.tick(frame_rate) clock.tick(frame_rate)
skip = True skip = False
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
pygame.quit() pygame.quit()

View File

@ -40,12 +40,12 @@ class PhysicsElementsHandler:
sorted_dynamic_sprites = sorted(dynamic_sprites, key=lambda spr: spr.position_scale.position[1]) sorted_dynamic_sprites = sorted(dynamic_sprites, key=lambda spr: spr.position_scale.position[1])
for sprite in sorted_dynamic_sprites: 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 total_motion = sprite.motion
motion_step = (total_motion[0] / motion_steps, total_motion[1] / motion_steps) motion_step = ((total_motion[0] * dt) / motion_steps, (total_motion[1] * dt) / motion_steps)
# print(motion_step) print(motion_step)
for i in range(motion_steps): for i in range(motion_steps):
sprite.reset_touches() sprite.reset_touches()