-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparticles_example.py
More file actions
executable file
·160 lines (118 loc) · 4.08 KB
/
particles_example.py
File metadata and controls
executable file
·160 lines (118 loc) · 4.08 KB
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
# -*- coding: utf-8 -*-
#PARTICLES - MINIMAL EXAMPLE
import pygame as pg
import numpy as np
from math import *
pg.init()
screen = pg.display.set_mode((640, 480), pg.DOUBLEBUF)
screen_rect=screen.get_rect()
##========================================================================#
## PHYSICS
##========================================================================#
#Definition of the forces that can act on particles
def centralforce(pos,ref,expo=2,vort=False):
#Any kind of central attraction or repulsion
dist=pos -ref
if vort:#vortex force (acceleration orthogonal to relative pos)
dist=np.cross(dist,(0,0,1))[:,:2]
hyp=np.hypot(*dist.T).reshape(pos.shape[0],1)
hyp[ hyp==0]=0.01
force=dist*0.001/hyp**(expo+1)
force=np.clip(force,-1.,1.)
return force
def mouseflee(pos,vel):
#Evade the mouse
return centralforce(pos,mousepos,2)
def randforce(pos,vel):
#Thermal noise
return 0.7*(np.random.random(pos.shape)-.5)
def friction(pos,vel):
#Bounds possible velocities
return -.5*vel
##========================================================================#
## DISPLAY
##========================================================================#
class Particle(pg.sprite.Sprite):
mass=0.8
size=(2,2)
def create(self,pos=(0.5,0.5)):
#Initialize a particle at given position
self.pos=np.array(pos,dtype='float')
self.vel=np.array((0.,0.))
self.acc=np.array((0.,0.))
#Create image
surf=pg.surface.Surface(self.size)
surf.fill((255,255,255))
self.image=surf
#Create rect
self.rect=surf.get_rect()
self.rect.center=(self.pos*screen_rect.size).astype('int')
class ParticleCommander(pg.sprite.Group):
#Group of particles+forces+equations of motion
def __init__(self):
pg.sprite.Group.__init__(self)
self.forces=[]
def calc_forces(self):
#Sum of forces on one particle
pos=np.array([part.pos for part in self.sprites()])
vel=np.array([part.vel for part in self.sprites()])
ftot=np.zeros(pos.shape)
for f in self.forces:
ftot+=f(pos,vel)
return ftot
def physics(self,deltat=1.):
#Equations of motion
forces=self.calc_forces()
for i,part in enumerate(self.sprites()):
pos,vel=part.pos,part.vel
part.acc=forces[i]/part.mass
vel+=deltat*part.acc
pos+=deltat*vel
#Reflection on walls
if pos[0]<=0. or pos[0]>=1.:
vel[0]*=-1
if pos[1]<=0. or pos[1]>=1.:
vel[1]*=-1
part.pos=np.clip(pos,0.,1.)
#Update Rect
part.rect.center=(part.pos*screen_rect.size).astype('int')
def init():
#Create particles and forces
partsys=ParticleCommander()
for i in range(2000):
p=Particle()
p.create((0.5,0.5))
partsys.add(p)
#Add forces from particles_forces.py
partsys.forces+=[mouseflee,randforce,friction]
return partsys
#Create event of type 30 every 40 milliseconds
#(30 is an event type unused by pygame. I use it as the signal for the physics simulation)
pg.time.set_timer(30,60) #Physics timer
def main():
global mousepos
particles=init()
alive=True
while alive:
events=pg.event.get()
mousepos=np.array(pg.mouse.get_pos(),dtype="float") /screen_rect.size
for e in events:
if e.type==pg.QUIT:
alive=False
if e.type==pg.KEYDOWN:
if e.unicode:
print "KEY PRESSED:", e.unicode
if e.unicode=='q':
print "Quit"
alive=False
if e.unicode=='r':
print "Reinitialize"
for p in particles.sprites():
p.create((0.5,0.5) )
if e.type==30:
#Update physics
particles.physics(0.06)
screen.fill( (0,20,20) )
particles.draw(screen)
pg.display.flip()
main()