game: add: padding to collision

This commit is contained in:
starnakin 2024-02-20 09:07:01 +01:00 committed by AdrienLSH
parent 4a97df6f38
commit 828e136bcc
6 changed files with 43 additions and 19 deletions

View File

@ -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)

View File

@ -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 }
export { GameConfig };

View File

@ -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
SERVER_TPS = 20
STROKE_THICKNESS = 10

View File

@ -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:

View File

@ -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())

View File

@ -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)