Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6625 in orxonox.OLD


Ignore:
Timestamp:
Jan 20, 2006, 1:29:52 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: ParticleSystem and Emitter get Deleted correctly

Location:
trunk/src/lib/particles
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/particles/particle_emitter.cc

    r6623 r6625  
    124124  else if (this->system != NULL)
    125125    this->system->removeEmitter(this);
    126   this->system = system;
    127126}
    128127
  • trunk/src/lib/particles/particle_system.cc

    r6623 r6625  
    5151ParticleSystem::~ParticleSystem()
    5252{
    53    // deleting all the living Particles
    54    while (this->particles)
    55      {
    56        Particle* tmpDelPart = this->particles;
    57        this->particles = this->particles->next;
    58        delete tmpDelPart;
    59      }
    60 
    61    // deleting all the dead particles
    62    while (this->deadList)
    63      {
    64        Particle* tmpDelPart = this->deadList;
    65        this->deadList = this->deadList->next;
    66        delete tmpDelPart;
    67      }
    68 
    69      while(!this->emitters.empty())
    70      {
    71        this->removeEmitter(this->emitters.front());
    72      }
     53  // deleting all the living Particles
     54  while (this->particles)
     55  {
     56    Particle* tmpDelPart = this->particles;
     57    this->particles = this->particles->next;
     58    delete tmpDelPart;
     59  }
     60
     61  // deleting all the dead particles
     62  while (this->deadList)
     63  {
     64    Particle* tmpDelPart = this->deadList;
     65    this->deadList = this->deadList->next;
     66    delete tmpDelPart;
     67  }
     68
     69  while(!this->emitters.empty())
     70  {
     71    this->removeEmitter(this->emitters.front());
     72  }
    7373
    7474}
     
    102102
    103103  LoadParam(root, "max-count", this, ParticleSystem, setMaxCount)
    104       .describe("the maximal count of Particles, that can be emitted into this system");
     104  .describe("the maximal count of Particles, that can be emitted into this system");
    105105
    106106  LoadParam(root, "life-span", this, ParticleSystem, setLifeSpan)
    107       .describe("sets the life-span of the Particles.");
     107  .describe("sets the life-span of the Particles.");
    108108
    109109  LoadParam(root, "conserve", this, ParticleSystem, setConserve)
    110       .describe("sets the Conserve factor of the Particles (1.0: they keep all their energy, 0.0:they keep no energy)");
     110  .describe("sets the Conserve factor of the Particles (1.0: they keep all their energy, 0.0:they keep no energy)");
    111111
    112112  LoadParamXML(root, "emitters", this, ParticleSystem, loadEmitters);
     
    115115  {
    116116    element->ToText();
    117   // PER-PARTICLE-ATTRIBUTES:
     117    // PER-PARTICLE-ATTRIBUTES:
    118118    LoadParam_CYCLE(element, "radius", this, ParticleSystem, setRadius)
    119         .describe("The Radius of each particle over time (TimeIndex [0-1], radius at TimeIndex, randomRadius at TimeIndex)");
     119    .describe("The Radius of each particle over time (TimeIndex [0-1], radius at TimeIndex, randomRadius at TimeIndex)");
    120120
    121121    LoadParam_CYCLE(element, "mass", this, ParticleSystem, setMass)
    122         .describe("The Mass of each particle over time (TimeIndex: [0-1], mass at TimeIndex, randomMass at TimeIndex)");
     122    .describe("The Mass of each particle over time (TimeIndex: [0-1], mass at TimeIndex, randomMass at TimeIndex)");
    123123
    124124    LoadParam_CYCLE(element, "color", this, ParticleSystem, setColor)
    125         .describe("The Color of each particle over time (TimeIndex: [0-1], red: [0-1], green: [0-1], blue: [0-1], alpha: [0-1])");
     125    .describe("The Color of each particle over time (TimeIndex: [0-1], red: [0-1], green: [0-1], blue: [0-1], alpha: [0-1])");
    126126  }
    127127  LOAD_PARAM_END_CYCLE(element);
     
    229229  if (emitter->getSystem() != NULL)
    230230    emitter->getSystem()->removeEmitter(emitter);
     231  emitter->system = this;
    231232  this->emitters.push_back(emitter);
    232233}
     
    235236void ParticleSystem::removeEmitter(ParticleEmitter* emitter)
    236237{
    237   std::list<ParticleEmitter*>::iterator it = std::find(this->emitters.begin(), this->emitters.end(), emitter);
     238  assert (emitter != NULL);
     239  emitter->system = NULL;
     240  this->emitters.remove(emitter);
     241  /*  std::list<ParticleEmitter*>::iterator it = std::find(this->emitters.begin(), this->emitters.end(), emitter);
    238242  if (it != this->emitters.end())
    239     this->emitters.erase(it);
     243    this->emitters.erase(it);*/
    240244}
    241245
     
    251255  Particle* prevPart = NULL;
    252256  while (likely(tickPart != NULL))
     257  {
     258    // applying force to the System.
     259    if (likely (tickPart->mass > 0.0))
     260      tickPart->velocity += tickPart->extForce / tickPart->mass * dt;
     261    // rendering new position.
     262    tickPart->position += tickPart->velocity * dt;
     263    tickPart->orientation += tickPart->momentum *dt;
     264
     265    tickPart->radius = radiusAnim.getValue(tickPart->lifeCycle)
     266                       + randRadiusAnim.getValue(tickPart->lifeCycle) * tickPart->radiusRand;
     267
     268    tickPart->mass = massAnim.getValue(tickPart->lifeCycle)
     269                     + randMassAnim.getValue(tickPart->lifeCycle) * tickPart->massRand;
     270
     271    tickPart->extForce = Vector(0,0,0);
     272
     273    // applying Color
     274    tickPart->color[0] = this->colorAnim[0].getValue(tickPart->lifeCycle);
     275    tickPart->color[1] = this->colorAnim[1].getValue(tickPart->lifeCycle);
     276    tickPart->color[2] = this->colorAnim[2].getValue(tickPart->lifeCycle);
     277    tickPart->color[3] = this->colorAnim[3].getValue(tickPart->lifeCycle);
     278
     279    // many more to come
     280
     281    if (this->conserve < 1.0)
    253282    {
    254       // applying force to the System.
    255       if (likely (tickPart->mass > 0.0))
    256         tickPart->velocity += tickPart->extForce / tickPart->mass * dt;
    257       // rendering new position.
    258       tickPart->position += tickPart->velocity * dt;
    259       tickPart->orientation += tickPart->momentum *dt;
    260 
    261       tickPart->radius = radiusAnim.getValue(tickPart->lifeCycle)
    262         + randRadiusAnim.getValue(tickPart->lifeCycle) * tickPart->radiusRand;
    263 
    264       tickPart->mass = massAnim.getValue(tickPart->lifeCycle)
    265         + randMassAnim.getValue(tickPart->lifeCycle) * tickPart->massRand;
    266 
    267       tickPart->extForce = Vector(0,0,0);
    268 
    269       // applying Color
    270       tickPart->color[0] = this->colorAnim[0].getValue(tickPart->lifeCycle);
    271       tickPart->color[1] = this->colorAnim[1].getValue(tickPart->lifeCycle);
    272       tickPart->color[2] = this->colorAnim[2].getValue(tickPart->lifeCycle);
    273       tickPart->color[3] = this->colorAnim[3].getValue(tickPart->lifeCycle);
    274 
    275       // many more to come
    276 
    277       if (this->conserve < 1.0)
     283      tickPart->velocity *= this->conserve;
     284      tickPart->momentum *= this->conserve;
     285    }
     286    // find out if we have to delete tickPart
     287    if (unlikely((tickPart->lifeCycle += dt/tickPart->lifeTime) >= 1.0))
     288    {
     289      // remove the particle from the list
     290      if (likely(prevPart != NULL))
    278291      {
    279         tickPart->velocity *= this->conserve;
    280         tickPart->momentum *= this->conserve;
     292        prevPart->next = tickPart->next;
     293        tickPart->next = this->deadList;
     294        this->deadList = tickPart;
     295        tickPart = prevPart->next;
    281296      }
    282       // find out if we have to delete tickPart
    283       if (unlikely((tickPart->lifeCycle += dt/tickPart->lifeTime) >= 1.0))
    284         {
    285           // remove the particle from the list
    286           if (likely(prevPart != NULL))
    287             {
    288               prevPart->next = tickPart->next;
    289               tickPart->next = this->deadList;
    290               this->deadList = tickPart;
    291               tickPart = prevPart->next;
    292             }
    293           else
    294             {
    295               prevPart = NULL;
    296               this->particles = tickPart->next;
    297               tickPart->next = this->deadList;
    298               this->deadList = tickPart;
    299               tickPart = this->particles;
    300             }
    301           --this->count;
    302         }
    303297      else
    304         {
    305           prevPart = tickPart;
    306           tickPart = tickPart->next;
    307         }
     298      {
     299        prevPart = NULL;
     300        this->particles = tickPart->next;
     301        tickPart->next = this->deadList;
     302        this->deadList = tickPart;
     303        tickPart = this->particles;
     304      }
     305      --this->count;
    308306    }
    309 
    310     std::list<ParticleEmitter*>::iterator emitter;
    311     for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
    312       (*emitter)->tick(dt);
     307    else
     308    {
     309      prevPart = tickPart;
     310      tickPart = tickPart->next;
     311    }
     312  }
     313
     314  std::list<ParticleEmitter*>::iterator emitter;
     315  for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
     316    (*emitter)->tick(dt);
    313317}
    314318
     
    321325  Particle* tickPart = particles;
    322326  while (tickPart)
    323     {
    324       tickPart->extForce += field->calcForce(tickPart->position);
    325       tickPart = tickPart->next;
    326     }
     327  {
     328    tickPart->extForce += field->calcForce(tickPart->position);
     329    tickPart = tickPart->next;
     330  }
    327331}
    328332
     
    427431{
    428432  if (this->count <= this->maxCount)
     433  {
     434    // if it is the first Particle
     435    if (unlikely(particles == NULL))
    429436    {
    430       // if it is the first Particle
    431       if (unlikely(particles == NULL))
    432         {
    433           if (likely(deadList != NULL))
    434             {
    435               this->particles = this->deadList;
    436               deadList = deadList->next;
    437             }
    438           else
    439             {
    440               PRINTF(5)("Generating new Particle\n");
    441               this->particles = new Particle;
    442             }
    443           this->particles->next = NULL;
    444         }
    445       // filling the List from the beginning
     437      if (likely(deadList != NULL))
     438      {
     439        this->particles = this->deadList;
     440        deadList = deadList->next;
     441      }
    446442      else
    447         {
    448           Particle* tmpPart;
    449           if (likely(deadList != NULL))
    450             {
    451               tmpPart = this->deadList;
    452               deadList = deadList->next;
    453             }
    454           else
    455             {
    456               PRINTF(5)("Generating new Particle\n");
    457               tmpPart = new Particle;
    458             }
    459           tmpPart->next = this->particles;
    460           this->particles = tmpPart;
    461         }
    462       particles->lifeTime = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan;
    463       particles->lifeCycle = 0.0;
    464       particles->position = position;
    465       particles->velocity = velocity;
    466 
    467       particles->orientation = orientation;
    468       particles->momentum = momentum;
    469 
    470       //  particle->rotation = ; //! @todo rotation is once again something to be done.
    471       particles->massRand = 2*(float)rand()/RAND_MAX -1;
    472       particles->radiusRand = 2* (float)rand()/RAND_MAX -1;
    473       particles->mass = this->massAnim.getValue(0.0) + this->randMassAnim.getValue(0.0)*particles->massRand;
    474       particles->radius = this->radiusAnim.getValue(0.0) + this->randRadiusAnim.getValue(0.0)*particles->radiusRand;
    475 
    476       ++this->count;
     443      {
     444        PRINTF(5)("Generating new Particle\n");
     445        this->particles = new Particle;
     446      }
     447      this->particles->next = NULL;
    477448    }
     449    // filling the List from the beginning
     450    else
     451    {
     452      Particle* tmpPart;
     453      if (likely(deadList != NULL))
     454      {
     455        tmpPart = this->deadList;
     456        deadList = deadList->next;
     457      }
     458      else
     459      {
     460        PRINTF(5)("Generating new Particle\n");
     461        tmpPart = new Particle;
     462      }
     463      tmpPart->next = this->particles;
     464      this->particles = tmpPart;
     465    }
     466    particles->lifeTime = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan;
     467    particles->lifeCycle = 0.0;
     468    particles->position = position;
     469    particles->velocity = velocity;
     470
     471    particles->orientation = orientation;
     472    particles->momentum = momentum;
     473
     474    //  particle->rotation = ; //! @todo rotation is once again something to be done.
     475    particles->massRand = 2*(float)rand()/RAND_MAX -1;
     476    particles->radiusRand = 2* (float)rand()/RAND_MAX -1;
     477    particles->mass = this->massAnim.getValue(0.0) + this->randMassAnim.getValue(0.0)*particles->massRand;
     478    particles->radius = this->radiusAnim.getValue(0.0) + this->randRadiusAnim.getValue(0.0)*particles->radiusRand;
     479
     480    ++this->count;
     481  }
    478482  else
    479483    PRINTF(5)("maximum count of particles reached not adding any more\n");
     
    487491  PRINT(0)("  ParticleCount: %d, maximumCount: %d :: filled %d%%\n", this->count, this->maxCount, 100*this->count/this->maxCount);
    488492  if (deadList)
    489     {
    490       PRINT(0)("  - ParticleDeadList is used: ");
    491       int i = 1;
    492       Particle* tmpPart = this->deadList;
    493       while (tmpPart = tmpPart->next) ++i;
    494       PRINT(0)("count: %d\n", i);
    495     }
    496 }
     493  {
     494    PRINT(0)("  - ParticleDeadList is used: ");
     495    int i = 1;
     496    Particle* tmpPart = this->deadList;
     497    while (tmpPart = tmpPart->next) ++i;
     498    PRINT(0)("count: %d\n", i);
     499  }
     500}
Note: See TracChangeset for help on using the changeset viewer.