Original Post
I'm trying to make a particle engine. I have a struct for each particle and a class that updates them. Here it is: I initiate like this: CParticleEngine ParticleEngine(1000, CVector3(100,200,100)); and for every pass of the game loop I call: ParticleEngine.Update(); Now for some reason this doesn't work, the life of the particle goes up and down like it's supposed to but the Direction of all the particles always stays (10,10,10) like the constructor and the position starts at 0? and just increases form there?!? Very strange. Were did I mess up? Thanks.
struct SParticle{
SParticle();
void InitParticle(CVector3 p);
bool Active;
float Life;
float Fade;
float Red;
float Green;
float Blue;
CVector3 Position;
CVector3 Direction;
CVector3 Gravity;
};
class CParticleEngine{
private:
vector<SParticle> m_vParticles;
int m_iNumberOfParticles;
float m_fSlowDown;
CVector3 m_vPosition;
void InitOneParticle();
void InitAllParticles();
public:
CParticleEngine(int n, CVector3 p);
~CParticleEngine();
void Update();
void Render();
CVector3 Vector(){return m_vParticles[10].Position;}
float Number(){return m_vParticles[0].Life;}
};
//////FUNCTIONS//////////////
SParticle :: SParticle()
{
Active = true;
Life = 1.0;
Fade = 0;
Red = 1;
Green = 1;
Blue = 1;
Position = CVector3(0,0,0);
Direction = CVector3(10,10,10);
Gravity = CVector3(0,0,0);
}
void SParticle :: InitParticle(CVector3 p)
{
Active = true;
Life = 1.0;
Fade = 0.05;
Red = (rand()%1000)/1000.0f;
Green = (rand()%600)/1000.0f;
Blue = (rand()%200)/1000.0f;
CVector3 Position = p;
CVector3 Direction = CVector3(float((rand()%50)-26.0f)*10.0f,
float((rand()%50)-26.0f)*10.0f,
float((rand()%50)-26.0f)*10.0f);
CVector3 Gravity = CVector3(0,-2,0);
}
CParticleEngine :: CParticleEngine(int n, CVector3 p)
{
m_vPosition = p;
m_fSlowDown = 2.0;
for(int i=0; i<n; ++i)
{
m_vParticles.push_back(SParticle());
}
InitAllParticles();
}
CParticleEngine :: ~CParticleEngine()
{
}
void CParticleEngine :: InitAllParticles()
{
for(int i=0; i<m_vParticles.size(); i++)
{
m_vParticles.InitParticle(m_vPosition);
}
}
void CParticleEngine :: Update()
{
glBindTexture(GL_TEXTURE_2D,t_Particles[0]);
glEnable(GL_BLEND);
for (int loop=0;loop<m_vParticles.size();loop++) // Loop Through All The Particles
{
float x = m_vParticles[loop].Position.x;
float y = m_vParticles[loop].Position.y;
float z = m_vParticles[loop].Position.z;
// Draw The Particle Using Our RGB Values, Fade The Particle Based On It's Life
glColor4f(m_vParticles[loop].Red,
m_vParticles[loop].Green,
m_vParticles[loop].Blue,
m_vParticles[loop].Life);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(1,1); glVertex3f(x+10.5f,y+10.5f,z); // Top Right
glTexCoord2d(0,1); glVertex3f(x-10.5f,y+10.5f,z); // Top Left
glTexCoord2d(1,0); glVertex3f(x+10.5f,y-10.5f,z); // Bottom Right
glTexCoord2d(0,0); glVertex3f(x-10.5f,y-10.5f,z); // Bottom Left
glEnd();
// Done Building Triangle Strip
m_vParticles[loop].Position.x += 10*m_vParticles[loop].Direction.x/
(m_fSlowDown*1000);
// Move On The X Axis By X Speed
m_vParticles[loop].Position.y += 10*m_vParticles[loop].Direction.y/
(m_fSlowDown*1000);
// Move On The Y Axis By Y Speed
m_vParticles[loop].Position.z += 10*m_vParticles[loop].Direction.z/
(m_fSlowDown*1000);
// Move On The Z Axis By Z Speed
m_vParticles[loop].Direction.x += m_vParticles[loop].Gravity.x; // Take Pull On X Axis Into Account
m_vParticles[loop].Direction.y += m_vParticles[loop].Gravity.y; // Take Pull On Y Axis Into Account
m_vParticles[loop].Direction.z += m_vParticles[loop].Gravity.z; // Take Pull On Z Axis Into Account
m_vParticles[loop].Life -= m_vParticles[loop].Fade; // Reduce Particles Life By 'Fade'
if (m_vParticles[loop].Life<=0.0f) // If Particle Is Burned Out
{
m_vParticles[loop].InitParticle(m_vPosition);
}
}//loop
}