2 import pymunk.pygame_util as pgu
4 import pygame.locals as pgl
11 pygame.display.set_mode((800, 600), pgl.SWSURFACE)
19 shape_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS)
20 s1_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS^1, categories=1)
21 s2_filter = p.ShapeFilter(mask=p.ShapeFilter.ALL_MASKS^2, categories=2)
23 b1 = p.Body(0, 0, p.Body.STATIC)
24 b2 = p.Body(0, 0, p.Body.STATIC)
26 b1.position = (400, 350)
27 b2.position = (400, 350)
29 s1 = p.Poly(b1, ((0, 0), (100, 100), (100, 0)))
30 s3 = p.Poly(b1, ((0, 0), (130, -100), (130, 0)))
31 s4 = p.Poly(b1, ((0, 0), (random.randint(-150, -50), random.randint(-150, -50)),
32 (random.randint(-150, -50), -200)))
33 s2 = p.Poly(b2, ((0, 0), (900, 50), (900, 500)))
37 space.add(b1, s1, s3, s4)
40 #s1.filter = s1_filter
41 #s2.filter = s2_filter
45 surface = pygame.display.get_surface()
47 clock = pygame.time.Clock()
53 for ev in pygame.event.get():
56 if ev.type == pgl.KEYDOWN and ev.key == pgl.K_q:
58 if ev.type == pgl.KEYDOWN and ev.key == pgl.K_ESCAPE:
60 surface.fill((0, 0, 0))
62 # stationary triangle2
63 for i, s in enumerate(b1.shapes):
64 points = [pgu.to_pygame(b1.local_to_world(x), surface) for x in s.get_vertices()]
65 pygame.draw.polygon(surface, (128, 0, i*64), points)
70 points = [pgu.to_pygame(b2.local_to_world(x), surface) for x in s2.get_vertices()]
71 pygame.draw.polygon(surface, (64, 64, 0), points)
73 hits = space.shape_query(s2)
75 points = [b2.local_to_world(x) for x in s2.get_vertices()]
76 info1 = space.segment_query_first(points[1], points[0], 0.1, shape_filter)
77 info2 = space.segment_query_first(points[2], points[0], 0.1, shape_filter)
80 draw_points = [pgu.to_pygame(points[0], surface)]
81 for x in s.get_vertices():
82 x = b1.local_to_world(x)
84 query = ss.point_query(x)
85 if query.distance < -0.01:
87 draw_points.append(pgu.to_pygame(x, surface))
88 if info1 and info1.point.get_dist_sqrd(points[0]) > 5 and info1.shape is s:
89 p1 = pgu.to_pygame(info1.point, surface)
90 pygame.draw.circle(surface, (0, 255, 0), p1, 1)
91 draw_points.append(p1)
92 if info2 and info2.point.get_dist_sqrd(points[0]) > 5 and info2.shape is s:
93 p2 = pgu.to_pygame(info2.point, surface)
94 pygame.draw.circle(surface, (0, 0, 255), p2, 1)
95 draw_points.append(p2)
96 #print draw_points, info1, info2
97 if len(draw_points) >= 3:
98 pygame.draw.polygon(surface, (128, 128, 0), draw_points)
101 pygame.display.flip()