Skip to main content
gamedev.net
🔒 Locked

Particle system (simple)

Started by middy 0 replies 1.1k views
Original Post
middy
middy
Yes I am sure you have seen this headline before. But the search function do not work for me Anyhow I want to implement simple bullets flying through the air, and I figured particle systems. The toutorials at gamedev seem a little over the edge for my usage. Could you guys point me to a simple toutorial explaining the basics of particle engine and particle. Something very simple.
ade-the-heat
ade-the-heat
I can recommend the chapter on particle systems in "OpenGl Game Programming" - I found it simple.
In case you don''t have the book here''s a shortened explanation.

What is it: a collection of particles, eg snow, bullets, exlosion particles. Each particle acts autonomusly. Each particle has common attributes, eg behaves in a similar way to the others, eg sparks from a fire cracker.
Attributes of particle: position (moves each frame),
velocity (may speed up/slow down), life span (may last for 10 seconds max), size, color, weight, ...

The particle system class when started up will create the number of particles you ask it to each frame, you may want to give a max value - ie never create more than 10,000 partilces in total but make it create a 1000 or so each frame (emission rate) or only have say 5000 alive at any one time its up to you what you do. When create the particle you may introduce some randomness to the attributes - ie not all particles will have the same size, color etc but will be about the same size eg;

eg here is a generic class for a system with an instantiation for snow afterwards (SIMPLIFIED)

===================GENERIC CLASS================
CParticleSystem::CParticleSystem(int maxParticles, vector3_t origin)
{
m_maxParticles = maxParticles; //AS I SAID-MAX NO. IN SYSTEM
m_origin = origin; //WHERE IT WILL BE CENTRED
m_particleList = NULL;
} // end CParticleSystem::Constructor


//Creates the number of new particles specified by
//parameter, using
// the general particle system values with some random element. int CParticleSystem::Emit(int numParticles)
{
// create numParticles new particles (if there''s room)
while (numParticles && (m_numParticles < m_maxParticles))
{
// initialize the current particle and increase the count
InitializeParticle(m_numParticles++);
--numParticles;
}
return numParticles;
} // end CParticleSystem::Emit


// Allocate memory for the maximum number of particles in the system
*****************************************************************************/
void CParticleSystem::InitializeSystem()
{
// if this is just a reset, free the memory
if (m_particleList)
{
delete[] m_particleList;
m_particleList = NULL;
}

// allocate the maximum number of particles
m_particleList = new particle_t[m_maxParticles];

// reset the number of particles and accumulated time
m_numParticles = 0;
m_accumulatedTime = 0.0f;
} // end CParticleSystem::InitializeSystem

========================SNOW INSTANTIATION=============
SNOWSTORM IS A DERIVED CLASS FROM PARTICLESYSTEM

void CSnowstorm::InitializeParticle(int index)
{
AS i SAID - RANDOMISE A BIT

// start the particle at the sky at a random location in the emission zone
m_particleList[index].m_pos.y = m_height;
m_particleList[index].m_pos.x = m_origin.x + FRAND * m_width;
m_particleList[index].m_pos.z = m_origin.z + FRAND * m_depth;

// set the size of the particle
m_particleList[index].m_size = SNOWFLAKE_SIZE;

// give the particle a random velocity
m_particleList[index].m_velocity.x = SNOWFLAKE_VELOCITY.x + FRAND * VELOCITY_VARIATION.x;
m_particleList[index].m_velocity.y = SNOWFLAKE_VELOCITY.y + FRAND * VELOCITY_VARIATION.y;
m_particleList[index].m_velocity.z = SNOWFLAKE_VELOCITY.z + FRAND * VELOCITY_VARIATION.z;
} // end CSnowstorm::InitializeParticle



=====UPDATE THE SYSTEM EACH FRAME==============
void CSnowstorm::Update(float elapsedTime)
{
for (int i = 0; i < m_numParticles; )
{
// update the particle''s position
m_particleList.m_pos = m_particleList[i].m_pos + m_particleList[i].m_velocity * elapsedTime;

// if the particle has hit the ground plane, kill it
if (m_particleList[i].m_pos.y <= m_origin.y)
{
// move the last particle to the current positon, and decrease the count
m_particleList[i] = m_particleList[--m_numParticles];
}
else
{
++i;
}
}

// store the accumulated time
m_accumulatedTime += elapsedTime;

// determine how many new particles are needed
int newParticles = SNOWFLAKES_PER_SEC * m_accumulatedTime;

// save the remaining time for after the new particles are released.
m_accumulatedTime -= 1.0f/(float)SNOWFLAKES_PER_SEC * newParticles;

Emit(newParticles);
}
========================RENDER THE STORM
void CSnowstorm::Render()
{
glPointSize(3.0);
glBegin(GL_POINTS);
for (int i = 0; i < m_numParticles; ++i)
{
partPos = m_particleList[i].m_pos;
size = m_particleList[i].m_size;
glVertex3f(partPos.x, partPos.y, partPos.z);
}
glEnd();
glPopAttrib();

}

Topic Locked

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

Sign in to reply to this topic.