From d45b8a306f39ed00fad1483019bc632200191617 Mon Sep 17 00:00:00 2001 From: starnakin Date: Fri, 2 Feb 2024 17:39:39 +0100 Subject: [PATCH] game: collision work --- games/objects/Ball.py | 2 +- games/objects/Segment.py | 3 ++ games/routine.py | 66 +++++++++++++++++----------------------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/games/objects/Ball.py b/games/objects/Ball.py index 77f4896..bb8b8ab 100644 --- a/games/objects/Ball.py +++ b/games/objects/Ball.py @@ -10,7 +10,7 @@ class Ball: def __init__(self) -> None: self.size: float = config.BALL_SIZE self.position: Point = Point(config.BALL_SPAWN_POS_X + self.size / 2, config.BALL_SPAWN_POS_Y + self.size / 2) - self.angle: float = math.pi * 0 + self.angle: float = math.pi * 0.25 self.speed: float = config.BALL_SPEED_START def to_dict(self): diff --git a/games/objects/Segment.py b/games/objects/Segment.py index 49e3d88..b785756 100644 --- a/games/objects/Segment.py +++ b/games/objects/Segment.py @@ -15,6 +15,9 @@ class Segment: def __str__(self) -> str: return f"Segment(start: {self.start}, stop: {self.stop})" + def copy(self): + return Segment(self.start.copy(), self.stop.copy()) + def to_dict(self): data: dict[str: dict] = { diff --git a/games/routine.py b/games/routine.py index f02ce05..71e9521 100644 --- a/games/routine.py +++ b/games/routine.py @@ -15,6 +15,8 @@ from .objects.Vector import Vector from . import config +from . import config + import math import asyncio @@ -65,16 +67,21 @@ def get_interception(segment1: Segment, segment2: Segment): if (identify(segment1) == VERTICALLY and identify(segment2) == VERTICALLY): return None + + # because of in matematics world y = 10 is above y = 5 and on a display it is inverted I invert the coordonate + + tmp1 = Segment(Point(segment1.start.x, config.MAP_SIZE_Y - segment1.start.y), Point(segment1.stop.x, config.MAP_SIZE_Y - segment1.stop.y)) + tmp2 = Segment(Point(segment2.start.x, config.MAP_SIZE_Y - segment2.start.y), Point(segment2.stop.x, config.MAP_SIZE_Y - segment2.stop.y)) if (identify(segment1) == NORMAL and identify(segment2) == NORMAL): # representation m * x + p - - m1 = get_derive(segment1) - m2 = get_derive(segment2) + + m1 = get_derive(tmp1) + m2 = get_derive(tmp2) - p1 = get_intercept(m1, segment1.start) - p2 = get_intercept(m2, segment2.start) + p1 = get_intercept(m1, tmp1.start) + p2 = get_intercept(m2, tmp2.start) # m1 * x + p1 = m2 * x + p2 # m1 * x = m2 * x + p2 -p1 @@ -83,27 +90,27 @@ def get_interception(segment1: Segment, segment2: Segment): # x = (p1 - p2) / (m1 - m2) if (m1 == m2): return None - else: - x: float = (p1 - p2) / (m1 - m2) - - y: float = m1 * x + p1 + # reinvert + x: float = (p1 - p2) / (m1 - m2) * (-1) + + y: float = config.MAP_SIZE_Y - (m1 * x + p1) else: - if (identify(segment1) == VERTICALLY): - constant: float = get_constant(segment1) - m: float = get_derive(segment2) - p: float = get_intercept(m, segment2.start) + if (identify(tmp1) == VERTICALLY): + constant: float = get_constant(tmp1) + m: float = get_derive(tmp2) + p: float = get_intercept(m, tmp2.start) else: - constant: float = get_constant(segment2) - m: float = get_derive(segment1) - p: float = get_intercept(m, segment1.start) + constant: float = get_constant(tmp2) + m: float = get_derive(tmp1) + p: float = get_intercept(m, tmp1.start) x: float = constant - y: float = m * x + p + y: float = config.MAP_SIZE_Y - (m * x + p) - impact: Point = Point(x,y) + impact: Point = Point(x, y) return impact @@ -112,14 +119,8 @@ def get_impact_point(segments: list[Segment], ball: Ball): cos: float = round(math.cos(ball.angle), 6) sin: float = round(math.sin(ball.angle), 6) - print("cos:", cos) - print("sin:", sin) - point: Point = Point(ball.position.x + cos, ball.position.y - sin) - print(ball.position) - print(point) - ball_segment = Segment(ball.position, point) closest: Point = None @@ -127,23 +128,17 @@ def get_impact_point(segments: list[Segment], ball: Ball): for segment in segments: impact: Point = get_interception(segment, ball_segment) - + if (impact is None): continue diff_x: float = ball.position.x - impact.x - print("x:", diff_x) if (get_sign(diff_x) == get_sign(cos) and cos != 0): continue - print("OK: X") - diff_y: float = ball.position.y - impact.y - print("y:", diff_y) + diff_y: float = (ball.position.y - impact.y) if (get_sign(diff_y) != get_sign(sin) and sin != 0): continue - print("OK: Y") - - print("OK: PARFAIT", impact) impact.x += (ball.size / 2) * get_sign(cos) * (-1) impact.y += (ball.size / 2) * get_sign(sin) @@ -157,12 +152,8 @@ async def update_ball(game: Game, impact: Point): distance: float = impact.distance(game.ball.position) - print("distance:", distance) - time_before_impact: float = distance / game.ball.speed - print("time:", time_before_impact) - await asyncio.sleep(time_before_impact) game.ball.angle = game.ball.angle + math.pi @@ -177,10 +168,9 @@ async def render(game: Game): segments: list[Segment] = [player.rail for player in game.players] + [wall.rail for wall in game.walls] - impact = get_impact_point(segments, game.ball) + impact = get_impact_point(segments, game.ball, ) await update_ball(game, impact) - print("bozo") def routine(game: Game):