-
Notifications
You must be signed in to change notification settings - Fork 187
/
contact_with_friction.py
104 lines (84 loc) · 3.17 KB
/
contact_with_friction.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
"""This example spawns (bouncing) balls randomly on a L-shape constructed of
two segment shapes. Displays collsion strength and rotating balls thanks to
friction. Not interactive.
"""
import math, sys, random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk
from pymunk import Vec2d
import pymunk.pygame_util
def draw_collision(arbiter, space, data):
for c in arbiter.contact_point_set.points:
r = max( 3, abs(c.distance*5) )
r = int(r)
p = pymunk.pygame_util.to_pygame(c.point_a, data["surface"])
pygame.draw.circle(data["surface"], THECOLORS["black"], p, r, 1)
def main():
global contact
global shape_to_remove
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
running = True
### Physics stuff
space = pymunk.Space()
space.gravity = (0.0, -900.0)
draw_options = pymunk.pygame_util.DrawOptions(screen)
# disable the build in debug draw of collision point since we use our own code.
draw_options.flags = draw_options.flags ^ pymunk.pygame_util.DrawOptions.DRAW_COLLISION_POINTS
## Balls
balls = []
### walls
static_lines = [pymunk.Segment(space.static_body, (11.0, 280.0), (407.0, 246.0), 0.0)
,pymunk.Segment(space.static_body, (407.0, 246.0), (407.0, 343.0), 0.0)
]
for l in static_lines:
l.friction = 0.5
space.add(static_lines)
ticks_to_next_ball = 10
ch = space.add_collision_handler(0, 0)
ch.data["surface"] = screen
ch.post_solve = draw_collision
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "contact_with_friction.png")
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 100
mass = 0.1
radius = 25
inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, inertia)
x = random.randint(115,350)
body.position = x, 400
shape = pymunk.Circle(body, radius, (0,0))
shape.friction = 0.5
space.add(body, shape)
balls.append(shape)
### Clear screen
screen.fill(THECOLORS["white"])
### Draw stuff
space.debug_draw(draw_options)
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 200: balls_to_remove.append(ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)
### Update physics
dt = 1.0/60.0
for x in range(1):
space.step(dt)
### Flip screen
pygame.display.flip()
clock.tick(50)
pygame.display.set_caption("fps: " + str(clock.get_fps()))
if __name__ == '__main__':
sys.exit(main())