Creating particle effects

Ranging from dust kicked up by an out of control race car spinning out into a run-off area over smoke that ascends from a battlefield to sparks spraying from a magic wand, particles are a great tool for adding life and visual fidelity to the graphics of a game. Therefore, this recipe will show you how to create a simple particle effect.

Getting ready

This recipe is based upon the project setup presented in Setting up the game structure. Please follow this recipe, found in Chapter 1, before proceeding.

How to do it...

Let's try the following Panda3D's particle effect system:

  1. Open the file Application.py and add the following code:
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import *
    from direct.particles.Particles import Particles
    from direct.particles.ParticleEffect import ParticleEffect
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    self.enableParticles()
    particles = Particles()
    particles.setPoolSize(1000)
    particles.setBirthRate(0.1)
    particles.setLitterSize(10)
    particles.setLitterSpread(3)
    particles.setFactory("PointParticleFactory")
    particles.setRenderer("GeomParticleRenderer")
    particles.setEmitter("SphereVolumeEmitter")
    smiley = loader.loadModel("smiley")
    smiley.setScale(0.1)
    particles.getRenderer().setGeomNode(smiley.node())
    particles.enable()
    self.effect = ParticleEffect("peffect", particles)
    self.effect.reparentTo(render)
    self.effect.enable()
    self.cam.setPos(0, -10, 0)
    
  2. Press F6 to launch the program and see the following output:
How to do it...

How it works...

We placed a simple particle emitter into our sample scene. It spawns new particles, represented by the smiley model, and makes them move to all directions. Let's see what we had to do to create this effect.

In the first line of our Application class' constructor, we enable tracking and updating of particles. After this is done, we can start setting up a particle system.

First, we set the pool size to 1000. This is the maximum amount of particles that are allowed to exist at the same time. Then we set up how many and how often particles are spawned with calls to setBirthRate(), setLitterSize(), and setLitterSpread(), where the last of these methods defines the maximum deviation from the litter size. With our setup, this means the particle system will spawn ten particles each 0.1 seconds with a variation of ±3 particles.

Next, we set up the particle system to use the point factory, want our particles rendered as geoms, and set the particles to be emitted within a spherical volume.

Finally, we load and attach the smiley model to the particle system renderer and add the new ParticleEffect that uses our settings to the scene.

There's more...

As you already may have noticed in the code sample, Panda3D allows you to choose between various factories, renderers, and emitters for your particle systems.

Particle Factories

Panda3D comes with two particle factory types: PointParticleFactory and ZSpinParticleFactory. While the former factory creates particles as points without orientation, the latter is responsible for the creation of particles that spin about their Z-axis.

Particle Renderers

The way particles emitted from a specific system are drawn depends on which particle renderer is set.

The GeomParticleRenderer allows you to assign a GeomNode to the particle system that is used to draw each of the particles, as shown in this recipe's sample code.

If you use the PointParticleRenderer, the LineParticleRenderer or the SparkleParticleRenderer, particles are rendered as points, lines, or little stars, respectively.

This leaves the SpriteParticleRenderer, which allows you to assign a texture image that is used as on-screen representation for your particles.

Particle Emitters

The SphereVolumeEmitter used in this recipe's code defines a sphere-shaped volume within the particles are emitted with a velocity that goes from the center of the sphere to the hull of the volume.

With a BoxVolumeEmitter, particles are spawned inside a box without any velocity.

A DiscVolumeEmitter works similar to the SphereVolumeEmitter, with the difference of using a flat, disc-shaped bounding volume.

PointEmitter, RectangleEmitter, and SphereSurfaceEmitter spawn particles from a single point, within a rectangle and on the outer hull of a sphere, without assigning any velocity to the newly created particles.

The RingEmitter and TangentRingEmitter emitters both create new particles on a ring. Particles spawned by a RingEmitter move on an axis that points from the center of the ring to the outside, whereas the TangentRingEmitter gives particles an initial velocity with a direction that is tangential to the ring that forms the emitter's bounding volume.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset