Showing posts with label particle. Show all posts
Showing posts with label particle. Show all posts

Monday, January 29, 2007

Simple Particle Rendering.

Particles are really nice to see. usually we can see particle effects in most computer games . They appear as water fountain , flame, wind,sand, etc. It has infinite scope.  Actully we can create anything using particles.. But it will depends on the speed of computer.

In this page i am trying to create a simple particle engine , which renders a water fountain. So what is a particle ? the first answer coming to my mind is it is a point , but this is not fully correct. It can be represented as a point , or triangle , or polygon etc. This will depends on the application which we are writing. For the creation of water fountain we can assume the following attributes for a particle.

It is represented a single point . (GL_POINT in opengl )
it has 3 dimensions.
if follows gravity rules.  ( if we are using gravity).
it has a specific acceleration in all directions. ( 3 directions , x , y z).

 Above are the basic things to keep in mind. So now we can create a structure like this

struct Particles
{

   double xPos,yPos,zPos; 
   double xAcc; // xAcc is a constant one.
   double yAcc; // this is a diminishing one due to the gravity. 
   double zAcc; // For the timebeing this is zero.
};
and
 #define XACC .05
int        YACC 1.0

now we can create a bunch of particles by declaring an array of Particles.
Particles fountain[MAX_NUMS];

 Now we need to design in which way one particle travels. it can be like this in a X-Y plane ( now no depth)

        o  o
    o         o
  o             o
o               o

so for simulating the movement of a single particle we need to make changes on both x , y .we can keep the x acceleration as constant number ( Particles::xAcc) . If u look the image more closely it can be seen that the acceleration in y direction is more compared to X ,  it is also decreasing due to gravity.

 so we can do it like this, For each particle particle[i].x += XACC ( constant) , and particle[i].y += particle[i].yAcc; so initially this particle[i].yAcc is initialized to 1. after each rendering this will be decreased by the amount of gravity , like  particle[i].yAcc-=gravity; so when the yAcc becomes -ve we will get a falling effect.

 If a particles y value is  -ve and x value is +ve it means that , that particles is falled down to ground. so we need to again reinitialize it position to zero , again sets its acceleration and gravity to the predefined value...

Till now we have drawn only particles in 2d space, ie we haven't changed the z value. we can do that by giving rotating about y axis. The pseudo code is shown below..

For(int i = 0; i < TOT_PARTICLE;i++)
{

       for(int j = 10; j < 360 ; j+=90)    
       {

              glRoate(j degrees in y axis);
              Apply colors or textures.
              DrawParticle(particle[i].x,particle[i].y,particle[i].z);
              ChangeAccleration(particle[i]); // here we are changing the accleration in both y and x direction
       } 
};

Now it is complete I am attaching a sample screen shot which i did with this idea. Hopes this may give some good starting for beginners who want to learn particles.

  

How to create a simple particle Engine ( Computer Graphics)

Particles are really nice to see. usually we can see particle effects in most computer games . They appear as water fountain , flame, wind,sand, etc. It has infinite scope.  Actully we can create anything using particles.. But it will depends on the speed of computer.

In this page i am trying to create a simple particle engine , which renders a water fountain. So what is a particle ? the first answer coming to my mind is it is a point , but this is not fully correct. It can be represented as a point , or triangle , or polygon etc. This will depends on the application which we are writing. For the creation of water fountain we can assume the following attributes for a particle.


It is represented a single point . (GL_POINT in opengl )
it has 3 diamensions.
if follows gravity rules.  ( if we are using gravity).
it has a specific accleration in all directions. ( 3 directions , x , y z).

Above are the basic things to keep in mind. So now we can create a structure like this

struct Particles
{


   double xPos,yPos,zPos; 
   double xAcc; // xAcc is a constant one.
   double yAcc; // this is a diminishing one due to the gravity.
   double zAcc; // For the timebeing this is zero.
};
and


#define XACC .05
int        YACC 1.0
now we can create a bunch of particles by declaring an array of Particles.


Particles fountain[MAX_NUMS];
Now we need to design in which way one particle travels. it can be like this in a X-Y plane ( now no depth)
       o  o

    o         o

  o             o

o               o


so for simulating the movement of a single particle we need to make changes on both x , y .we can keep the x accelration as constant number ( Particles::xAcc) . If u look the image more closely it can be seen that the acceleration in y direction is more compared to X ,  it is also decreasing due to gravity.

so we can do it like this,


For each particle particle[i].x += XACC ( constant) , and particle[i].y += particle[i].yAcc; so initially this particle[i].yAcc is initialized to 1.

After each rendering this will be decreased by the amount of gravity , like  particle[i].yAcc-=gravity; so when the yAcc becomes -ve we will get a falling effect. If a particles y value is  -ve and x value is +ve it means that , that particles is falled down to ground. so we need to again reinitialize it position to zero , again sets its acceleration and gravity to the predefined value...


Till now we have drawn only particles in 2d space, ie we havn't changed the z value. we can do that by giving rotating about y axisl.The pseudo code is shown below..

For(int i = 0; i < TOT_PARTICLE;i++)
{
       for(int j = 10; j < 360 ; j+=90)    
       {
              glRoate(j degrees in y axis);
              Apply colors or textures.
              DrawParticle(particle[i].x,particle[i].y,particle[i].z);
              ChangeAccleration(particle[i]); // here we are changing the accleration in both y and x direction
       } 
};


Now it is complete I am attaching a sample screen shot which i did with this idea. Hopes this may give some good starting for beginners who want to learn particles.