-
Notifications
You must be signed in to change notification settings - Fork 12
/
physics_test.py
70 lines (56 loc) · 1.63 KB
/
physics_test.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
# -*- coding: utf-8 -*-
import sfml as sf
import pymunk # Chipmunk C/C++ # Box2D
import sys
window = sf.RenderWindow(sf.VideoMode(640, 480), "SFML Pymunk")
window.framerate_limit = 60
rad = 14
ball_elasticity = 0.8
friction = 0.8
space = pymunk.Space()
space.gravity = (0.0, -900.0)
circles = []
def create_circle(position):
mass = 1
inertia = pymunk.moment_for_circle(mass, 0, rad)
body = pymunk.Body(mass, inertia)
body.position = position
# body.position = position
shape = pymunk.Circle(body, rad)
shape.elasticity = ball_elasticity
shape.friction = friction
space.add(body, shape)
return shape
def create_line():
body = pymunk.Body()
body.position = (0, 600)
line_shape = pymunk.Segment(body, (-400, -400), (700, -400), 15)
line_shape.elasticity = 0.5
space.add(line_shape)
return line_shape
line = create_line()
while window.is_open:
for event in window.events:
if type(event) is sf.CloseEvent:
window.close()
if type(event) is sf.MouseButtonEvent and event.released:
x, y = event.position
y = 600 - y
pos = pymunk.Vec2d(x, y)
circles.append(create_circle(pos))
window.clear()
for c in circles:
c_draw = sf.CircleShape(c.radius)
x, y = c.body.position
y = 600 - y
c_draw.position = sf.Vector2(x, y)
window.draw(c_draw)
line_draw = sf.RectangleShape((640, 15))
x, y = line.body.position
y = y - 200
line_draw.position = x, y
line_draw.rotation = line.body.angle
window.draw(line_draw)
window.display()
space.step(1/60.0)
sys.exit()