Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / particle-1.pyOpen in CoCalc
Author: William A. Stein
1# Soya 3D tutorial
2# Copyright (C) 2001-2004 Jean-Baptiste LAMY
3#
4# This program is free software; you can redistribute it and/or modify
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18# This shows how to use soya's particles.
19
20# Imports and inits Soya (see lesson basic-1.py).
21
22import sys, os, os.path, soya
23
24from random import random
25from math import sqrt
26from soya import sdlconst
27from soya import particle
28
29soya.init()
30soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
31
32# Creates the scene.
33
34scene = soya.World()
35
36# Create a particle system
37
38# particles are just like any other object in soya. you must
39# still add them to the scene and they have the same methods available.
40# all particles also support a material for the sprite which you can
41# pass to the constructor or set as a property later.
42# particle systems have nb_particles to specifiy how many particles
43# will be generated, in conjunction with this there is the
44# auto_generate_particle property which determines wether new particles
45# should be created when old ones die or not.
46
47# comment/uncomment the different sections to try the different systems
48
49# here we use the built in FireWork system. auto_generate_particle is
50# on by default so this will continue forever
51fountain = particle.FlagFirework(scene, nb_particles=4, nb_sub_particles=10)
52
53# this is slightly more dull
54# you will notice that this doesnt automatically set auto_generate_particle
55#smoke=particle.Smoke(scene)
56
57# here we use the same smoke particle system but set auto_generate_particle
58# so that it continues
59#smoke=particle.Smoke(scene)
60#smoke.auto_generate_particle=1
61
62# its also possible to create your own particle systems
63# i dont think you can beat looking at the source file model/particle.pyx
64# for understanding how to do this
65class MyParticleSystem(particle.Smoke):
66  def __init__(self,parent):
67    particle.Particles.__init__(self,parent,nb_max_particles=50)
68    self.set_colors((1.0, 1.0, 1.0, 1.0), (1.0, 0.0, 0.0,0.5),(1.0,1.0,0.,0.5),(0.5,0.5,0.5,0.5),(0.,0.,0.,0.5))
69    self.set_sizes ((0.19, 0.19), (0.35, 0.35))
70    self.auto_generate_particle=1
71
72  def generate(self, index):
73    sx = (random()- 0.5) * .2
74    sy = (random())
75    sz = (random() - 0.5) * .2
76    l = (0.2 * (1.0 + random())) / sqrt(sx * sx + sy * sy + sz * sz) * 0.5
77    self.set_particle(index, random()*.5, sx * l, sy * l, sz * l, 0.,0.,0.)
78
79#particles=MyParticleSystem(scene)
80
81# Creates a light.
82
83light = soya.Light(scene)
84light.set_xyz(0.5, 0.0, 2.0)
85
86# Creates a camera.
87
88camera = soya.Camera(scene)
89camera.z = 10.0
90soya.set_root_widget(camera)
91
92# make an idler that stops on any keystroke
93class Idler(soya.Idler):
94  def begin_round(self):
95    soya.Idler.begin_round(self)
96
97    # wait for any keystoke to quit
98    for e in soya.process_event():
99      if e[0]==sdlconst.KEYDOWN and e[1]!=0:
100        self.stop()
101
102Idler(scene).idle()
103
104
105