loop: values get added together? :S

Started by
3 comments, last by Subotron 21 years ago

	for (int j = 0; j < NUM_PARTICLES; j++)
	{
		if (!g_pParticle[j]->bUpdate(g_vForce))
		{
			g_vVelocity->m_fX = float(rand() % 100) / 1000.0f - 0.05f;
			g_vVelocity->m_fY = float(rand() % 100) / 2000.0f + 0.01f;
			g_vVelocity->m_fZ = 0.0f;

			g_vPosition->m_fX = g_vSystemPos->m_fX;
			g_vPosition->m_fY = g_vSystemPos->m_fY;
			g_vPosition->m_fZ = g_vSystemPos->m_fZ;

			g_fColor[0] = float(rand() % 256) / 256.0f;
			g_fColor[1] = float(rand() % 256) / 256.0f;
			g_fColor[2] = float(rand() % 256) / 256.0f;
			g_fColor[3] = 1.0f;

			g_fFadeSpeed = float (rand() % 100) / 1000000.0f + 0.03f;

			if (!g_pParticle[j]->bInit(g_vPosition, g_vVelocity, g_fColor, g_fSize, g_fFadeSpeed))
				return false;
		}
	}
 
Now, this loop goes trough all the particles I have in a system. If there is one, it works allright. If I add more particles than 1, the values are somehow added together. Color becomes white, and speed really increases. So there must be something wrong. The variables passed with the function are all globals:

CParticle*			g_pParticle[NUM_PARTICLES];
CVector*			g_vForce		= NULL;
CVector*			g_vSystemPos	= NULL;
CVector*			g_vPosition		= NULL;
CVector*			g_vVelocity		= NULL;
float				g_fColor[4];
float				g_fFadeSpeed;
float				g_fSize;
 
The code I posted on top of this post is used whenever a particle dies, to respawn it. almost the same function is used when it''s initialized for the first time. What can be the cause of this? When the particle number is increased there''s still only one particle rendered (or they are on top of eachother) and it has much higher velocity and color values (totally white and incredible fast if I add enough particles) | Panorama 3D Engine | Contact me | | MSDN | Google | SourceForge |
Advertisement
I don't think that the error is in the previous code...
If you are sure that this code is the problem, perhaps it is in the two member functions "bUpdate" and "bInit".

It's really a bad idea to have so many global data. It certainly here that you have a problem. (Perhaps a change where you think work locally.)

Can't help you more with that code... I think the error is elsewhere.

Good Luck.

[edited by - Viquel on April 30, 2003 3:54:14 PM]
About the global data, I know I shouldn''t have that, but all those variables will be replaced when I get to adding systems and managers so it''s just temporary. Well here''s the other code that could have anything to do with it:

This is everything in the main loop:

	for (int i = 0; i < NUM_PARTICLES; i++)	{		g_pParticle->vRender();	}	for (int j = 0; j < NUM_PARTICLES; j++)	{		if (!g_pParticle[j]->bUpdate(g_vForce))		{			g_vVelocity->m_fX = float(rand() % 100) / 1000.0f - 0.05f;			g_vVelocity->m_fY = float(rand() % 100) / 2000.0f + 0.01f;			g_vVelocity->m_fZ = 0.0f;			g_vPosition->m_fX = g_vSystemPos->m_fX;			g_vPosition->m_fY = g_vSystemPos->m_fY;			g_vPosition->m_fZ = g_vSystemPos->m_fZ;			g_fColor[0] = float(rand() % 256) / 256.0f;			g_fColor[1] = float(rand() % 256) / 256.0f;			g_fColor[2] = float(rand() % 256) / 256.0f;			g_fColor[3] = 1.0f;			g_fFadeSpeed = float (rand() % 100) / 1000000.0f + 0.03f;			if (!g_pParticle[j]->bInit(g_vPosition, g_vVelocity, g_fColor, g_fSize, g_fFadeSpeed))				return false;		}	} 


Here are the functions:

void CParticle::	vRender(void){	glColor4f(m_fColor[0], m_fColor[1], m_fColor[2], m_fColor[3]);	glBegin(GL_TRIANGLES);		glTexCoord2i(0, 0); glVertex3f(m_vPosition->m_fX - m_fSize, m_vPosition->m_fY - m_fSize, m_vPosition->m_fZ);		glTexCoord2i(1, 0); glVertex3f(m_vPosition->m_fX + m_fSize, m_vPosition->m_fY - m_fSize, m_vPosition->m_fZ);		glTexCoord2i(1, 1); glVertex3f(m_vPosition->m_fX + m_fSize, m_vPosition->m_fY + m_fSize, m_vPosition->m_fZ);		glTexCoord2i(0, 0); glVertex3f(m_vPosition->m_fX - m_fSize, m_vPosition->m_fY - m_fSize, m_vPosition->m_fZ);		glTexCoord2i(1, 1); glVertex3f(m_vPosition->m_fX + m_fSize, m_vPosition->m_fY + m_fSize, m_vPosition->m_fZ);		glTexCoord2i(0, 1); glVertex3f(m_vPosition->m_fX - m_fSize, m_vPosition->m_fY + m_fSize, m_vPosition->m_fZ);	glEnd();}//--------------------------------------//--------------------------------------bool CParticle::	bUpdate(CVector* a_vForce){	m_vVelocity->m_fX	+= a_vForce->m_fX;	m_vVelocity->m_fY	+= a_vForce->m_fY;	m_vVelocity->m_fZ	+= a_vForce->m_fZ;	m_vOldPosition		=  m_vPosition;	m_vPosition->m_fX	+= m_vVelocity->m_fX;	m_vPosition->m_fY	+= m_vVelocity->m_fY;	m_vPosition->m_fZ	+= m_vVelocity->m_fZ;	m_fLife				-= m_fFadeSpeed;	if (m_fLife <= 0.0f)		return false;	return true;}//--------------------------------------//--------------------------------------bool CParticle::	bInit(CVector* a_vPosition, CVector* a_vVelocity, float a_fColor[4], float a_fSize, float a_fFadeSpeed){	m_vVelocity		= a_vVelocity;	m_vOldPosition	= a_vPosition;	m_vPosition		= a_vPosition;	m_fColor[0]		= a_fColor[0];	m_fColor[1]		= a_fColor[1];	m_fColor[2]		= a_fColor[2];	m_fColor[3]		= a_fColor[3];	m_fLife			= 1.0f;	m_fSize			= a_fSize;	m_fFadeSpeed	= a_fFadeSpeed;	return true;}//--------------------------------------//-------------Constructor--------------//--------------------------------------	CParticle(void)	{		m_vPosition		= NULL;		m_vOldPosition	= NULL;		m_vVelocity		= NULL;	} 


| Panorama 3D Engine | Contact me |
| MSDN | Google | SourceForge |
Your member variables m_vVelocity and m_vPosition are pointers (and some others). This means that every single particle has these members pointing to the same object in memory - you update one, then all of them get updated.
Each particle requires unique instances of these member variables. So instead of:
CVector *m_vVelocity;
have:
CVector m_vVelocity;
and change the code appropiately.
Your colours aren't exhibiting this problem, but are turning white because the same poly is been written to screen multiple times and I'm assuming you've got additive blending enabled.

A couple of other comments about your code:
1) Why is g_pParticle[] an array of pointers to objects? Surely having an array of objects would be a) quicker since there's less deferencing going on and b) take up less memory?
2) Why use global variables to store intermediate data? A lot of that code could go into the Init function. Also, the globals should really go into a particle system class so that you can have multiple particle systems. i.e. a particle system has a set of particles and a position. And so on.
3) The update is not time dependant, it assumes a fixed frame rate.
4) You can save memory per particle by removing m_fColor[3] (it's always 1.0f), m_vOldPosition (it's never used) and m_vPosition (you can work out the position given the initial conditions using s=ut+0.5a.t.t. (you'd need to pass the particle system's origin as an argument though, but would allow you to move all the particles by updating the particle system's position - useful for rocket trails).

Skizz


[edited by - Skizz on May 1, 2003 7:21:20 AM]
Ok it''s fixed now, tnx a lot. About the points that were wrong, I knew a few of them and I''m doing something about them this is just temporary because I''m trying some stuff. But thanks for the help, it''s very usefull

| Panorama 3D Engine | Contact me |
| MSDN | Google | SourceForge |

This topic is closed to new replies.

Advertisement