Hi there
I am having a frustrating and strange problem. I am building a particle system. As a result I need particles to reset there variables to be reintroduced into the system.
I have had it working in the past.

I run through the system once, when the particles pass a threshold they should then reset there variables.
The function I have to do this is simply

particlesToRender[i].x = 20;// + rand() % 20;
	particlesToRender[i].y = 1;
	particlesToRender[i].z = -20;// + rand() % -5;
	particlesToRender[i].temperature = 100;
	particlesToRender[i].active = true;

I have experimented and the x,y and z variables all seem to reset fine however the temperature does not?

Is there any reason as to why this wouldn't force the variable to change?

It is just declared as a simple float.

Any advice would be useful thanks

Recommended Answers

All 8 Replies

Is the particlesToRender array (assuming it's an array) being used polymorphically?

I declare the array in a header called Particle. This array contains an elements of structs that contain all the variables (position,temperatures,densities, pressures etc).

I then have a function in particle cpp that resets the variables in the array.

The actual array is called and used in the main function.

Is the problem possibly because I have to pass from the main to the particle .cpp by reference (which I'm not doing). Am I inadvertently having multiple arrays?

I have not set up any templates or done anything special so have no polymorphism that I am aware of.

Hope this helps

Sorry I am unsure as to how to set up items polymorphically so am not aware if I have or not.

If you would like some code snippets to help please let me know The file is pretty large so will only post what is asked as I don't want to span the boards and have you read over lots of large amounts of unnecessary code.

Many thanks

Please write up a test case and post it here. By test case, I mean something similar to that described here.

main.cpp
#include "particle.h"
#include "Grid.h"
#include <iostream>

Particle fireParticle; //particles information
Grid grid; //used to keep track of particles

static bool initilise = true; //initilise particles on first loop

if(initilise)
{

fireParticle.particlesToRender[0].x = 0; //give particles values
fireParticle.particlesToRender[0].y = 1;
fireParticle.particlesToRender[0].z = -20;
fireParticle.particlesToRender[0].temperature = 100;

initilise = false; //set to false to ignore on further loops
}


for(int i = 0; i < 1; i++)
{
if(fireParticle.particlesToRender[0].temperature > 35) //is particle is over temperature updatye and move particles correctly
		{

glPushMatrix();
glTranslatef(particlesToRender[0].x,particlesToRender[0].y,particlesToRender[0].z); //move particles on screen
glDrawParticle(i); //used to draw particles
glPopMatrix();

grid.particleCheck(fireParticle,0);

else //if particle is less then desired temperature rest particle

fireParticle.resetParticle(i);

}
Particle.h
#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>	
#include "Conversion.h"



#ifndef Particle_H
#define Particle_H

class Particle
{
public:
	Particle(){}
	~Particle(){}


	struct ParticleStruct
	{
	public:
		float x,y,z;
		float temperature,wind,density,pressure,timeStep;
		int prevGrid,currentGridSpace;
		bool grid,active;
		
private:
	};


	void initilisePartcile(int i);
	void resetParticle(int index);

	void drawParticle(int particleIndex);
	bool particleInGrid(int currentGrid,int i);
	float particleTemperature(const float Cooling, float ambientTemperature,float currentTemp, int i);
	float calculateParticleVelocity(float ambientDensity,float gravity);
	
	

	

	GLUquadricObj *quadratic;
ParticleStruct particlesToRender[10000];


private:
};

#endif
particle.cpp (reset function)

void Particle::resetParticle(int i)
{
	particlesToRender[i].x = 0 + rand() % 20;
	particlesToRender[i].y = 1;
	particlesToRender[i].z = -20 + rand() % -5;
	particlesToRender[i].temperature = 100;
	std::cout<<"the particle temperature is "<<particlesToRender[i].temperature<<std::endl;
	particlesToRender[i].active = true;
}

Hope this helps and is what you were after.
If i print to the console i get one print out telling me the particle is 100 and another straight after the function telling me the particle temperature is 34.9 and decreasing.

Am i generating several instances of the same array?

Thanks again

Sorry dude, but I'm very busy right now. If you can't post code that compiles with a cut and paste, I won't help because I lack the time to mock everything up on the off chance that your problem with manifest on my end.

Sorry everything in the code tends to rely on everything else so I would have to copy and paste a lot of stuff to get it to the point it is now would you want me to do that?

There would be the main method, a particle class, a conversion class and a grid class.

The code has quite a lot of commented out printing stuff that I could leave in for you to get results etc. But I know you don't want to be going through somebody else's code.

I can post it if you want.

If not if I remove my grid.particleCheck(fireParticle,0); and replce it with just fireParticle.particlesToRender[0].temperature -= 0.1;

It seems to reset fine.

Would this suggest that my grids are stopping the reset from happening correctly?

Are there any circumstances where an array would copy itself to a new section of memory as opposed to directly affect itself?

I have found that if I put an if statement telling it to reset and print out directly after the particle will be the temperature I have specified. As soon as it leaves the if statement it goes back to the temperature it was to begin with?

std::cout<<"the particles temperature in the grid is "<<position.particlesToRender[i].temperature<<std::endl; //this prints out the correct temperature that doesn't update e.g it will say 68,67,66,65,64 and keep triggering the other one to say 500

					if(position.particlesToRender[i].temperature<68)
					{
						position.particlesToRender[i].temperature = 500;
						std::cout<<"the particles temperature in the grid is now "<<position.particlesToRender[i].temperature<<std::endl; //here it will print 500 to the console.
					}

I am pretty sure I have found the problem. In one of my functions I am having to use static floats in order for the calculations to occur correctly. This then causes the array element to be changed back to its previous values.

Is there anyway to get a value to only be static in a given circumstance and then to be able to overwrite this at a later date?

Or will the logic have to be changed for this to happen?

Many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.