Advertisement

First timer Particle

Started by August 30, 2002 04:07 PM
5 comments, last by Nickp3d 22 years, 5 months ago
Hey guys Iam tring to whip up my first particle engine, I wanna use the advanced particle engine theory but I don''t understand where do begin or how to load the particles can you please anylize this for me?
You miss 100% of the shots you do not take. - Wayne Gretzky
Well, what is the advanced particle engine theory?

Apart from that, there are LOTS of pages on the web detailing particle engines. Check http://freespace.virgin.net/hugo.elias/ - for example
Advertisement
As with all software you should start simple. eg. give each particle a position, velocity, lifetime, texture, ect... Then when you get things working add more complex attributes. As far as "loading" particles. I suppose you could load attributes from a file you create with an editor you created. That is what i do!

We did projectile motion in maths and i just used the equation there to move my particle, and specified the initial velocity etc.. and whatever else was in the equation, then the particle will move naturally during it''s lifetime according to the parameters you specified. I''ll just cut and paste the code if you can be bothered to look through:

the globals which affect all particles:

  //affects all particlesfloat gx	= 0.0f;	                        //gravity x float gy	= -9.8f;			//gravity yfloat gz	= 0.0f;				//gravity zfloat timescale	= 0.06f;			//time speed  


Particle class:

       typedef struct{	float r;	float g;	float b;} COLOR;class Particle{public:	Particle(void);	~Particle(void);	void init(int initialX, int initialY, int initialZ,		//initialize particle				float initialVx, float initialVy, float initialVz, float mass, COLOR particleRGB);	void draw(void);					//draw particle	void update(bool lifeCheck);		//update particle position		int getSize(void) { return size; }				//get particle size 	void setSize(int Size) { size = Size; }			//set particle size	void drawPoints(bool pnt) { points = pnt; }		//particle rendering mode	float getLifetime(void) { return lifetime; }	//get particle lifetime	void setLifetime(float duration) { lifetime = duration; } //set particle lifetime	float getMass(void) { return m; }				//get particle mass	void setMass(float mass) { m = mass; }			//set particle mass	int getx(void) { return x; }					//get x position	int gety(void) { return y; }					//get y position	int getz(void) { return z; }					//get z positionprivate:	int		ix, iy, iz;		//initial position	int		x, y, z;		//current position	float	ivx, ivy, ivz;	//initial velocity	float	vx, vy, vz;		//current velocity	COLOR	color;			//particle colour	float	t;				//lifetime	float	m;				//mass	int		size;			//particle size	bool	points;			//draw points or quads	float	lifetime;		//particle life duration};  



constructor destructor:

  //----------------------------------------------------------------//constructors and destructor//----------------------------------------------------------------Particle::Particle(void){	ix = iy = iz				= 0;		//initial xyz	x = y = z					= 0;		//current xyz	ivx = ivx = ivz				= 0.0f;		//initial velocity xyz	vx	= vy = vz				= 0.0f;		//current velocity xyz	color.r = color.b = color.g = 0;		//shade colour	t							= 0.0f;		//time	m							= 0.0f;		//mass	size						= 5;		//size	points						= false;	//render mode	lifetime					= 15.0f;	//particle life time}Particle::~Particle(void){}  


initialize particle:

  //-------------------------------------------------------------------//initialize particle //-------------------------------------------------------------------void Particle::init(int initialX, int initialY, int initialZ, 					float initialVx, float initialVy, float initialVz, 					float mass, 					COLOR particleRGB){	ix			= initialX;	iy			= initialY;	iz			= initialZ;	ivx			= initialVx;	ivy			= initialVy;	ivz			= initialVz;	m			= mass;	color.r		= particleRGB.r;	color.g		= particleRGB.g;	color.b		= particleRGB.b;	t			= 0.0f;	update(false);}  


update particle position:

  //-----------------------------------------------------------------//update particle position using equations of projectile motion //-----------------------------------------------------------------void Particle::update(bool lifeCheck){	t += timescale;			//increment particle lifetime	//update velocity 	vx = (gx * t) + ivx;	vy = (gy * t) + ivy;	vz = (gz * t) + ivz;	//update position 	x = (int)((0.5 * gx * SQRD(t)) + (ivx * t) + ix);	y = (int)((0.5 * gy * SQRD(t)) + (ivy * t) + iy);	z = (int)((0.5 * gz * SQRD(t)) + (ivz * t) + iz);	//check if particle "died" 	if ((t > lifetime) && (lifeCheck))	{		//restart particle		init(ix, iy, iz, ivx, ivy, ivz, m, color);	}}  


draw particle (in OpenGL)

  //-----------------------------------------------------------------//render particle//-----------------------------------------------------------------void Particle::draw(void){	int xvertex, yvertex, zvertex = 0;	glColor3f(color.r, color.g, color.b);	//render points or quads for particles	if (points)	{		glBegin(GL_POINTS);			glVertex3i(x, y, z);		glEnd();	}	else	{		glBegin(GL_QUADS);			//bottom left			xvertex = x;			yvertex = y;			zvertex = z;			glTexCoord2f(0.0f, 0.0f);			glVertex3i(xvertex, yvertex, zvertex);			//top left 			xvertex = x;			yvertex = y + size;			zvertex = z;			glTexCoord2f(0.0f, 1.0f);			glVertex3i(xvertex, yvertex, zvertex);			//top right			xvertex = x + size;			yvertex = y + size;			zvertex = z;			glTexCoord2f(1.0f, 1.0f);			glVertex3i(xvertex, yvertex, zvertex);			//bottom right			xvertex = x + size;			yvertex = y;			zvertex = z;			glTexCoord2f(1.0f, 0.0f);			glVertex3i( xvertex, yvertex, zvertex);		glEnd();	}}  


So everytime i need to create an explosion for example I just use particle.init() in a loop and specify a random initial velocity, then when drawing the scene call particle.update() to update the position and particle.render() to draw it. It''s fun playing with the parameters too

I was just told that it''s better to use floats to store positions and convert them to ints before rendering them so you don''t loose accuracy in calculations though.. I wrote the particle class yesterday without having seen any other examples or tutorials so it may not be the best or optimal way to do it, but i''d appreciate if someone could give some suggestions as it seems to only manage about 100 000 point particles and another galaxy collider program i saw could do 150 000 on my PC.

You will need...
color
motion vars
alive/dead var
time-to-live var.
You can then work from there....

I guess you could do motion that way. *shrug*
I`d simply use the newtonian? method.

Psedocoded a bit...

cVector position
cVector velocity
cVector accel

velocity += accel

position += velocity

You proably will want a drag vector or two to decrease your accel or whatever in given directions.

thats how i am implementing motion...

hope this helps some.


Bugle4d
~V'lionBugle4d
Wow, thanks so much Kyo and everyone else, you guys are really great.

I have learned alot from you guys.
You miss 100% of the shots you do not take. - Wayne Gretzky
Advertisement
quote:
Original post by Vlion
You will need...
color
motion vars
alive/dead var
time-to-live var.
You can then work from there....



And boy, can you work from there. The particle effects in our current game use a gazillion different fields...

quote:

You proably will want a drag vector or two to decrease your accel or whatever in given directions.



We''ve found that drag+acceleration is not worth the effort. Particles reach terminal velocity rather quickly for realistic drag/accel combinations.

So just having velocity or is enough if the ''perfect'' simulation would include accel and drag. Saves quite a bit of CPU time, cutting out on a vector multiply and a vector add per particle.

Have fun, and make sure you buy Mortal Kombat when it comes out

This topic is closed to new replies.

Advertisement