Skip to content

Commit 083171e

Browse files
committed
wip benchmarks
1 parent ec0f2fd commit 083171e

File tree

1 file changed

+115
-31
lines changed

1 file changed

+115
-31
lines changed

benchmarks/chipmunk.py

+115-31
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@
2222
"""
2323

2424

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)
3143

3244
for i in range(15):
3345
a = 0.5 + i / 15 * 2.5
@@ -37,15 +49,15 @@ def __init__(self, space, size):
3749

3850
p = pymunk.Poly.create_box(b, (a, a))
3951
p.density = 5
40-
space.add(b, p)
52+
self.space.add(b, p)
4153

4254

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)
4961

5062
for i in range(15):
5163
a = 0.5 + i / 15 * 2.5
@@ -54,11 +66,13 @@ def __init__(self, space, size):
5466
b.position = i * 7 + j * 0.25 - 60, -2 * a * (size - j)
5567
c = pymunk.Circle(b, a / 2)
5668
c.density = 5
57-
space.add(b, c)
69+
self.space.add(b, c)
5870

5971

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
6276

6377
for i in range(size):
6478
b = pymunk.Body()
@@ -69,14 +83,14 @@ def __init__(self, space, size):
6983
y = math.sin(s * 30) * (s * 30 + 5)
7084
b.position = pymunk.Vec2d(x, y)
7185
b.velocity = 0.2 * b.position
72-
space.add(b, c)
86+
self.space.add(b, c)
7387

7488

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)
8094

8195
# table
8296
for i in range(size):
@@ -98,7 +112,7 @@ def __init__(self, space, size):
98112
top.density = 2
99113
left.density = 2
100114

101-
space.add(top, left, right, b)
115+
self.space.add(top, left, right, b)
102116

103117
# triangle
104118
for i in range(size):
@@ -110,12 +124,12 @@ def __init__(self, space, size):
110124
right = pymunk.Poly(b, [(2, 0), (-1, -2), (0, -4)])
111125
right.density = 3
112126

113-
space.add(b, left, right)
127+
self.space.add(b, left, right)
114128

115129

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__()
119133
# ground = pymunk.Segment(space.static_body, (-50, 0), (50, 0), 1)
120134
# space.add(ground)
121135

@@ -124,7 +138,72 @@ def __init__(self, space, size):
124138
b.position = (i * 0.01, -i * 0.01)
125139
c = pymunk.Circle(b, 1)
126140
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 &center, 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
128207

129208

130209
tests = [SlowExplosion, FallingSquares]
@@ -135,12 +214,17 @@ def __init__(self, space, size):
135214
space = pymunk.Space()
136215

137216
draw_options = pymunk.pygame_util.DrawOptions(screen)
217+
draw_options.flags = draw_options.DRAW_SHAPES
218+
138219
# FallingSquares(space, 200)
139220
# FallingCircles(space, 200)
140221
# MildN2(space, 50)
141-
N2(space, 500)
222+
# N2(space, 500)
223+
# Diagonal(space, 100)
142224
# SlowExplosion(space, 5000)
143225

226+
sim = Tumbler(1000)
227+
144228
translation = pymunk.Transform()
145229
scaling = 2
146230

@@ -165,11 +249,11 @@ def __init__(self, space, size):
165249

166250
fps = 60.0
167251
dt = 1.0 / fps
168-
space.step(dt)
252+
sim.update(dt)
169253
steps += 1
170254

171255
screen.fill(pygame.Color("white"))
172-
space.debug_draw(draw_options)
256+
sim.draw(draw_options)
173257
pygame.display.flip()
174258

175259
clock.tick(fps)

0 commit comments

Comments
 (0)