22
22
"""
23
23
24
24
25
- class FallingSquares :
26
- def __init__ (self , space , size ):
27
- space .gravity = 0 , 10
28
- space .static_body .position = 0 , 10
29
- ground = pymunk .Poly .create_box (space .static_body , (200 , 20 ))
30
- space .add (ground )
25
+ class Benchmark :
26
+ def __init__ (self ):
27
+ self .space = pymunk .Space ()
28
+ self .space .gravity = 0 , 10
29
+
30
+ def update (self , dt ):
31
+ self .space .step (dt )
32
+
33
+ def draw (self , draw_options ):
34
+ self .space .debug_draw (draw_options )
35
+
36
+
37
+ class FallingSquares (Benchmark ):
38
+ def __init__ (self , size ):
39
+ super ().__init__ ()
40
+ self .space .static_body .position = 0 , 10
41
+ ground = pymunk .Poly .create_box (self .space .static_body , (200 , 20 ))
42
+ self .space .add (ground )
31
43
32
44
for i in range (15 ):
33
45
a = 0.5 + i / 15 * 2.5
@@ -37,15 +49,15 @@ def __init__(self, space, size):
37
49
38
50
p = pymunk .Poly .create_box (b , (a , a ))
39
51
p .density = 5
40
- space .add (b , p )
52
+ self . space .add (b , p )
41
53
42
54
43
- class FallingCircles :
44
- def __init__ (self , space , size ):
45
- space . gravity = 0 , 10
46
- space .static_body .position = 0 , 10
47
- ground = pymunk .Poly .create_box (space .static_body , (200 , 20 ))
48
- space .add (ground )
55
+ class FallingCircles ( Benchmark ) :
56
+ def __init__ (self , size ):
57
+ super (). __init__ ()
58
+ self . space .static_body .position = 0 , 10
59
+ ground = pymunk .Poly .create_box (self . space .static_body , (200 , 20 ))
60
+ self . space .add (ground )
49
61
50
62
for i in range (15 ):
51
63
a = 0.5 + i / 15 * 2.5
@@ -54,11 +66,13 @@ def __init__(self, space, size):
54
66
b .position = i * 7 + j * 0.25 - 60 , - 2 * a * (size - j )
55
67
c = pymunk .Circle (b , a / 2 )
56
68
c .density = 5
57
- space .add (b , c )
69
+ self . space .add (b , c )
58
70
59
71
60
- class SlowExplosion :
61
- def __init__ (self , space , size ):
72
+ class SlowExplosion (Benchmark ):
73
+ def __init__ (self , size ):
74
+ super ().__init__ ()
75
+ self .space .gravity = 0 , 0
62
76
63
77
for i in range (size ):
64
78
b = pymunk .Body ()
@@ -69,14 +83,14 @@ def __init__(self, space, size):
69
83
y = math .sin (s * 30 ) * (s * 30 + 5 )
70
84
b .position = pymunk .Vec2d (x , y )
71
85
b .velocity = 0.2 * b .position
72
- space .add (b , c )
86
+ self . space .add (b , c )
73
87
74
88
75
- class MildN2 :
76
- def __init__ (self , space , size ):
77
- space . gravity = 0 , 10
78
- ground = pymunk .Segment (space .static_body , (- 50 , 0 ), (50 , 0 ), 1 )
79
- space .add (ground )
89
+ class MildN2 ( Benchmark ) :
90
+ def __init__ (self , size ):
91
+ super (). __init__ ()
92
+ ground = pymunk .Segment (self . space .static_body , (- 50 , 0 ), (50 , 0 ), 1 )
93
+ self . space .add (ground )
80
94
81
95
# table
82
96
for i in range (size ):
@@ -98,7 +112,7 @@ def __init__(self, space, size):
98
112
top .density = 2
99
113
left .density = 2
100
114
101
- space .add (top , left , right , b )
115
+ self . space .add (top , left , right , b )
102
116
103
117
# triangle
104
118
for i in range (size ):
@@ -110,12 +124,12 @@ def __init__(self, space, size):
110
124
right = pymunk .Poly (b , [(2 , 0 ), (- 1 , - 2 ), (0 , - 4 )])
111
125
right .density = 3
112
126
113
- space .add (b , left , right )
127
+ self . space .add (b , left , right )
114
128
115
129
116
- class N2 :
117
- def __init__ (self , space , size ):
118
- space . gravity = 0 , 10
130
+ class N2 ( Benchmark ) :
131
+ def __init__ (self , size ):
132
+ super (). __init__ ()
119
133
# ground = pymunk.Segment(space.static_body, (-50, 0), (50, 0), 1)
120
134
# space.add(ground)
121
135
@@ -124,7 +138,72 @@ def __init__(self, space, size):
124
138
b .position = (i * 0.01 , - i * 0.01 )
125
139
c = pymunk .Circle (b , 1 )
126
140
c .density = 1
127
- space .add (b , c )
141
+ self .space .add (b , c )
142
+
143
+
144
+ class Diagonal (Benchmark ):
145
+ def __init__ (self , size ):
146
+ super ().__init__ ()
147
+
148
+ a = 0.5
149
+ N = size
150
+ M = size / 2
151
+ position = pymunk .Vec2d (0 , 0 )
152
+ for j in range (int (M )):
153
+ position = pymunk .Vec2d (- N * a * 3 , position .y )
154
+
155
+ for i in range (N ):
156
+ b = pymunk .Body (body_type = pymunk .Body .STATIC )
157
+ # (float hx, float hy, const b2Vec2 ¢er, float angle)
158
+
159
+ b .position = position
160
+ b .angle = math .pi / 4
161
+ s = pymunk .Poly .create_box (b , (a , ((3 * j + 1 ) * a )))
162
+ self .space .add (b , s )
163
+ position = position + ((8 * a ), 0 )
164
+ position = position - (0 , 8 * a )
165
+
166
+ for i in range (3000 ):
167
+ b = pymunk .Body ()
168
+ b .position = (i / 15 ) * 2 - 75 , - (i % 15 * 2 ) - 50
169
+
170
+ c = pymunk .Circle (b , 0.5 )
171
+ c .density = 1
172
+
173
+ self .space .add (b , c )
174
+
175
+
176
+ class Tumbler (Benchmark ):
177
+ def __init__ (self , size ):
178
+ super ().__init__ ()
179
+
180
+ self .m_count = 0
181
+ self .e_count = size
182
+
183
+ b = pymunk .Body ()
184
+ b .position = 0 , 10
185
+ s1 = pymunk .Segment (b , (- 10 , 10 ), (10 , 10 ), 0.5 )
186
+ s2 = pymunk .Segment (b , (10 , 10 ), (10 , - 10 ), 0.5 )
187
+ s3 = pymunk .Segment (b , (10 , - 10 ), (- 10 , - 10 ), 0.5 )
188
+ s4 = pymunk .Segment (b , (- 10 , - 10 ), (- 10 , 10 ), 0.5 )
189
+ s1 .density = 5
190
+ s2 .density = 5
191
+ s3 .density = 5
192
+ s4 .density = 5
193
+
194
+ j1 = pymunk .constraints .SimpleMotor (self .space .static_body , b , 1 )
195
+ j2 = pymunk .constraints .PinJoint (self .space .static_body , b , (0 , 0 ), (0 , 0 ))
196
+ self .space .add (b , s1 , s2 , s3 , s4 , j1 , j2 )
197
+
198
+ def update (self , dt ):
199
+ super ().update (dt )
200
+ if self .m_count < self .e_count :
201
+ b = pymunk .Body ()
202
+ b .position = 0 , 10
203
+ s = pymunk .Poly .create_box (b , (0.125 , 0.125 ))
204
+ s .density = 1
205
+ self .space .add (b , s )
206
+ self .m_count += 1
128
207
129
208
130
209
tests = [SlowExplosion , FallingSquares ]
@@ -135,12 +214,17 @@ def __init__(self, space, size):
135
214
space = pymunk .Space ()
136
215
137
216
draw_options = pymunk .pygame_util .DrawOptions (screen )
217
+ draw_options .flags = draw_options .DRAW_SHAPES
218
+
138
219
# FallingSquares(space, 200)
139
220
# FallingCircles(space, 200)
140
221
# MildN2(space, 50)
141
- N2 (space , 500 )
222
+ # N2(space, 500)
223
+ # Diagonal(space, 100)
142
224
# SlowExplosion(space, 5000)
143
225
226
+ sim = Tumbler (1000 )
227
+
144
228
translation = pymunk .Transform ()
145
229
scaling = 2
146
230
@@ -165,11 +249,11 @@ def __init__(self, space, size):
165
249
166
250
fps = 60.0
167
251
dt = 1.0 / fps
168
- space . step (dt )
252
+ sim . update (dt )
169
253
steps += 1
170
254
171
255
screen .fill (pygame .Color ("white" ))
172
- space . debug_draw (draw_options )
256
+ sim . draw (draw_options )
173
257
pygame .display .flip ()
174
258
175
259
clock .tick (fps )
0 commit comments