Skip to main content
gamedev.net
🔒 Locked

Particle problem

Started by Kris2456 7 replies 550+ views
Original Post
Kris2456
Kris2456
hello, recently i tried out NeHe''s tutorial on particle systems. From the one in the example, i decided to make my own, so that i could make particles appear on the screen using only one function. However whn i tried it out, all the particles seemed to do, was sit at 0,0,0 and clump together. I tried a few things, but couldnt get the to work. So, here is my code:
typedef struct
{
bool active;
float life;
float fade;

float r;
float g;
float b;

float x;
float y;
float z;

float xi;
float yi;
float zi;

}particles;


void DrawParticles(/*unsigned int texture*/int MAX_PARTICLES,float r,float g,float b)
{
GLuint	loop;
//Create storage for Maximum number of particles

particles particle[MAX_PARTICLES];

//Bind the particle texture

//glBindTexture(GL_TEXTURE_2D,texture);


//Asign values to particles

for(loop=0;loop<MAX_PARTICLES;loop++)
{
    particle[loop].active=true;
    particle[loop].life=1.0;
    particle[loop].fade=float(rand()%100)/1000.0f+0.003f;
    
    particle[loop].r=r;
    particle[loop].g=g;
    particle[loop].b=b;
    
    particle[loop].xi=((rand()%50)-26.0f)*10.0f;
    particle[loop].yi=((rand()%50)-25.0f)*10.0f;
    particle[loop].zi=((rand()%50)-25.0f)*10.0f;
}
for (loop=0;loop<MAX_PARTICLES;loop++)
	{
	if (particle[loop].active)
		{
		float x=particle[loop].x;
		float y=particle[loop].y;
		float z=particle[loop].z;
		
        glEnable(GL_BLEND);
        //glDisable(GL_DEPTH_TEST);

		glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life);
		
		glBegin(GL_TRIANGLE_STRIP);
		glTexCoord2d(1,1); glVertex3f(x+0.5f,y+0.5f,z); // Top Right

		glTexCoord2d(0,1); glVertex3f(x-0.5f,y+0.5f,z); // Top Left

		glTexCoord2d(1,0); glVertex3f(x+0.5f,y-0.5f,z); // Bottom Right

		glTexCoord2d(0,0); glVertex3f(x-0.5f,y-0.5f,z); // Bottom Left

		glEnd();
        glDisable(GL_BLEND);
        //glEnable(GL_DEPTH_TEST);

        glColor4f(1,1,1,1);
        
		particle[loop].x+=particle[loop].xi;
		particle[loop].y+=particle[loop].yi;
		particle[loop].z+=particle[loop].zi;
		
		particle[loop].life-=particle[loop].fade;
		if (particle[loop].life<0.0f)
		{
			particle[loop].life=1.0f;
			particle[loop].x=0.0f;					// Center On X Axis

			particle[loop].y=0.0f;					// Center On Y Axis

			particle[loop].z=0.0f;
		}
	}
}
}
I think ive just messed up somehwre, problem is i ont know where... ----------- "Here lies a toppled God, His fall was not a small one, We but built his pedastle, A narrow, and a tall one" Frank Herbert (Dune:Messiah)
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Daivuk
Daivuk
The problem here seem to be that you recreate a bunch of particles each frame.

put this line outside the function DrawParticle :
particles particle[MAX_PARTICLES];

And put this in an Init function. You call it only one time, not each frame like you did :

for(loop=0;loop<MAX_PARTICLES;loop++){ particle[loop].active=true; particle[loop].life=1.0; particle[loop].fade=float(rand()%100)/1000.0f+0.003f; particle[loop].r=r; particle[loop].g=g; particle[loop].b=b; particle[loop].xi=((rand()%50)-26.0f)*10.0f; particle[loop].yi=((rand()%50)-25.0f)*10.0f; particle[loop].zi=((rand()%50)-25.0f)*10.0f;}
Kris2456
Kris2456
Thx for that. Now it wroks and the particles move. But i have another problem. Bascily, the paricles look completly spastic, you can make them vaguely look like water fountain or fire, bu the look like they are just flashing. Based on my code, do you know how i could change this?

--------

"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Fahrenheit451
Fahrenheit451
Could be one of two things (or both possibly). The lifetime decrements too quickly and the particle resets itself, or the velocity of the particles is too great and they fire off the screen very quickly. Try tweaking the values a little. Also, spend some time to understand the basic physics involved and it will become clearer as to how this works. Also, at the end of each particle life you need to reset that particular particles values. This is not a call to the init routine again, but something that works on the one individual particle.

Hope that helps! Let me know if it doesn''t.
Kris2456
Kris2456
Thx for that, i figured out how to not mak them go spastic. Basicly i put the code that made it go in a random irection into an Init Function, and made a separate draw function. So now that i have it working, i was wondering how to make it fly on the vector way from your camera (IE. Like a bullet). Im not very good with the old vector maths, and im not sure exactly how to do it.
thx

-------

"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Fahrenheit451
Fahrenheit451
Getting the particles to fire off in a particular direction is fairly simple (acutally, I think it was the first question I asked when I started learning OpenGL :-). To get a direction (vector) you subtract the destination point (x,y,z) from the particle start point (x,y,z) or in your case the camera location. In the screenshot point B is the start point and A is the destination, and the particles fire toward A. The direction vector, or more correctly, the velocity vector is then added to the particle position in the render routine thus causing the particle to move from B to A.



Here is a site I later found that does a good job of explaining some of this, but also gamedev has a ton of tutorials on this topic and particle systems - go to Articles & Resources and scroll down to the Maths & Physics section and the Special Effects section.

For bullets a *lot* of other physics is involved so I recommend searching these forums for more info and perhaps picking up the O'Reilly book Physics for Game Developers.

[edited by - Fahrenheit451 on May 16, 2004 4:12:02 AM]
Kris2456
Kris2456
Thx for those sites, i had a look at them, but they all went over vectors in two lines, then moved on to expain how to add them, and unit vectors (?). Basicly, from what i thought out myself. To get a 3D vector you do (x1-x2)(y1-y2)(z1-z2). Then you add those values on to the objects x,y,z coords to make it move in that direction, right?

Would you have any code that would expain how to acctually code something moving along a vector.

cheers

-------

"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)

[edited by - Kris2456 on May 16, 2004 6:11:50 PM]
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Fahrenheit451
Fahrenheit451
Yep, thats the basic jist of it.

dest.x-origin.x,dest.y-origin.y,dest.z-origin.z = direction vector

or if you are using a vector to start with it''s simply

VECTOR PVelocity = dest - origin;

Then add this vector to the position of each particle in the update loop, and render the particle at the new position. You might need to put in some energy value to get things moving, but that depends upon your implementation

PPosition.x += PVelocity.x * Energy;
PPosition.y += PVelocity.x * Energy;
PPosition.z += PVelocity.x * Energy;

or if you are using a vector class, it''s simply

PPosition += PVelocity * Energy;

It''s all in the NeHe code and better explained in the Particle System tutorials in the Special Effects section. I also suggest you go through the code line by line and then cross reference with the Physics tutorials and the particle system tutorials. It will all come together then.
Kris2456
Kris2456
OK, i managed to get round this the buget way. Basicly ive aded the drawParticles() function before gluLookAt, so it stays in front. The problem is, that when i move, the particles move also IE. once fired they still follow tie fighter. How would i fix this?


PS. Also would anyone know how to make a .DLL file in Dev-C++, i wanted to put my particle system in one...

----------

"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)

[edited by - Kris2456 on May 17, 2004 6:46:52 PM]
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.