From: Neil Date: Mon, 5 Sep 2016 20:12:10 +0000 (+0200) Subject: Add very hack'ish rotating light via pymunk thing X-Git-Tag: tabakrolletjie-v1.0.0~239 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=bb98345153a9111d9af5b3d24e6a00037b59b977;p=tabakrolletjie.git Add very hack'ish rotating light via pymunk thing --- diff --git a/misc/README b/misc/README new file mode 100644 index 0000000..777fa16 --- /dev/null +++ b/misc/README @@ -0,0 +1,3 @@ +This is a collection of experimental scripts and things generated while experimenting with abusing pymunk to do what we want. + +Ideas which work will hopefully be integrated into the game proper. diff --git a/misc/intersecting_triangles.py b/misc/intersecting_triangles.py new file mode 100644 index 0000000..43cf0fc --- /dev/null +++ b/misc/intersecting_triangles.py @@ -0,0 +1,103 @@ +import pymunk as p +import pymunk.pygame_util as pgu +import pygame +import pygame.locals as pgl +import pygame.draw +import pygame.time + +import random + + +pygame.display.set_mode((800, 600), pgl.SWSURFACE) +pygame.display.init() + +step = 0.01 +#step = 0 + +space = p.Space() + +shape_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS) +s1_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS^1, categories=1) +s2_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS^2, categories=2) + +b1 = p.Body(0, 0, p.Body.STATIC) +b2 = p.Body(0, 0, p.Body.STATIC) + +b1.position = (400, 350) +b2.position = (400, 350) + +s1 = p.Poly(b1, ((0, 0), (100, 100), (100, 0))) +s3 = p.Poly(b1, ((0, 0), (130, -100), (130, 0))) +s4 = p.Poly(b1, ((0, 0), (random.randint(-150, -50), random.randint(-150, -50)), + (random.randint(-150, -50), -200))) +s2 = p.Poly(b2, ((0, 0), (900, 50), (900, 500))) + + + +space.add(b1, s1, s3, s4) +#space.add(b2, s2) + +#s1.filter = s1_filter +#s2.filter = s2_filter + +running = True + +surface = pygame.display.get_surface() + +clock = pygame.time.Clock() + +b2.angle -= 0.15 + + +while running: + for ev in pygame.event.get(): + if ev == pgl.QUIT: + running = False + if ev.type == pgl.KEYDOWN and ev.key == pgl.K_q: + running = False + if ev.type == pgl.KEYDOWN and ev.key == pgl.K_ESCAPE: + running = False + surface.fill((0, 0, 0)) + + # stationary triangle2 + for i, s in enumerate(b1.shapes): + points = [pgu.to_pygame(b1.local_to_world(x), surface) for x in s.get_vertices()] + pygame.draw.polygon(surface, (128, 0, i*64), points) + + # moving triangle + b2.angle += step + + points = [pgu.to_pygame(b2.local_to_world(x), surface) for x in s2.get_vertices()] + pygame.draw.polygon(surface, (64, 64, 0), points) + + hits = space.shape_query(s2) + if hits: + points = [b2.local_to_world(x) for x in s2.get_vertices()] + info1 = space.segment_query_first(points[1], points[0], 0.1, shape_filter) + info2 = space.segment_query_first(points[2], points[0], 0.1, shape_filter) + + for s in b1.shapes: + draw_points = [pgu.to_pygame(points[0], surface)] + for x in s.get_vertices(): + x = b1.local_to_world(x) + for ss in b2.shapes: + query = ss.point_query(x) + if query.distance < -0.01: + #print x, query + draw_points.append(pgu.to_pygame(x, surface)) + if info1 and info1.point.get_dist_sqrd(points[0]) > 5 and info1.shape is s: + p1 = pgu.to_pygame(info1.point, surface) + pygame.draw.circle(surface, (0, 255, 0), p1, 1) + draw_points.append(p1) + if info2 and info2.point.get_dist_sqrd(points[0]) > 5 and info2.shape is s: + p2 = pgu.to_pygame(info2.point, surface) + pygame.draw.circle(surface, (0, 0, 255), p2, 1) + draw_points.append(p2) + #print draw_points, info1, info2 + if len(draw_points) >= 3: + pygame.draw.polygon(surface, (128, 128, 0), draw_points) + + + pygame.display.flip() + + clock.tick(60)