Add count down bar.
[tabakrolletjie.git] / misc / intersecting_triangles.py
1 import pymunk as p
2 import pymunk.pygame_util as pgu
3 import pygame
4 import pygame.locals as pgl
5 import pygame.draw
6 import pygame.time
7
8 import random
9
10
11 pygame.display.set_mode((800, 600), pgl.SWSURFACE)
12 pygame.display.init()
13
14 step = 0.01
15 #step = 0
16
17 space = p.Space()
18
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)
22
23 b1 = p.Body(0, 0, p.Body.STATIC)
24 b2 = p.Body(0, 0, p.Body.STATIC)
25
26 b1.position = (400, 350)
27 b2.position = (400, 350)
28
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)))
34
35
36
37 space.add(b1, s1, s3, s4)
38 #space.add(b2, s2)
39
40 #s1.filter = s1_filter
41 #s2.filter = s2_filter
42
43 running = True
44
45 surface = pygame.display.get_surface()
46
47 clock = pygame.time.Clock()
48
49 b2.angle -= 0.15
50
51
52 while running:
53     for ev in pygame.event.get():
54         if ev == pgl.QUIT:
55             running = False
56         if ev.type == pgl.KEYDOWN and ev.key == pgl.K_q:
57             running = False
58         if ev.type == pgl.KEYDOWN and ev.key == pgl.K_ESCAPE:
59             running = False
60     surface.fill((0, 0, 0))
61
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)
66
67     # moving triangle
68     b2.angle += step
69
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)
72
73     hits = space.shape_query(s2)
74     if hits:
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)
78
79         for s in b1.shapes:
80             draw_points = [pgu.to_pygame(points[0], surface)]
81             for x in s.get_vertices():
82                 x = b1.local_to_world(x)
83                 for ss in b2.shapes:
84                     query = ss.point_query(x)
85                     if query.distance < -0.01:
86                         #print x, query
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)
99
100
101     pygame.display.flip()
102
103     clock.tick(60)