game: add: paddle collision

This commit is contained in:
starnakin 2024-02-22 16:39:37 +01:00
parent 8fa282da9d
commit 4eeb5ad102
2 changed files with 30 additions and 20 deletions

View File

@ -10,8 +10,8 @@ class Segment:
self.start: Point = start self.start: Point = start
self.stop: Point = stop self.stop: Point = stop
def angle(self): def angle(self) -> float:
return math.atan2((self.start.x - self.start.y), (self.stop.x - self.stop.y)) return math.atan2((self.start.y - self.stop.y), (self.start.x - self.stop.x))
def length(self): def length(self):
return self.start.distance(self.stop) return self.start.distance(self.stop)

View File

@ -158,21 +158,20 @@ def get_impact_data(segments: list[Segment], ball: Ball) -> dict:
return closest return closest
def wall_collision(ball_angle: float, wall_angle: float) -> float: def wall_collision(ball_angle: float, wall: Segment) -> float:
ball_cos: float = math.cos(ball_angle) wall_angle: float = wall.angle()
ball_sin: float = math.sin(ball_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 incident_angle: float = ball_angle - wall_angle
reflection_angle: float = wall_angle - incident_angle reflection_angle: float = wall_angle - incident_angle
new_cos: float = math.cos(reflection_angle) return reflection_angle
new_sin: float = math.sin(reflection_angle)
new_angle: float = math.atan2(new_sin, new_cos)
return new_angle
async def paddle_collision(ball: Ball, impact: Point, player: Player, inc_x: float, inc_y: float): 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}) await SyncToAsync(player.game.broadcast)("goal", {"player": player.user_id, "nb_goal": player.nb_goal})
return None 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: async def collision(game: Game, impact_data: dict) -> bool:
@ -218,12 +230,10 @@ async def collision(game: Game, impact_data: dict) -> bool:
player_hitted = player player_hitted = player
break break
surface_angle: float = math.atan2(segment.start.y - segment.stop.y, segment.start.x - segment.stop.y)
angle: float angle: float
if (player_hitted is None): if (player_hitted is None):
angle = wall_collision(game.ball.angle, surface_angle) angle = wall_collision(game.ball.angle, segment)
else: else:
angle = await paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y")) angle = await paddle_collision(game.ball, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y"))