-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_01.py
98 lines (74 loc) · 2.99 KB
/
simple_01.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
import Tkinter
import random
import numpy as np
import Box2D # The main library
from Box2D.b2 import * # This maps Box2D.b2Vec2 to vec2 (and so on)
# --- constants ---
# Box2D deals with meters, but we want to display pixels,
# so define a conversion factor:
PPM = 20.0 # pixels per meter
TARGET_FPS = 30
TIME_STEP = 1.0 / TARGET_FPS
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 800
# pretty colors
colors = ['#A50026', '#D73027', '#F46D43', '#FDAE61', '#FEE090',
'#E0F3F8', '#ABD9E9', '#74ADD1', '#4575B4', '#313695']
# --- tkinter setup ---
master = Tkinter.Tk()
canvas = Tkinter.Canvas(master, width=800, height=800, bd=-2)
canvas.pack(fill=Tkinter.BOTH, expand=1,
ipadx=0, ipady=0, padx=0, pady=0)
canvas.create_rectangle(0, 0, 800, 800, fill=colors[6], tags=('background'))
def _create_circle(self, x, y, r, **kwargs):
return self.create_oval(x - r, y - r, x + r, y + r, **kwargs)
Tkinter.Canvas.create_circle = _create_circle
# --- pybox2d world setup ---
# Create the world
world = world(gravity=(0, -10), doSleep=True)
# And a static body to hold the ground shape
ground_body = world.CreateStaticBody(
position=(0, 1),
shapes=polygonShape(box=(50, 5)),
)
# Create a dynamic body
dynamic_body = world.CreateDynamicBody(position=(10, 15), angle=15)
# And add a box fixture onto it (with a nonzero density, so it will move)
box = dynamic_body.CreatePolygonFixture(box=(2, 1), density=1, friction=0.3)
body = world.CreateDynamicBody(position=(20, 15))
circle = body.CreateCircleFixture(radius=0.5, density=1, friction=0.3)
cpos = circle.shape.pos * PPM
body.userData = canvas.create_circle(
cpos[0], cpos[1], circle.shape.radius * PPM, fill="blue", outline="#DDD", width=4)
items = []
for body in (dynamic_body, ground_body): # or: world.bodies
for fixture in body.fixtures:
shape = fixture.shape
print shape
vertices = [(body.transform * v) * PPM for v in shape.vertices]
vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]
body.userData = canvas.create_polygon(vertices, outline='white',
fill=colors[2], width=2)
def update():
for body in (world.bodies):
for fixture in body.fixtures:
fixture.shape.draw(body, fixture)
world.Step(TIME_STEP, 10, 10)
master.after(30, update)
def update_circle(circle, body, fixture):
position = body.transform * circle.pos * PPM
x, y = position[0], SCREEN_HEIGHT - position[1]
canvas_item = body.userData
r = circle.radius * PPM
canvas.coords(canvas_item, x - r, y - r, x + r, y + r)
def update_polygon(polygon, body, fixture):
vertices = [(body.transform * v) * PPM for v in polygon.vertices]
vertices = [[v[0], SCREEN_HEIGHT - v[1]]
for v in vertices]
vertices = sum(vertices, [])
canvas_item = body.userData
canvas.coords(canvas_item, *vertices)
circleShape.draw = update_circle
polygonShape.draw = update_polygon
master.after(30, update)
master.after(30, draw)
Tkinter.mainloop()