/
engine.py
406 lines (345 loc) · 12.4 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
'''
This runs the box2d engine with pygame visualizations
'''
import Box2D
import Box2D.b2 as b2
import pygame
import pygame.font
import pygame.gfxdraw
import sys
# =====
# Globals
# =====
PPM = 30.0 # Pixels per meter
TARGET_FPS = 60
TIME_STEP = 1.0 / TARGET_FPS
SCREEN_WIDTH, SCREEN_HEIGHT = 1000, 800
GROUND_WIDTH = 300
GROUND_START = GROUND_WIDTH - 40
# ======
# Accessory functions
# =====
def usim_draw_poly(polygon, body, fixture, screen, camera_x=0, camera_y=0):
vertices = [(body.transform * v) * PPM for v in polygon.vertices]
vertices = [(v[0] - camera_x, SCREEN_HEIGHT - v[1] - camera_y) for v in vertices]
pygame.draw.polygon(screen, body.color, vertices)
if body.userData['name'] == 'ground':
# Texture ground
try:
grass = pygame.image.load('grass.jpg').convert()
pygame.gfxdraw.textured_polygon(
screen, vertices, grass, int(-camera_x), int(-camera_y)
)
except:
pass
b2.polygonShape.draw = usim_draw_poly
def usim_draw_circle(circle, body, fixture, screen, camera_x=0, camera_y=0):
position = body.transform * circle.pos * PPM
position = (position[0] - camera_x, SCREEN_HEIGHT - position[1] - camera_y)
pygame.draw.circle(
screen, body.color,
[int(x) for x in position],
int(circle.radius * PPM)
)
b2.circleShape.draw = usim_draw_circle
def convert_coords_world2disp(point, camera_x=0, camera_y=0):
point = [v * PPM for v in point]
point = [point[0] - camera_x, SCREEN_HEIGHT - point[1] - camera_y]
return point
def convert_coords_disp2world(point):
point = [point[0], SCREEN_HEIGHT - point[1]]
point = [int(v / PPM) for v in point]
return point
# =====
# Basically the engine object
# =====
class Engine():
# =====
# Physics parameters
# =====
world = None
# =====
# Rendering parameters
# =====
# Frames and dimensions
screen = None
width = SCREEN_WIDTH
height = SCREEN_HEIGHT
clock = pygame.time.Clock()
# Render destination
render_window = True # Show rendering in a window
render_video = False # Save rendering to video on disk
video_file = None
# Decorations
font = None
# =====
# World objects and state
# =====
num_ticks = 0
bodies = {}
joints = {}
def __init__(
self,
contactListener=None,
render_window=True, render_video=False, video_file=None,
font='arial', font_size=16
):
# Initialize rendering destination params
self.render_window = render_window
self.render_video = render_video
self.video_file = video_file
if self.render_video and video_file is None:
raise ValueError('Engine error: Specified rendering to video, but did not specify file name')
# Initialize world
self.world = b2.world(
gravity = (0, -50),
doSleep = True,
contactListener = contactListener
)
# Create ground
self.add_object(
'ground',
{
'position': (GROUND_START, 0)
},
'poly',
{
'box': (GROUND_WIDTH, 5),
'friction': 0.5
},
(80, 80, 80, 255),
True,
0x01
)
# Create mouse
self.add_object(
'mouse',
{
'position': (0, 0)
},
None,
None
)
# Initialize pygame modules
pygame.init()
pygame.font.init()
# Initialize rendering decorations
self.font = pygame.font.SysFont(font, font_size)
def reset(self):
# Remove joints
for joint_key in self.joints:
joint = self.joints[joint_key]
if joint is not None:
self.world.DestroyJoint(self.joints[joint_key])
self.joints[joint_key] = None
# Remove objects
for body_key in self.bodies:
body = self.bodies[body_key]
if body is not None and body_key != 'mouse' and body_key != 'ground':
self.world.DestroyBody(self.bodies[body_key])
self.bodies[body_key] = None
# Reset time
self.num_ticks = 0
# Clean up internal data structures
self.cleanup()
def cleanup(self):
joint_keys = list(self.joints.keys())
for key in joint_keys:
if self.joints[key] is None:
self.joints.pop(key)
body_keys = list(self.bodies.keys())
for key in body_keys:
if self.bodies[key] is None:
self.bodies.pop(key)
def add_object(self, name, obj_args, shape_type, shape_args, color=(50, 50, 50, 100), fixed=False, category=0x00):
# And objects to the world
if fixed:
obj = self.world.CreateStaticBody(**obj_args)
else:
obj = self.world.CreateDynamicBody(**obj_args)
if shape_type == 'poly':
obj.CreatePolygonFixture(**shape_args)
elif shape_type == 'circle':
obj.CreateCircleFixture(**shape_args)
obj.color = color
obj.userData = {
'name': name
}
self.bodies[name] = obj
if len(self.bodies[name].fixtures) > 0:
self.bodies[name].fixtures[0].filterData.categoryBits = category
def add_joint(self, name, joint_type, obj1_name, obj2_name, joint_args, anchor_offset=None):
joint = None
if anchor_offset is None:
anchor_offset = 0
anchor = self.bodies[obj1_name].worldCenter + anchor_offset
if joint_type == 'revolute':
joint = self.world.CreateRevoluteJoint(
bodyA = self.bodies[obj1_name],
bodyB = self.bodies[obj2_name],
anchor = anchor,
**joint_args
)
elif joint_type == 'prismatic':
joint = self.world.CreatePrismaticJoint(
bodyA = self.bodies[obj1_name],
bodyB = self.bodies[obj2_name],
anchor = anchor,
**joint_args
)
elif joint_type == 'weld':
joint = self.world.CreateWeldJoint(
bodyA = self.bodies[obj1_name],
bodyB = self.bodies[obj2_name],
anchor = anchor
)
joint.frequencyHz = 0
joint.dampingRatio = 1
elif joint_type == 'rope':
r_joint = Box2D.b2RopeJointDef(
bodyA = self.bodies[obj1_name],
bodyB = self.bodies[obj2_name],
**joint_args
)
joint = self.world.CreateJoint(r_joint)
if joint is not None:
self.joints[name] = joint
else:
raise ValueError('Unsupported joint type ' + joint_type + ' was specified')
def get_state(self):
state = {
'ticks': self.num_ticks,
'bodies': {},
'joints': {}
}
for body in self.bodies:
state['bodies'][body] = {}
state['bodies'][body]['position'] = list(self.bodies[body].position)
state['bodies'][body]['angle'] = self.bodies[body].angle
state['bodies'][body]['lin_vel'] = self.bodies[body].linearVelocity
state['bodies'][body]['ang_vel'] = self.bodies[body].angularVelocity
state['bodies'][body]['custom_data'] = self.bodies[body].userData
for joint in self.joints:
state['joints'][joint] = {}
if hasattr(self.joints[joint], 'motorSpeed'):
state['joints'][joint]['force'] = self.joints[joint].motorSpeed
else:
state['joints'][joint]['force'] = 0
return state
def get_camera_position(self, obj_to_track='', follow_x=True, follow_y=True):
# Get camera offsets
camera_x = 0
camera_y = 0
if obj_to_track in self.bodies and (follow_x or follow_y):
position = list(self.bodies[obj_to_track].position)
camera_x, camera_y = convert_coords_world2disp(position)
camera_x -= 0.5 * SCREEN_WIDTH
camera_y -= 0.5 * SCREEN_HEIGHT
if not follow_x:
camera_x = 0
if not follow_y:
camera_y = 0
return camera_x, camera_y
def loop(self, key_events=[], controls=[]):
while True:
self.loop_once(key_events, controls)
def loop_once(self, key_events=[], controls=[]):
self.handle_controls(key_events, controls)
self.render()
def handle_controls(self, key_events=[], controls=[], custom_dat=None):
# Capture events
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
sys.exit()
#elif (event.type == pygame.KEYDOWN and event.key == pygame.K_x):
# for joint_key in self.joints:
# world.DestroyJoint(self.joints[joint_key])
# self.joints[joint_key] = None
for key_event in key_events:
if event.type == key_event['type']:
if key_event['key'] is None or event.key == key_event['key']:
key_event['fn'](
self,
self.world,
self.bodies,
key_event['body_names'],
self.joints,
key_event['joint_names'],
custom_dat
)
# Handle custom controls
for control in controls:
control['fn'](
self,
self.world,
self.bodies,
control['body_names'],
self.joints,
control['joint_names'],
custom_dat
)
# Cleanup if necessary
self.cleanup()
# Update mouse
mouse_pos = list(pygame.mouse.get_pos())
mouse_pos = convert_coords_disp2world(mouse_pos)
self.bodies['mouse'].position = mouse_pos
# Update bodies attached to mouse
if 'joint_mouse' in self.joints:
self.joints['joint_mouse'].bodyA.position = mouse_pos
def step(self):
# Advance timestep
self.num_ticks += 1
self.world.Step(TIME_STEP, 10, 10)
self.clock.tick(TARGET_FPS)
def render(self, obj_to_track='', follow_x=True, follow_y=True, custom_render=[]):
if not self.render_window and not self.render_video:
raise AttributeError(
'Attempted to render, but user set Engine render_window and render_video flags to False'
)
# Define screen if it does not exist yet
if self.screen is None:
size = self.width, self.height = SCREEN_WIDTH, SCREEN_HEIGHT
if self.render_window:
# Create display
self.screen = pygame.display.set_mode(size)
pygame.display.set_caption('usim2.0')
else:
# Create surface and do NOT display it
self.screen = pygame.Surface(size)
# Clear canvas
#self.screen.fill((0, 180, 255))
self.screen.fill((60, 120, 216))
camera_x, camera_y = self.get_camera_position(obj_to_track, follow_x, follow_y)
# Render objects
for body_key in self.bodies:
body = self.bodies[body_key]
if body.fixtures is not None:
for fixture in body.fixtures:
fixture.shape.draw(
body, fixture, self.screen, camera_x, camera_y
)
# Render joints
for joint_key in self.joints:
joint = self.joints[joint_key]
if joint is not None:
point1 = list(joint.anchorA)
point2 = list(joint.anchorB)
vert1 = convert_coords_world2disp(point1, camera_x, camera_y)
vert2 = convert_coords_world2disp(point2, camera_x, camera_y)
pygame.draw.line(self.screen, (200, 100, 80, 100), vert1, vert2, 2)
# Custom rendering
for custom in custom_render:
custom['fn'](
self,
**custom['args']
)
# Swap buffers (draw new frame)
if self.render_window:
pygame.display.flip()
def quit(self):
pygame.quit()
if __name__ == '__main__':
eng = Engine()
eng.loop()