2020-11-15 01:48:07 -07:00
|
|
|
import pygame
|
2020-11-15 01:24:53 -07:00
|
|
|
from ..geom import Polygon, generate_polygon
|
2020-11-14 13:24:44 -07:00
|
|
|
from .. import WIDTH, HEIGHT, get_cursor_pos, get_inputs
|
2020-11-14 11:12:00 -07:00
|
|
|
from ..colors import *
|
|
|
|
from ..math import *
|
|
|
|
from ..draw import *
|
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
|
2020-11-14 11:12:00 -07:00
|
|
|
class SeparatingAxisTheorem:
|
|
|
|
|
|
|
|
title = "Separating Axis Theorem"
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-11-15 01:24:53 -07:00
|
|
|
self.shape1 = generate_polygon(0, 0, sides=4, radius=60)
|
2020-11-14 11:12:00 -07:00
|
|
|
|
2020-11-15 01:48:07 -07:00
|
|
|
self.shape2 = None
|
|
|
|
self.shape2_sides = 4
|
|
|
|
self.shape2_radius = 60
|
|
|
|
self.generate_shape2()
|
|
|
|
|
|
|
|
def generate_shape2(self):
|
|
|
|
rotation = 45 if self.shape2 is None else self.shape2.rotation
|
|
|
|
self.shape2 = generate_polygon(*get_cursor_pos(),
|
|
|
|
rotation,
|
|
|
|
sides=self.shape2_sides,
|
|
|
|
radius=self.shape2_radius)
|
|
|
|
|
|
|
|
def handle_key_down(self, key):
|
|
|
|
if key == pygame.K_a:
|
|
|
|
self.shape2_sides = max(self.shape2_sides - 1, 3)
|
|
|
|
self.generate_shape2()
|
|
|
|
if key == pygame.K_d:
|
|
|
|
self.shape2_sides = min(self.shape2_sides + 1, 31)
|
|
|
|
self.generate_shape2()
|
2020-11-14 11:12:00 -07:00
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
def handle_input(self):
|
2020-11-14 11:12:00 -07:00
|
|
|
self.shape2.set_position(*get_cursor_pos())
|
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
inputs = get_inputs()
|
|
|
|
if inputs[pygame.K_q]:
|
|
|
|
self.shape2.rotate((1/60) * -90)
|
|
|
|
if inputs[pygame.K_e]:
|
|
|
|
self.shape2.rotate((1/60) * 90)
|
|
|
|
|
2020-11-15 02:26:20 -07:00
|
|
|
def draw_axis(self, surface, normal, overlap, min1, max1, min2, max2):
|
2020-11-14 13:24:44 -07:00
|
|
|
offset = scale(*rnormal(*normal), 300)
|
|
|
|
|
2020-11-15 02:26:20 -07:00
|
|
|
colliding = overlap < 0
|
|
|
|
|
|
|
|
if not colliding:
|
|
|
|
# separating line
|
|
|
|
dist = scale(*normal,
|
|
|
|
(min1 if min1 > min2 else min2) - overlap * 0.5)
|
|
|
|
line(surface, BLUE,
|
|
|
|
add(*dist, *offset),
|
|
|
|
add(*dist, *scale(*lnormal(*normal), 1000)))
|
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
# axis
|
|
|
|
line(surface, WHITE,
|
|
|
|
add(*scale(*reverse(*normal), 1000), *offset),
|
|
|
|
add(*scale(*normal, 1000), *offset))
|
|
|
|
|
|
|
|
# shape 1
|
2020-11-15 01:21:51 -07:00
|
|
|
line(surface, GREEN if not colliding else RED,
|
2020-11-14 13:24:44 -07:00
|
|
|
add(*scale(*normal, min1), *offset),
|
2020-11-15 01:21:51 -07:00
|
|
|
add(*scale(*normal, max1), *offset), 8)
|
2020-11-14 13:24:44 -07:00
|
|
|
# shape 2
|
2020-11-15 01:21:51 -07:00
|
|
|
line(surface, GREEN if not colliding else RED,
|
2020-11-14 13:24:44 -07:00
|
|
|
add(*scale(*normal, min2), *offset),
|
2020-11-15 01:21:51 -07:00
|
|
|
add(*scale(*normal, max2), *offset), 8)
|
2020-11-14 13:24:44 -07:00
|
|
|
|
|
|
|
def render(self, surface):
|
|
|
|
self.handle_input()
|
|
|
|
|
2020-11-14 11:12:00 -07:00
|
|
|
normals = []
|
|
|
|
|
|
|
|
def add_if_not_exists(normal):
|
|
|
|
for existing in normals:
|
|
|
|
if is_equal(*existing, *normal):
|
|
|
|
return
|
|
|
|
normals.append(normal)
|
|
|
|
|
|
|
|
for normal in self.shape1.get_normals() + self.shape2.get_normals():
|
|
|
|
add_if_not_exists(normal)
|
|
|
|
|
|
|
|
def get_min_max(normal, vertices):
|
|
|
|
_min = float('inf')
|
|
|
|
_max = float('-inf')
|
|
|
|
for vertex in vertices:
|
|
|
|
proj = dot(*normal, *vertex)
|
|
|
|
_min = min(_min, proj)
|
|
|
|
_max = max(_max, proj)
|
|
|
|
return _min, _max
|
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
overlaps = []
|
2020-11-14 11:12:00 -07:00
|
|
|
for normal in normals:
|
|
|
|
min1, max1 = get_min_max(normal,
|
|
|
|
self.shape1.get_translated_vertices())
|
|
|
|
min2, max2 = get_min_max(normal,
|
|
|
|
self.shape2.get_translated_vertices())
|
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
overlap = max(min2 - max1, min1 - max2)
|
|
|
|
overlaps.append(overlap)
|
2020-11-14 11:12:00 -07:00
|
|
|
|
2020-11-15 02:26:20 -07:00
|
|
|
self.draw_axis(surface, normal, overlap,
|
2020-11-15 01:24:53 -07:00
|
|
|
min1, max1, min2, max2)
|
2020-11-14 11:12:00 -07:00
|
|
|
|
2020-11-14 13:24:44 -07:00
|
|
|
colliding = all(overlap < 0 for overlap in overlaps)
|
|
|
|
if colliding:
|
|
|
|
text_screen(surface, WHITE, (10, HEIGHT - 20),
|
|
|
|
f"Min Distance to Resolve: {max(overlaps)}")
|
2020-11-14 11:12:00 -07:00
|
|
|
|
2020-11-15 02:26:20 -07:00
|
|
|
self.shape1.draw(surface, YELLOW, 1)
|
|
|
|
self.shape2.draw(surface, RED if colliding else WHITE, 1)
|