Skip to content

Line collidepolygon() and Polygon collideline() #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 6, 2023
Merged
Prev Previous commit
Next Next commit
Added python example
  • Loading branch information
itzpr3d4t0r committed Feb 13, 2023
commit 15811104edbd4e6f381c21e3da59d139f46bf623
105 changes: 105 additions & 0 deletions examples/polygon_line_collision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from math import radians, sin, cos

import pygame
from pygame.draw import line as draw_line, polygon as draw_polygon
from geometry import Line, regular_polygon


# using this because we're missing line.rotate()
def rotate_line(line, angle):
angle = radians(angle)

x = line.x2 - line.x1
y = line.y2 - line.y1

x1 = x * cos(angle) - y * sin(angle)
y1 = x * sin(angle) + y * cos(angle)

line.b = (x1 + line.x1, y1 + line.y1)


pygame.init()

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
SHAPE_THICKNESS = 3
FPS = 60
WIDTH, HEIGHT = 800, 800
WIDTH2, HEIGHT2 = WIDTH // 2, HEIGHT // 2

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Polygon-Line Collision Visualization")
clock = pygame.time.Clock()

font = pygame.font.SysFont("Arial", 25, bold=True)
colliding_text = font.render("Colliding", True, RED)
colliding_textr = colliding_text.get_rect(center=(WIDTH2, 50))
not_colliding_text = font.render("Not colliding", True, WHITE)
not_colliding_textr = not_colliding_text.get_rect(center=(WIDTH2, 50))

modei_text = font.render("Right click to toggle collision mode", True, WHITE)
modei_textr = modei_text.get_rect(center=(WIDTH2, HEIGHT - 50))

modei2_text = font.render("Left click to rotate", True, WHITE)
modei2_textr = modei2_text.get_rect(center=(WIDTH2, HEIGHT - 80))

mode0_txt = font.render("Current: EDGES ONLY", True, YELLOW)
mode0_txtr = mode0_txt.get_rect(center=(WIDTH2, HEIGHT - 25))

mode1_txt = font.render("Current: FULL", True, YELLOW)
mode1_txtr = mode1_txt.get_rect(center=(WIDTH2, HEIGHT - 25))

polygon = regular_polygon(6, (WIDTH2, HEIGHT2), 180)
line = Line((0, 0), (135, 135))
only_edges = False
running = True
color = WHITE

rotating = False

while running:
delta = (clock.tick(FPS) / 1000) * 60

line.midpoint = pygame.mouse.get_pos()

colliding = line.collidepolygon(polygon, only_edges)
# Alternative:
# colliding = polygon.collideline(line, only_edges)

if rotating:
rotate_line(line, 2)

color = RED if colliding else WHITE

screen.fill((10, 10, 10))

draw_polygon(screen, color, polygon.vertices, SHAPE_THICKNESS)
draw_line(screen, color, line.a, line.b, SHAPE_THICKNESS)

screen.blit(
colliding_text if colliding else not_colliding_text,
colliding_textr if colliding else not_colliding_textr,
)

screen.blit(modei2_text, modei2_textr)
screen.blit(modei_text, modei_textr)

screen.blit(
mode0_txt if only_edges else mode1_txt, mode0_txtr if only_edges else mode1_txtr
)

pygame.display.flip()

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
rotating = True
elif event.button == 3:
only_edges = not only_edges
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
rotating = False
pygame.quit()