From 59cac2b1fd4568e4b5ba7541be23be99d3667c43 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 20 Feb 2024 09:07:01 +0100 Subject: [PATCH] game: add: padding to collision --- frontend/static/js/api/game/Game.js | 4 +++ frontend/static/js/api/game/GameConfig.js | 7 +++- games/config.py | 4 ++- games/objects/Ball.py | 2 +- games/routine.py | 39 +++++++++++++++-------- games/views.py | 6 ++-- 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/frontend/static/js/api/game/Game.js b/frontend/static/js/api/game/Game.js index 028e547..81dc406 100644 --- a/frontend/static/js/api/game/Game.js +++ b/frontend/static/js/api/game/Game.js @@ -107,6 +107,10 @@ class Game ctx.clearRect(0, 0, this.config.size_x, this.config.size_y); this.draw_sides(ctx); this.ball.render(ctx); + + ctx.strokeStyle = "#000000"; + ctx.lineWidth = this.config.stroke_thickness; + ctx.stroke(); } _send(data) diff --git a/frontend/static/js/api/game/GameConfig.js b/frontend/static/js/api/game/GameConfig.js index 4b2ae61..e89a784 100644 --- a/frontend/static/js/api/game/GameConfig.js +++ b/frontend/static/js/api/game/GameConfig.js @@ -77,8 +77,13 @@ class GameConfig */ this.ball_spawn_y = this.center_y; + /** + * @type {Number} + */ + this.stroke_thickness = response_data.STROKE_THICKNESS; + return 0; } } -export { GameConfig } \ No newline at end of file +export { GameConfig }; \ No newline at end of file diff --git a/games/config.py b/games/config.py index 11ccfba..6c7645d 100644 --- a/games/config.py +++ b/games/config.py @@ -17,4 +17,6 @@ BALL_SIZE = 4 BALL_SPAWN_POS_X = MAP_SIZE_X / 2 BALL_SPAWN_POS_Y = MAP_SIZE_Y / 2 -SERVER_TPS = 20 \ No newline at end of file +SERVER_TPS = 20 + +STROKE_THICKNESS = 10 \ No newline at end of file diff --git a/games/objects/Ball.py b/games/objects/Ball.py index 4c03d0d..d32afd4 100644 --- a/games/objects/Ball.py +++ b/games/objects/Ball.py @@ -18,7 +18,7 @@ class Ball: def reset(self) -> None: self.size = config.BALL_SIZE self.position = Point(config.BALL_SPAWN_POS_X + self.size / 2, config.BALL_SPAWN_POS_Y + self.size / 2) - self.angle = math.pi * 1 + self.angle = math.pi * 0.3 self.speed = config.BALL_SPEED_START def to_dict(self) -> dict: diff --git a/games/routine.py b/games/routine.py index abbfbe1..2eadebb 100644 --- a/games/routine.py +++ b/games/routine.py @@ -115,6 +115,9 @@ def get_impact_point(segments: list[Segment], ball: Ball) -> dict: cos: float = round(math.cos(ball.angle), 6) sin: float = round(math.sin(ball.angle), 6) + inc_x: float = (-1) * get_sign(cos) * (config.STROKE_THICKNESS + config.BALL_SIZE / 2) + inc_y: float = get_sign(sin) * (config.STROKE_THICKNESS + config.BALL_SIZE / 2) + point: Point = Point(ball.position.x + cos, ball.position.y - sin) ball_segment = Segment(ball.position, point) @@ -122,8 +125,16 @@ def get_impact_point(segments: list[Segment], ball: Ball) -> dict: closest: dict = None for segment in segments: + + segment_with_padding = segment.copy() + + segment_with_padding.start.x += inc_x + segment_with_padding.stop.x += inc_x + + segment_with_padding.start.y += inc_y + segment_with_padding.stop.y += inc_y - impact: Point = get_interception(segment, ball_segment) + impact: Point = get_interception(segment_with_padding, ball_segment) if (impact is None): continue @@ -136,17 +147,13 @@ def get_impact_point(segments: list[Segment], ball: Ball) -> dict: if (get_sign(diff_y) != get_sign(sin) and sin != 0): continue - impact_with_padding: Point = impact.copy() - - impact_with_padding.x += (ball.size / 2) * get_sign(cos) * (-1) - impact_with_padding.y += (ball.size / 2) * get_sign(sin) - - if (closest is None or impact_with_padding.distance(ball.position) < closest.get("distance")): + if (closest is None or impact.distance(ball.position) < closest.get("distance")): closest = { - "impact_with_padding": impact_with_padding, + "inc_x": inc_x, + "inc_y": inc_y, "impact": impact, "segment": segment, - "distance": impact_with_padding.distance(ball.position), + "distance": impact.distance(ball.position), } return closest @@ -167,7 +174,7 @@ def wall_collision(ball_angle: float, wall_angle: float) -> float: return new_angle -def paddle_collision(ball: Ball, impact: Point, player: Player): +def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, inc_y: float): diff_x: float = player.rail.stop.x - player.rail.start.x diff_y: float = player.rail.stop.y - player.rail.start.y @@ -190,8 +197,12 @@ def paddle_collision(ball: Ball, impact: Point, player: Player): paddle: Segment = Segment(start, stop) - if (not paddle.is_on(impact)): - print(not paddle.is_on(impact)) + hit_point: Point = Point(impact.x - inc_x, impact.y - inc_y) + + print(hit_point, paddle) + + if (not paddle.is_on(hit_point)): + print(not paddle.is_on(hit_point)) return None return ball.angle + math.pi @@ -216,7 +227,7 @@ def collision(game: Game, impact_data: dict) -> bool: angle = wall_collision(game.ball.angle, surface_angle) else: - angle = paddle_collision(game.ball, impact_data.get("impact"), player_hitted) + angle = paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y")) if (angle is None): return False @@ -239,7 +250,7 @@ async def update_ball(game: Game, impact_data: dict) -> None: print("Goal") game.ball.reset() else: - game.ball.position = impact_data.get("impact_with_padding") + game.ball.position = impact_data.get("impact") await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict()) diff --git a/games/views.py b/games/views.py index 2559264..137ce24 100644 --- a/games/views.py +++ b/games/views.py @@ -20,8 +20,10 @@ class GameConfigView(APIView): "PADDLE_SPEED_PER_SECOND_MAX": config.PADDLE_SPEED_PER_SECOND_MAX, "PADDLE_RATIO": config.PADDLE_RATIO, - "BALL_SIZE": config.BALL_SIZE, + "BALL_SIZE": config.BALL_SIZE, "BALL_SPEED_INC": config.BALL_SPEED_INC, - "BALL_SPEED_START": config.BALL_SPEED_START + "BALL_SPEED_START": config.BALL_SPEED_START, + + "STROKE_THICKNESS": config.STROKE_THICKNESS, } return Response(config_data, status = status.HTTP_200_OK) \ No newline at end of file