# Particles, help controlling direction

This topic is 2894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I am trying to modify this Digiben sample in order to get the effect of particles that generate from a spot (impact point) and float upwards kind of like the sparks of a fire. The sample has the particles rotating in a circle... I have tried removing the cosine/sine functions and replace them with a normal glTranslate with increasing Y value but I just can't get any real results... could anyone please point out roughly where I should add/modify the translation in this code to obtain that result?

 void ParticleMgr::init(){ tex.Load("part.bmp"); GLfloat angle = 0; // A particle's angle GLfloat speed = 0; // A particle's speed // Create all the particles for(int i = 0; i < P_MAX; i++) { speed = float(rand()%50 + 450); // Make a random speed // Init the particle with a random speed InitParticle(particle,speed,angle); angle += 360 / (float)P_MAX; // Increment the angle so when all the particles are // initialized they will be equally positioned in a // circular fashion } } 

 void ParticleMgr::InitParticle(PARTICLE &particle, GLfloat sss, GLfloat aaa) { particle.speed = sss; // Set the particle's speed particle.angle = aaa; // Set the particle's current angle of rotation // Randomly set the particles color particle.red = rand()%255; particle.green = rand()%255; particle.blue = rand()%255; } 

 void ParticleMgr::DrawParticle(const PARTICLE &particle) { tex.Use(); // Calculate the current x any y positions of the particle based on the particle's // current angle -- This will make the particles move in a "circular pattern" GLfloat xPos = sinf(particle.angle); GLfloat yPos = cosf(particle.angle); // Translate to the x and y position and the #defined PDEPTH (particle depth) glTranslatef(xPos,yPos,PDEPTH); // Draw the first quad glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-5, 5, 0); glTexCoord2f(1,0); glVertex3f(5, 5, 0); glTexCoord2f(1,1); glVertex3f(5, -5, 0); glTexCoord2f(0,1); glVertex3f(-5, -5, 0); glEnd(); // Done drawing quad // Draw the SECOND part of our particle tex.Use(); glRotatef(particle.angle,0,0,1); // Rotate around the z-axis (depth axis) //glTranslatef(0, particle.angle, 0); // Draw the second quad glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-4, 4, 0); glTexCoord2f(1,0); glVertex3f(4, 4, 0); glTexCoord2f(1,1); glVertex3f(4, -4, 0); glTexCoord2f(0,1); glVertex3f(-4, -4, 0); glEnd(); // Done drawing quad // Translate back to where we began glTranslatef(-xPos,-yPos,-PDEPTH); } 

 void ParticleMgr::run(){ for(int i = 0; i < P_MAX; i++) { DrawParticle(particle); // Increment the particle's angle particle.angle += ANGLE_INC; } } 

For now I am adding a glPushMatrix(), glTranslate(x, y, z) in the run() function above, right before the loop, with x,y,z as the position of the enemy for placing them on top of the enemy....is that the best place for that?

Thanks for any input!

##### Share on other sites

Hello,

I am trying to modify this Digiben sample in order to get the effect of particles that generate from a spot (impact point) and float upwards kind of like the sparks of a fire. The sample has the particles rotating in a circle... I have tried removing the cosine/sine functions and replace them with a normal glTranslate with increasing Y value but I just can't get any real results... could anyone please point out roughly where I should add/modify the translation in this code to obtain that result?

 void ParticleMgr::init(){ tex.Load("part.bmp"); GLfloat angle = 0; // A particle's angle GLfloat speed = 0; // A particle's speed // Create all the particles for(int i = 0; i < P_MAX; i++) { speed = float(rand()%50 + 450); // Make a random speed // Init the particle with a random speed InitParticle(particle,speed,angle); angle += 360 / (float)P_MAX; // Increment the angle so when all the particles are // initialized they will be equally positioned in a // circular fashion } } 

 void ParticleMgr::InitParticle(PARTICLE &particle, GLfloat sss, GLfloat aaa) { particle.speed = sss; // Set the particle's speed particle.angle = aaa; // Set the particle's current angle of rotation // Randomly set the particles color particle.red = rand()%255; particle.green = rand()%255; particle.blue = rand()%255; } 

 void ParticleMgr::DrawParticle(const PARTICLE &particle) { tex.Use(); // Calculate the current x any y positions of the particle based on the particle's // current angle -- This will make the particles move in a "circular pattern" GLfloat xPos = sinf(particle.angle); GLfloat yPos = cosf(particle.angle); // Translate to the x and y position and the #defined PDEPTH (particle depth) glTranslatef(xPos,yPos,PDEPTH); // Draw the first quad glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-5, 5, 0); glTexCoord2f(1,0); glVertex3f(5, 5, 0); glTexCoord2f(1,1); glVertex3f(5, -5, 0); glTexCoord2f(0,1); glVertex3f(-5, -5, 0); glEnd(); // Done drawing quad // Draw the SECOND part of our particle tex.Use(); glRotatef(particle.angle,0,0,1); // Rotate around the z-axis (depth axis) //glTranslatef(0, particle.angle, 0); // Draw the second quad glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(-4, 4, 0); glTexCoord2f(1,0); glVertex3f(4, 4, 0); glTexCoord2f(1,1); glVertex3f(4, -4, 0); glTexCoord2f(0,1); glVertex3f(-4, -4, 0); glEnd(); // Done drawing quad // Translate back to where we began glTranslatef(-xPos,-yPos,-PDEPTH); } 

 void ParticleMgr::run(){ for(int i = 0; i < P_MAX; i++) { DrawParticle(particle); // Increment the particle's angle particle.angle += ANGLE_INC; } } 

For now I am adding a glPushMatrix(), glTranslate(x, y, z) in the run() function above, right before the loop, with x,y,z as the position of the enemy for placing them on top of the enemy....is that the best place for that?

Thanks for any input!

You need to push and pop the matrix for each of the particles otherwise glTranslate/glRotate/glScale will concatenate the current transformation with whatever transformation was already there.

##### Share on other sites
To do particle opperations you can use shaders. Try this

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 22
• 11
• 25