Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tuxx

Me particle engine, and me code

This topic is 6112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm making a particle engine. It's an OOP extravaganza (at least for me). I like the code, but it's kind of weird. There's an emitter class, which you initialize the x,y,z,gravity(x),and gravity(y). Then, you have a particle class (two seperate classes). It's the emitter's friend, so it can accesess the emitters x and y positions. It's just I don't have the friend stuff down pat yet, but there are a few empty functions, so just ignore those. Please help me, cause I'm a little new to this OOP stuff and friends.
    
//           CEmitter: emitter class   

// 

// constructer to initialize when you create it

// drawEmitter() to draw the particle

// gravity is the force acting on a particles to move a certain way

//

class CEmitter				// the emitter class

{
public:
	// the overloaded constructor to initialize the particle

	CEmitter(GLfloat ix,GLfloat iy, GLfloat iz, GLfloat igx, GLfloat igy)
	{
		this->x  = ix;		// initialize x (position)

		this->y  = iy;		// initialize y (position)

		this->z  = iz;		// initialize z (position)

		this->gX = igx;		// initialize x (gravity)

		this->gY = igy;		// initialize y (gravity)

	}

	void drawEmitter()										// function to draw particles 

	{
		glLoadIdentity();									// Reset The View

		glTranslatef(this->x,this->y,this->z);				// translate the camera

	
		glBindTexture(GL_TEXTURE_2D, texture[0]);			// bind the texture

	
		glBegin(GL_QUADS);									// begin drawing particle

		/* Note that the z for the vertex is -100.0f; this is because
		/* the particle is too big at 0.0f, so in order for it to work
		/* without having to subtract 100.0f from the initialization
		/* (witch can be a pain), it's integrated into the function. */
			glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  -100.0f);	// bottom left

			glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  -100.0f);	// bottom right

			glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  -100.0f);	// top right

			glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  -100.0f);	// top left

		glEnd();						
	}

friend class CParticle;		// create friendship with particle to let it access private variables


private:
	GLfloat x;				// x position

	GLfloat y;				// y position

	GLfloat z;				// z position

	GLfloat gX;				// x gravity

	GLfloat gY;				// y gravity

};

class CParticle
{
public:
	// constructer to initialize particle

	CParticle()
	{
		this->pX = CEmitter.x;          // initialize x

		this->pY = CEmitter.y;		// initialize y

		this->pZ = CEmitter.z;		// initialize z

	}

	void processParticle()			// process the particle's position

	{
		// translate the particle by it's velocity

		if (pvX > 0) {
			this->pX += pvX; }		// translate x

		else {
			this->pX -= pvX; }
		
		if (pvY > 0) {
			this->pY += pvY; }		// translate y

		else {
			this->pY -= pvY; }
			if (pvZ > 0) {
			this->pZ += pvZ; }		// translate z

		else {
			this->pZ -= pvZ; }

		// update the gravity

		if (gX < 0) {
			this->pvX += CEmitter.gX + (rand()% 100 *.10); }	// translate x velocity, add random-ness

		else {
			this->pvX -= CEmitter.gX - (rand()% 100 *.10); }	// translate x velocity, add random-ness


		if (gY < 0) {
			this->pvY += CEmitter.gY + (rand()% 100 *.10); }	// translate y velocity, add random-ness

		else {
			this->pvY -= CEmitter.gY - (rand()% 100 *.10); }	// translate y velocity, add random-ness

	}
private:
	CEmitter parent;				// parent

	GLfloat pX;						// x (position)

	GLfloat pY;						// y (position)

	GLfloat pZ;						// z (position)

	GLfloat pvX;					// x (velocity)

	GLfloat pvY;					// y (velocity)

	GLfloat pvZ;					// z (velocity)

};
    
Please help me, especially with friends. Whenever I compile it, these errors come up on the lines with CEmitter.x and CEmitter.gx and those: 'CEmitter': Illegal use of this type as an expression, and: See declaration of 'CEmitter.' The error is really weird. Thanks in advance for your help, and thanks for decyphering my code. '"I just want to warn you that when I wrote this code I was listening to The Cure a lot." - Robby Hart, or Billy Madison, or Adam Sandler, one of those guys' - Crazy George next door Edited by - tuxx on December 27, 2001 9:59:50 PM

Share this post


Link to post
Share on other sites
Advertisement
Using friends is naughty. Just make those members in CEmitter public.

// constructer to initialize particle
CParticle()
{
this->pX = CEmitter.x; // initialize x
this->pY = CEmitter.y; // initialize y
this->pZ = CEmitter.z; // initialize z
}

Change CEmitter in CParticle() and drawEmitter() to parent. That is the name of your CEmitter member variable.

Share this post


Link to post
Share on other sites
Lightbulb! From reading some of Jesse Liberty's C++ in __ _______ books, if his sample programs have a private variable, he just about always has something like GetX() { return x; }, thank you for telling me not to use friends. It's just I am reading the OOP section on C++ The Complete Reference, and I didn't know that much about friends before, I didn't want to inherit CParticle from CEmitter and change private to protected, or make CParticle a subclass. Friends just looked like a good idea to me, because I have never really used them in any of my programs. I just want to warn you that when I was writing this code I was listening to the cure a lot. Hehe, that's just my stupid-quote-to-be.
    
// CEmitter: emitter class

//

// constructer to initialize when you create it

// drawEmitter() to draw the particle

// gravity is the force acting on a particles to move a certain way

//

class CEmitter // the emitter class

{
public:
// the overloaded constructor to initialize the particle

CEmitter(GLfloat ix,GLfloat iy, GLfloat iz, GLfloat igx, GLfloat igy)
{
this->x = ix; // initialize x (position)

this->y = iy; // initialize y (position)

this->z = iz; // initialize z (position)

this->gX = igx; // initialize x (gravity)

this->gY = igy; // initialize y (gravity)

}

void drawEmitter() // function to draw particles

{
glLoadIdentity(); // Reset The View

glTranslatef(this->x,this->y,this->z); // translate the camera


glBindTexture(GL_TEXTURE_2D, texture[0]); // bind the texture


glBegin(GL_QUADS); // begin drawing particle

/* Note that the z for the vertex is -100.0f; this is because
/* the particle is too big at 0.0f, so in order for it to work
/* without having to subtract 100.0f from the initialization
/* (witch can be a pain), it's integrated into the function. */

glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -100.0f); // bottom left

glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -100.0f); // bottom right

glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -100.0f); // top right

glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -100.0f); // top left

glEnd();
}

GLfloat GetX() { return x; } // get the emitter's x position

GLfloat GetY() { return y; } // get the emitter's y position

GLfloat GetZ() { return z; } // get the emitter's z position

GLfloat GetGX() { return gX; } // get the emitter's x gravity

GLfloat GetGY() { return gY; } // get the emitter's y gravity



private:
GLfloat x; // x position

GLfloat y; // y position

GLfloat z; // z position

GLfloat gX; // x gravity

GLfloat gY; // y gravity

};

class CParticle
{
public:
// constructer to initialize particle

CParticle()
{
this->pX = CEmitter.GetX(); // initialize x

this->pY = CEmitter.GetY(); // initialize y

this->pZ = CEmitter.GetZ(); // initialize z

}

void processParticle() // process the particle's position

{
// translate the particle by it's velocity

if (pvX > 0) {
this->pX += pvX; } // translate x

else {
this->pX -= pvX; }

if (pvY > 0) {
this->pY += pvY; } // translate y

else {
this->pY -= pvY; }
if (pvZ > 0) {
this->pZ += pvZ; } // translate z

else {
this->pZ -= pvZ; }

// update the gravity

if (gX < 0) {
this->pvX += CEmitter.GetGX() + (rand()% 100 *.10); } // translate x velocity, add random-ness

else {
this->pvX -= CEmitter.GetGX() - (rand()% 100 *.10); } // translate x velocity, add random-ness


if (gY < 0) {
this->pvY += CEmitter.GetGY() + (rand()% 100 *.10); } // translate y velocity, add random-ness

else {
this->pvY -= CEmitter.GetGY() - (rand()% 100 *.10); } // translate y velocity, add random-ness

}
private:
GLfloat pX; // x (position)

GLfloat pY; // y (position)

GLfloat pZ; // z (position)

GLfloat pvX; // x (velocity)

GLfloat pvY; // y (velocity)

GLfloat pvZ; // z (velocity)

};

Now, when I compile this, I get the same error:

pengine.cpp(87) : error C2275: 'CEmitter' : illegal use of this type as an expression

Is there some way to get this to work, or do I just have to make the variables public? It seems like it would work, but it doesn't. Thanks in advance for your help, because it must be hard getting a point accross to me when I'm constantly suggesting some other crazy code .

Edited by - tuxx on December 28, 2001 11:09:12 AM

Share this post


Link to post
Share on other sites
The error is because you are trying to access the class name and not an instance of the class. Try creating an instance of the CEmitter class inside CParticle like:

CEmitter myEmitter;

Then you can access the items like:

myEmitter.GetX();
myEmitter.GetY();

and so on.

Share this post


Link to post
Share on other sites
WOOHOO! I got it working! Instead of CEmitter.GetX(), in the constructor, I just made a parameter, CEmitter &parent, and also in the processParticle() function. Gotta love them references! Thanks for both of your help!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!