PSystem.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Das Tank - Copyright (C) 2006, 2007 Windsor Schmidt <windsor@windsorworld.net>
00004 //
00005 // This program is free software; you can redistribute it and/or modify it
00006 // under the terms of the GNU General Public License as published by the Free
00007 // Software Foundation; either version 2 of the License, or (at your option)
00008 // any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful, but WITHOUT
00011 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 // more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along with
00016 // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017 // Place, Suite 330, Boston, MA 02111-1307 USA
00018 //
00020 
00021 
00022 #ifndef STDAFX
00023 #include "stdafx.h"
00024 #endif
00025 
00029 PSystem::PSystem(void)
00030 {
00031        int i;
00032        vPosition.x = 0;
00033        vPosition.y = 0;
00034        vPosition.z = 0;
00035 
00036        vForce.x = 0;
00037        vForce.y = 0;
00038        vForce.z = 0;
00039 
00040        fSpreadAngle = PI/2; // 90 degrees
00041        fFriction = 1;
00042        iNumParticles = 1;
00043 
00044        pParticles = (Particle *) malloc(sizeof(Particle) * iNumParticles);
00045 
00046        for(i=0; i<iNumParticles; i++) // init particles
00047        {
00048               pParticles[i].fPosX = 0;
00049               pParticles[i].fPosY = 0;
00050               pParticles[i].fVelX = 0;
00051               pParticles[i].fVelY = 0;
00052               pParticles[i].fRotVel = 0;
00053               pParticles[i].fSize = .01;
00054               pParticles[i].bAlive = true;
00055               pParticles[i].fAngle = 0;
00056               pParticles[i].fEnergy = 1;
00057        }
00058 
00059        fRandomness = 0;
00060        fDecayRate = 0;
00061        fColorR = 1; // set default color to white
00062        fColorG = 1;
00063        fColorB = 1;
00064 }
00065 
00066 
00070 PSystem::PSystem(Vector vPos, Vector vFce, float fSAngle, float fFric, float fRand, int iParticles)
00071 {
00072        int i;
00073        Vector vTemp;
00074        float angle1, angle2;
00075 
00076        vPosition = vPos;
00077        vForce = vFce;
00078        
00079        fSpreadAngle = fSAngle;
00080        fFriction = fFric;
00081        iNumParticles = iParticles;
00082 
00083        fColorR = 1; // set default color to white
00084        fColorG = 1;
00085        fColorB = 1;
00086 
00087        angle1 = -(fSAngle/2);
00088 
00089        if(iNumParticles > MAX_PARTICLES) { iNumParticles = MAX_PARTICLES; } // limit particles
00090 
00091        pParticles = (Particle *) malloc(sizeof(Particle) * iNumParticles); // reserve memory
00092 
00093        for(i=0; i<iNumParticles; i++) // init particles
00094        {
00095               fRandomness = fRand;
00096               fDecayRate = 0.1;
00097 
00098               vTemp = vForce;
00099               vTemp *= 1 + ((float)(rand()%100) -50) / 100 * fRandomness;
00100               angle2 = angle1 + ((float) i/ (float) iNumParticles) * fSpreadAngle;
00101               vTemp = VRotate2D(angle2 + ((float)(rand()%100) -50) / 100 * fRandomness, vTemp);
00102 
00103               pParticles[i].fPosX = vPos.x;
00104               pParticles[i].fPosY = vPos.y;
00105               pParticles[i].fVelX = vTemp.x;
00106               pParticles[i].fVelY = vTemp.y;
00107               pParticles[i].fSize = .1 + ((float)(rand()%100) - 50) / 100 * 3.5;
00108               pParticles[i].bAlive = true;
00109               pParticles[i].fAngle = 0;
00110               pParticles[i].fEnergy = 2;
00111               pParticles[i].fDecayRate = fDecayRate + (((float)(rand()%100)) / 100) * .1;
00112 
00113               pParticles[i].fRotVel = ((float)(rand()%100) -50) / 100 * 0.1;
00114        }
00115 }
00116 
00117 
00120 PSystem::~PSystem(void)
00121 {
00122        free(pParticles); // free memory reserved for particles
00123 }
00124 
00125 
00131 bool PSystem::Step(float timeStep)
00132 {
00133        int i;
00134        bool someAlive = false; // still some particles with energy
00135 
00136        for(i=0; i<iNumParticles; i++)
00137        {
00138               if(pParticles[i].fEnergy <= 0)
00139               {
00140                      pParticles[i].bAlive = false;
00141                      continue; // done with this particle
00142               }
00143 
00144               pParticles[i].fVelX *= 1 / fFriction;
00145               pParticles[i].fVelY *= 1 / fFriction;
00146 
00147               pParticles[i].fPosX += (pParticles[i].fVelX * timeStep);
00148               pParticles[i].fPosY += (pParticles[i].fVelY * timeStep);
00149 
00150               pParticles[i].fAngle += pParticles[i].fRotVel;
00151 
00152               pParticles[i].fEnergy -= fDecayRate;
00153 
00154               someAlive = true; // at least this particle is still alive
00155        }
00156        
00157        if(someAlive == false) // no particles left alive
00158        {
00159               return true; // expired
00160        }
00161        else
00162        {
00163               return false; // not expired yet
00164        }
00165 }
00166 
00174 void PSystem::Draw(void)
00175 {
00176        int i;
00177        
00178        glBlendFunc (GL_SRC_ALPHA, GL_ONE);
00179        glEnable (GL_BLEND);
00180        glDisable(GL_TEXTURE_2D);
00181 
00182        glPushMatrix();
00183        glTranslatef(0,0,.1);
00184 
00185        for(i=0; i<iNumParticles; i++)
00186        {
00187               glPushMatrix();
00188               glTranslatef(pParticles[i].fPosX, pParticles[i].fPosY,.5);
00189               glRotatef(pParticles[i].fAngle,0,0,1);
00190               glScalef(pParticles[i].fSize, pParticles[i].fSize, 1);
00191               glColor4f(fColorR, fColorG, fColorB, pParticles[i].fEnergy);
00192               glBegin(GL_TRIANGLES);
00193               glVertex2f(0.0, 0.1);
00194               glVertex2f(-0.1, -0.1);
00195               glVertex2f(0.1, -0.1);
00196               glEnd();
00197               glPopMatrix();
00198        }
00199        glPopMatrix();
00200 }
00201 
00202 
00205 void PSystem::SetColor(float r, float g, float b)
00206 {
00207        fColorR = r;
00208        fColorG = g;
00209        fColorB = b;
00210 }

 

Copyright Windsor Schmidt 2006 - All rights reserved.