Thread: Particles

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Particles

    So, my breakout game is half finish. I have collision detection, load and destroy functions, as well as render functions.

    for some cool effect, the particle system will have to get involved. I have particles for the ball when it collides to the walls and the bar. The final thing is the particles for the bricks. There will be 5 or 6 pieces of quad (or triangles) fly out randomly when the ball hit the bricks. But, there are certain problems i'm just unsure about.

    - I start the game by loading the position, textures from a file and create the bricks for the level, and this is my brick class:
    Code:
    class Brick
    {
    public:
    	vect2 Pos;
    	vect2* Vertices;
    
    
    	Brick()
    	{}
    
    	~Brick(){}
    
    
    
    	vect2 *BuildBlock(int width, int height)
    	{
    		Vertices = new vect2[4];
    			Vertices[0] = vect2(0, 0);
    			Vertices[1] = vect2(width, 0);
    			Vertices[2] = vect2(width, height);
    			Vertices[3] = vect2(0, height);
    			return Vertices;
    	}
    	void Render()
    	{
    
    			glPushMatrix();
    			glLoadIdentity();
    			glTranslated(Pos.x, Pos.y, 0.0f);			
    			glColor3f(1, 1, 1);
    			glBegin(GL_QUADS);
    				for(int j=0; j<4; j++)
    				{					
    					glVertex2d(Vertices[j].x, Vertices[j].y);
    				}
    				glEnd();		
    	}
    
    	
    
    	void Destroy()
    	{	
    		Vertices = NULL;
    	}
    };
    Then i create an array of vector and push all the brick objects and empty the load function.

    So when the ball hit one of the brick, the vector will erase the block out the array:
    Code:
    for(int j=0; j<block.size(); j++)
    {
         if(ball is collide with the bricks)
         { 
             block[j].destroy();
             block.erase(block.begin()+j);
         }
    }
    Now, there is the hard part, should i include the particle function in the brick class and then delay the destroy function? Because once the block is erase out the vector array, the particle function will not run. The other way is create an outside particle function and set the particles' position equal the brick's position that had been hit. Then i realize, the particles' position must be going somewhere before it can be reuse at another time when another brick is hit. (make sense?)

    Can anyone point out a better way for this problem? Thanks
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'd separate it into a class of its own. When the brick is destroyed (call the destroy method?) call a CreateParticle function/method with the position of the brick as an argument which creates the particle(s).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    what if 2 blocks are hit at the same time? Should the function being call twice?
    Hello, testing testing. Everthing is running perfectly...for now

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you want twice the amount of particles if two are destroyed then yes! It's up to you.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The problem you will run into is that a particle system must track every particle that is currently active. What if you hit 5 blocks or hit a block before the old particle effect from the previous hit block expires?

    So you can either set a max value for particles or you can use a list and add/remove particles as needed.

    Personally since a particle effect is relatively quick I would fire off a thread for each effect and the thread would be responsible for updating the particle positions. When all of the particles are dead, the thread would terminate and return.

  6. #6
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    How about create the particles classes and push them into array of vector, then attach to each brick. So when the particles are done, delete them from the array
    Hello, testing testing. Everthing is running perfectly...for now

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Another method which lends itself to hardware quite nicely is batching of particles. Process a batch of particles and then process another batch. Send the batches to the hardware for rendering. If done correctly this can eliminate a lot of pipeline stalls.

  8. #8
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by Bubba
    Another method which lends itself to hardware quite nicely is batching of particles. Process a batch of particles and then process another batch. Send the batches to the hardware for rendering. If done correctly this can eliminate a lot of pipeline stalls.
    Can you explain more about this method?
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  2. proper way to render particles (OpenGL)
    By ... in forum Game Programming
    Replies: 2
    Last Post: 03-29-2004, 10:45 PM
  3. DirectX Particles?
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 10-03-2003, 02:46 PM
  4. OpenGL Particles
    By Speedy5 in forum Game Programming
    Replies: 2
    Last Post: 09-30-2003, 01:24 PM
  5. Rights of Particles
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-10-2002, 08:42 PM