81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from .objects.Spectator import Spectator
|
|
from .objects.Player import Player
|
|
from .objects.Game import Game
|
|
from .objects.Ball import Ball
|
|
|
|
from .objects.Point import Point
|
|
from .objects.Segment import Segment
|
|
|
|
from . import config
|
|
|
|
import math
|
|
from time import sleep
|
|
|
|
#see the video to understand the algorithme
|
|
#https://www.youtube.com/watch?v=KOYoMYWUTEo
|
|
def determine_director_coefficient(segment: Segment):
|
|
return ((segment.start.y - segment.stop.y) / (segment.start.x - segment.stop.x))
|
|
|
|
def determine_ordinate_at_origin(point: Point, director_cofficient: float):
|
|
return point.y - point.x * director_cofficient
|
|
|
|
def determine_intersection(director_coefficient1: float, ordinate_at_origin1: float, director_coefficient2: float, ordinate_at_origin2: float):
|
|
if (director_coefficient1 == director_coefficient2):
|
|
return None
|
|
return (ordinate_at_origin1 + ordinate_at_origin2) / (director_coefficient1 + director_coefficient2)
|
|
|
|
def determine_intersections(ball: Ball, segments: list[Segment]):
|
|
|
|
intersections: list[Point] = []
|
|
|
|
for segment in segments:
|
|
|
|
# form m * x + p
|
|
m: float = determine_director_coefficient(segment)
|
|
p: float = determine_ordinate_at_origin(segment.start, m)
|
|
|
|
x: float = determine_intersection(m, p, ball.velocity_y, 0)
|
|
|
|
if (x is None):
|
|
continue
|
|
|
|
y: float = m * x + p
|
|
|
|
intersections.append(Point(x, y))
|
|
|
|
return intersections
|
|
|
|
def determine_distance_between_ball_and_wall(ball: Ball, segments: list[Segment]):
|
|
|
|
intersections: list[Point] = determine_intersections(ball, segments)
|
|
|
|
distances = list(map(math.dist, intersections))
|
|
|
|
return min(distances)
|
|
|
|
def render(ball: Ball, game: Game):
|
|
|
|
segments: list[Segment] = [player.rail for player in game.players]
|
|
|
|
print(determine_distance_between_ball_and_wall(ball))
|
|
|
|
def routine(game: Game):
|
|
|
|
while True:
|
|
for player in game._updated_players:
|
|
game.broadcast("update_paddle", player.to_dict(), [player])
|
|
|
|
game._updated_players.clear()
|
|
|
|
if (game.started):
|
|
game.ball.postion_x = game.ball.postion_x + game.ball.velocity_x
|
|
game.ball.postion_y = game.ball.postion_y + game.ball.velocity_y
|
|
|
|
game.broadcast("update_ball", game.ball.to_dict())
|
|
|
|
sleep(1 / config.SERVER_TPS) |