Tutorial 2 - Particle Initialization Function

Top  Previous  Next

In this tutorial we will look at the Particle Initialization Function, which specifies what a particle's initial properties (position, velocity, color, etc.) should be.

 

Like in tutorial 1, we will be working with the Default Textured Quad Particle System Template again, only this time the template file and class have been renamed to MyParticleSystem.  You should always rename the the template's file and class when importing it into your project to avoid name conflicts when importing template flies again later to create more particle systems, as is mentioned in the templates section.

 

Load the Tutorial 2 project and open up MyParticleSystem.cs.

 

 

Specifying a particle's initial properties using the InitialProperties object

 

The Default templates inherit from the Default Particle System classes.  These Default Particle System classes have a corresponding Default Particle class which specifies the properties of the particles, such as position, velocity, size, color, etc.  The Default Particle System classes also provide an InitialProperties object in which you can specify the Min and Max values that the particle properties should be initialized with.

 

Locate the LoadParticleSystem() function.  The first line of code in this function you will see is:

 

       ParticleInitializationFunction = InitializeParticleUsingInitialProperties;

 

Every DPSF Particle System has a ParticleInitializationFunction property.  This property is basically a function pointer that points to the function that should be used to initialize a new particle's properties when it is added to the particle system simulation.

 

The Default Particle System classes provide the function InitializeParticleUsingInitialProperties(), which will set the new particle's properties according to the values specified in the InitialProperties object.  The next set of code in the LoadParticleSystem() function specifies the InitialProperties object's values:

 

    InitialProperties.LifetimeMin = 1.5f;

      InitialProperties.LifetimeMax = 3.0f;

      InitialProperties.PositionMin = Vector3.Zero;

      InitialProperties.PositionMax = Vector3.Zero;

      InitialProperties.VelocityMin = new Vector3(-50, 50, -50);

      InitialProperties.VelocityMax = new Vector3(50, 100, 50);

      InitialProperties.RotationMin.Z = 0.0f;

      InitialProperties.RotationMax.Z = MathHelper.Pi;

      InitialProperties.RotationalVelocityMin.Z = -MathHelper.Pi;

      InitialProperties.RotationalVelocityMax.Z = MathHelper.Pi;

      InitialProperties.StartSizeMin = 20;

      InitialProperties.StartSizeMax = 40;

      InitialProperties.EndSizeMin = 30;

      InitialProperties.EndSizeMax = 30;

      InitialProperties.StartColorMin = Color.Black;

      InitialProperties.StartColorMax = Color.White;

      InitialProperties.EndColorMin = Color.Black;

      InitialProperties.EndColorMax = Color.White;

 

Play around with changing these values and running the source code to see how it affects the generated particle system.

 

The InitialProperties and Particle objects have several more properties than what is shown here, such as acceleration and external force.  However, setting these properties will not have an effect on the particle system simulation yet, and tutorial 3 will explain why.

 

 

Specifying a particle's initial properties with a new function

 

In addition to using the InitialProperties object, you can also create your own function to initialize a particle's properties. Any particle initialization functions you create must follow the InitializeParticleDelegate prototype, which is:

 

       public void FunctionName(Particle cParticle); where Particle is the particle class that the particle system is using for its particles.

 

An example of this is shown in MyParticleSystem.cs by the InitializeParticleProperties() function.

 

       public void InitializeParticleProperties(DefaultTexturedQuadParticle cParticle)

       {

          // Set the Particle's Lifetime (how long it should exist for)

           cParticle.Lifetime = 2.0f;

 

          // Set the Particle's initial Position to be wherever the Emitter is

           cParticle.Position = Emitter.PositionData.Position;

 

          // Set the Particle's Velocity

          Vector3 sVelocityMin = new Vector3(-100, 25, -100);

          Vector3 sVelocityMax = new Vector3(100, 50, 100);

           cParticle.Velocity = DPSFHelper.RandomVectorBetweenTwoVectors(sVelocityMin, sVelocityMax);

 

          // Adjust the Particle's Velocity direction according to the Emitter's Orientation

           cParticle.Velocity = Vector3.Transform(cParticle.Velocity, Emitter.OrientationData.Orientation);

 

          // Give the Particle a random Size

          // Since we have Size Lerp enabled we must also set the Start and End Size

           cParticle.Size = cParticle.StartSize = cParticle.EndSize = RandomNumber.Next(10, 20);

 

          // Give the Particle a random Color

          // Since we have Color Lerp enabled we must also set the Start and End Color

           cParticle.Color = cParticle.StartColor = cParticle.EndColor = DPSFHelper.RandomColor();

       }

 

You can create several particle initialization functions if you like.  Also, creating your own function to initialize the particle property values can give you more control over the exact values assigned to the particle's properties.  An example of this is shown by the InitializeParticleProperties2() function, where the size of the new particle is based on a variable that increases or decreases each time a new particle is added to the simulation:

 

       // Variables used by the InitializeParticleProperties2() function

       int miCurrentSize = 10;

    bool mbSizeIncreasing = true;

 

       public void InitializeParticleProperties2(DefaultTexturedQuadParticle cParticle)

       {

          // Set the Particle's Lifetime (how long it should exist for)

           cParticle.Lifetime = 1.0f;

 

          // Set the Particle's initial Position to be wherever the Emitter is

           cParticle.Position = Emitter.PositionData.Position;

 

          // Set the Particle's Velocity

           cParticle.Velocity = new Vector3(0, 200, 0);

 

          // Adjust the Particle's Velocity direction according to the Emitter's Orientation

           cParticle.Velocity = Vector3.Transform(cParticle.Velocity, Emitter.OrientationData.Orientation);

 

          // If the Particles should be Increasing in Size

          if (mbSizeIncreasing)

           {

               miCurrentSize++;

           }

          // Else they should be Decreasing in Size

          else

           {

               miCurrentSize--;

           }

 

          // If the Particle Size is too small or too large, negate the size scaling

          if (miCurrentSize <= 10 || miCurrentSize >= 60)

           {

               mbSizeIncreasing = !mbSizeIncreasing;

           }

 

          // Set the Particle's Size to our CurrentSize value

          // Since we have Size Lerp enabled we must also set the Start and End Size

           cParticle.Size = cParticle.StartSize = cParticle.EndSize = miCurrentSize;

 

          // Give the Particle a random Color

          // Since we have Color Lerp enabled we must also set the Start and End Color

           cParticle.Color = cParticle.StartColor = cParticle.EndColor = DPSFHelper.RandomColor();

       }

 

You can even switch particle initialization functions at run-time.  Tutorial 2 shows this by allowing you to switch between the 3 initialization functions shown here by pressing the X, C, and V keys, resulting in different effects for each one.

 

 

Remarks

 

Feel free to play around with the particle initialization functions or to create your own new ones.