CorEngine RSS
NAVIGATION >>
HOME TUTORIAL API TOOLS FORUM REPORT BUGS
:. Tutorial : Particles

In this tutorial should give you a basic idea how to use the particle system.

Step 1: Getting the tutorial resources


[tutorial7_resources.zip] contains the particle sprite we are going to use in this tutorial.

Step 2: Extracting the tutorials resources


The tutorial resource package contains a folder called resources. Copy and paste that folder to the folder where you have your CorEngine Developmet Kit.

Step 3: Simple particle system


For this tutorials base init.lua, we will start with a simple script that loads a level and runs the engine.

  scene1 = Engine.LoadScene( "level1.pak" )

  while ( Engine.Run() == true ) do
  end

All good so far. Now we will create a simple particle system.

  scene1 = Engine.LoadScene( "level1.pak" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

Try running the engine now. You should see some particles spawning above the ground.

So what happened there? We created a particle system with the name "MyParticles" and with the particle cap of 1000 particles. The second argument of Create.Particles is the maximum amount of particles which the particle system will allow to be created. The bigger the value, the more memory it takes. You can later on change the value with Particles.SetMaxCount if you want.

Next me set the position of the particle system, and finally we add the particle system to the scene. Simple enough, isn't it?

Step 4: Texture, Color, Size


Next we will set a texture for the particles.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

Now you should see some round particles. Lets add some variable color next.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Particles.SetColorMin( particle1, 0.5, 0.5, 0.5, 1.0 )
  Particles.SetColorMax( particle1, 1.0, 1.0, 1.0, 1.0 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

Nice, now we have some colored particles. Lets set a variable size next.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Particles.SetColorMin( particle1, 0.5, 0.5, 0.5, 1.0 )
  Particles.SetColorMax( particle1, 1.0, 1.0, 1.0, 1.0 )
  Particles.SetSize( particle1, 0.25, 1.0 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

You probably noticed that the size doesn't have two functions, one for min and one for max. Instead the size has only one function which sets both, the min and the max of the variable size.

Step 5: Velocity, Gravity, Spawn Delay


Lets say we wan't to make a fountain. First thing would be to set the variable velocity to spread the particles upwards.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Particles.SetColorMin( particle1, 0.5, 0.5, 0.5, 1.0 )
  Particles.SetColorMax( particle1, 1.0, 1.0, 1.0, 1.0 )
  Particles.SetSize( particle1, 0.25, 1.0 )
  Particles.SetVelocityMin( particle1, -3, -3, 1.0 )
  Particles.SetVelocityMax( particle1,  3,  3, 7.0 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

Nice, now they are spreading upwards. Next we add some gravity to them.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Particles.SetColorMin( particle1, 0.5, 0.5, 0.5, 1.0 )
  Particles.SetColorMax( particle1, 1.0, 1.0, 1.0, 1.0 )
  Particles.SetSize( particle1, 0.25, 1.0 )
  Particles.SetVelocityMin( particle1, -3, -3, 1.0 )
  Particles.SetVelocityMax( particle1,  3,  3, 7.0 )
  Particles.SetGravity( particle1, 0, 0, -20.0 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

Assuming that at this point, we wan't more particles, we just set the spawn delay smaller. The amount of particles in existance at once can be counted with life_span/spawn_delay. Lifespan can be set with Particles.SetLifeSpan. Both the life span, spawn delay (and fade speed) are set in seconds.

  scene1 = Engine.LoadScene( "level1.pak" )

  partex = Create.TextureFromFile( "ParticleTex", "resources/particle.png" )

  particle1 = Create.Particles( "MyParticles", 1000 )
  Particles.SetTexture( particle1, partex )
  Particles.SetColorMin( particle1, 0.5, 0.5, 0.5, 1.0 )
  Particles.SetColorMax( particle1, 1.0, 1.0, 1.0, 1.0 )
  Particles.SetSize( particle1, 0.25, 1.0 )
  Particles.SetVelocityMin( particle1, -3, -3, 1.0 )
  Particles.SetVelocityMax( particle1,  3,  3, 7.0 )
  Particles.SetGravity( particle1, 0, 0, -20.0 )
  Particles.SetSpawnDelay( particle1, 0.01 )
  Particles.SetLifeSpan( particle1, 1.5, 2.0 )
  Actor.SetPosition( particle1, 0.0, 0.0, 3.0 )
  Scene.AddParticles( scene1, particle1 )

  while ( Engine.Run() == true ) do
  end

That should give you a REALLY shiny fountain...

In addition to the particle parameters introducet in this tutorial, there are some more of them, like fade speed and variable position. Also you can set a mesh for the particle system and the particles with be spawned according to the vertices of the mesh. Check the API documentation for more information.

Thats it! I hope you enjoyed the tutorial, and if you have any comments about it, please post to the forums.

SourceForge.net
CorEngine.sf.net Except where otherwise noted, content on this site is
licensed under a Creative Commons Attribution 3.0 License.