From 4eeb5ad102512a09f2dc2fb39e43c5d02f634392 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 22 Feb 2024 16:39:37 +0100 Subject: [PATCH] game: add: paddle collision --- games/objects/Segment.py | 4 ++-- games/routine.py | 46 ++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/games/objects/Segment.py b/games/objects/Segment.py index fc62ada..c45ee33 100644 --- a/games/objects/Segment.py +++ b/games/objects/Segment.py @@ -10,8 +10,8 @@ class Segment: self.start: Point = start self.stop: Point = stop - def angle(self): - return math.atan2((self.start.x - self.start.y), (self.stop.x - self.stop.y)) + def angle(self) -> float: + return math.atan2((self.start.y - self.stop.y), (self.start.x - self.stop.x)) def length(self): return self.start.distance(self.stop) diff --git a/games/routine.py b/games/routine.py index 1e551f5..6d0e268 100644 --- a/games/routine.py +++ b/games/routine.py @@ -158,21 +158,20 @@ def get_impact_data(segments: list[Segment], ball: Ball) -> dict: return closest -def wall_collision(ball_angle: float, wall_angle: float) -> float: - - ball_cos: float = math.cos(ball_angle) - ball_sin: float = math.sin(ball_angle) - +def wall_collision(ball_angle: float, wall: Segment) -> float: + + wall_angle: float = wall.angle() + + cos: float = math.cos(wall_angle) * -1 + sin: float = math.sin(wall_angle) + + wall_angle: float = math.atan2(sin, cos) + incident_angle: float = ball_angle - wall_angle reflection_angle: float = wall_angle - incident_angle - - new_cos: float = math.cos(reflection_angle) - new_sin: float = math.sin(reflection_angle) - - new_angle: float = math.atan2(new_sin, new_cos) - - return new_angle + + return reflection_angle async def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, inc_y: float): @@ -204,7 +203,20 @@ async def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: flo await SyncToAsync(player.game.broadcast)("goal", {"player": player.user_id, "nb_goal": player.nb_goal}) return None - return ball.angle + math.pi + paddle_angle: float = paddle.angle() + + normal: float = paddle_angle - math.pi / 2 + + start_distance: float = paddle.start.distance(impact) + stop_distance: float = paddle.stop.distance(impact) + + hit_percent: float = (start_distance) / (start_distance + stop_distance) + + hit_percent = round(hit_percent, 1) + + new_angle: float = normal + (math.pi * 0.85) * (hit_percent - 0.5) + + return new_angle async def collision(game: Game, impact_data: dict) -> bool: @@ -216,14 +228,12 @@ async def collision(game: Game, impact_data: dict) -> bool: continue if (player.rail is segment): player_hitted = player - break - - surface_angle: float = math.atan2(segment.start.y - segment.stop.y, segment.start.x - segment.stop.y) + break angle: float if (player_hitted is None): - angle = wall_collision(game.ball.angle, surface_angle) + angle = wall_collision(game.ball.angle, segment) else: angle = await paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y")) @@ -242,7 +252,7 @@ async def update_ball(game: Game, impact_data: dict) -> None: time_before_impact: float = distance / game.ball.speed await asyncio.sleep(time_before_impact) - + hit: bool = await collision(game, impact_data) if (hit == False):