Skip to content

Instantly share code, notes, and snippets.

@davepape
Created August 27, 2013 06:03
Show Gist options
  • Save davepape/6350100 to your computer and use it in GitHub Desktop.
Save davepape/6350100 to your computer and use it in GitHub Desktop.
Basic particle system example
# fire.py
# by Dave Pape, for DMS 423
#
# Example particle system for a simple fire-like effect
# This code is intended as a trivial introduction to particle systems and
# graphics programming; it is NOT intended to be optimal; many values are
# hard-coded for simplicity.
#
# Requires: pyglet (http://www.pyglet.org/), euclid (https://code.google.com/p/pyeuclid/),
# and a texture image fireparticle.png
import sys, time, math
from pyglet.gl import *
from euclid import *
from random import *
window = pyglet.window.Window()
gravity = Vector3(0, -2, 0)
tex = pyglet.image.load('fireparticle.png').get_texture()
class particle:
def __init__(self, pos):
self.pos = pos.copy()
self.pos[0] += gauss(0,25)
self.pos[1] += uniform(0,40)
self.vel = Vector3(uniform(-20,20), 300+gauss(0,50), 0)
self.removeTime = time.time() + abs(gauss(0.0,0.25))
def update(self, dt):
self.pos += self.vel * dt
self.vel += gravity * dt
class particleSystem:
def __init__(self, num=1):
self.particles = []
self.addParticles(num)
def addParticles(self, num):
for i in range(0,num):
p = particle(Vector3(300,100,0))
self.particles.append(p)
def draw(self):
glColor3f(1,1,1)
glEnable(tex.target)
glBindTexture(tex.target, tex.id)
glEnable(GL_BLEND)
glBlendFunc(GL_ONE, GL_ONE)
glBegin(GL_QUADS)
for p in self.particles:
size = 20
glTexCoord2f(0,0)
glVertex3f(p.pos[0]-size, p.pos[1]-size, p.pos[2])
glTexCoord2f(1,0)
glVertex3f(p.pos[0]+size, p.pos[1]-size, p.pos[2])
glTexCoord2f(1,1)
glVertex3f(p.pos[0]+size, p.pos[1]+size, p.pos[2])
glTexCoord2f(0,1)
glVertex3f(p.pos[0]-size, p.pos[1]+size, p.pos[2])
glEnd()
glDisable(GL_BLEND)
glDisable(tex.target)
def update(self, dt):
for p in self.particles:
p.update(dt)
t = time.time()
for i in range(len(self.particles)-1, -1, -1):
if (self.particles[i].removeTime <= t) or (self.particles[i].pos[1] < 0):
del self.particles[i]
self.addParticles(1)
systems = [particleSystem(200)]
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
for s in systems:
s.draw()
def update(dt):
for s in systems:
s.update(dt)
for i in range(len(systems)-1, -1, -1):
if len(systems[i].particles) == 0:
del systems[i]
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment