Last Update: 01.12.2009. By azarai in pyglet | python
In Animating a Scene with pyglet - first version i said my wind effect did not look natural. Luckily Casey Duncan, the developer of [py-lepton] (http://code.google.com/p/py-lepton/ “link to py-lepton site”), dropped in and gave me the tip to use the drag controller instead of the magnet. Drag is designed for simulating particles moving through fluids such as air or water. Too bad it’s not in the docs and i overlooked it in the demos. Anyways, i switched the particle controller, tweaked it a bit and now my animated scene does look much better. Hurray and a big thank you.
Here is the code of the new version. Comments inside. The images are in the first versions zipfile.
from lepton import Particle, ParticleGroup, default_system
from lepton.renderer import BillboardRenderer
from lepton.texturizer import SpriteTexturizer
from lepton.emitter import StaticEmitter
from lepton.controller import Gravity, Lifetime, Movement, Growth, Drag
from lepton.domain import AABox, Sphere
import pyglet
from pyglet.gl import *
from pyglet import image
width = 1280
height = 720
#create pyglet window
window = pyglet.window.Window(width=width, height=height, visible=False)
window.clear()
#enable alpha blending
glEnable(GL_BLEND)
glShadeModel(GL_SMOOTH)
glBlendFunc(GL_SRC_ALPHA, GL_ONE)
glDisable(GL_DEPTH_TEST)
def get_color(r, g, b, a):
""" converts rgba values of 0 - 255 to the equivalent in 0 - 1"""
return (r / 255.0, g / 255.0, b / 255.0, a / 255.0)
flower_tex = image.load('textures/flower.png').get_texture()
background = image.load('textures/bg.png').get_texture()
## create domain for wind effect
wind_domain = AABox((0, 0, 0), (width, height, 0)) #base wind, whole window
wind_domain_hill = Sphere((85, 100, 0), 300) #left hill, basic upwinds
wind_domain_up = Sphere((300, 225, 0), 150) #top wind before left hill
def create_flower_particle(pos):
flower = Particle(
position=pos,
size=(20, 20, 0),
mass=1.0,
velocity=(0, 0, 0),
color=get_color(255, 192, 213, 255),
)
return flower
# create 3 flower emitters at different positions
flower_emitter = StaticEmitter(
rate=1,
template=create_flower_particle((786, 439)),
deviation=Particle(
position=(20, 20, 0),
velocity=(20, 50, 0),
),
)
flower_emitter2 = StaticEmitter(
rate=1,
template=create_flower_particle((1126, 579)),
deviation=Particle(
position=(20, 20, 0),
velocity=(20, 50, 0),
),
)
flower_emitter3 = StaticEmitter(
rate=0.5,
template=create_flower_particle((961, 582)),
deviation=Particle(
position=(20, 20, 0),
velocity=(20, 50, 0),
mass=0.5,
),
)
# use default_system and set global controllers, so that flowers are emitted at a constant rates
default_system.add_global_controller(
Lifetime(20),
Movement(damping=0.93),
Growth(-0.75),
Gravity((0, -60, 0)),
Drag(0.0, 0.10, fluid_velocity=(-120, 0, 0), domain=wind_domain),
Drag(0.0, 0.10, fluid_velocity=(-100, 80, 0), domain=wind_domain_hill),
Drag(0.0, 0.15, fluid_velocity=(-100, 220, 0), domain=wind_domain_up),
)
flowers = ParticleGroup(
controllers=[
flower_emitter
, flower_emitter2, flower_emitter3
],
renderer=BillboardRenderer(SpriteTexturizer(flower_tex.id)))
@window.event
def on_mouse_press(x, y, button, modifiers):
if(pyglet.window.mouse.LEFT == button):
print x, y
# clear screen and draw particles
@window.event
def on_draw():
window.clear()
background.blit(0, 0)
glLoadIdentity()
default_system.draw()
# setup scheduler for particle updates and start pyglet
if __name__ == '__main__':
window.set_visible(True)
pyglet.clock.schedule_interval(default_system.update, (1.0 / 30.0))
pyglet.clock.set_fps_limit(None)
pyglet.app.run()