Quelcode des Partikelsystems Boden
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

particleSystem.cpp 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // particleSystem.cpp
  3. // emptyExample
  4. //
  5. // Created by Sebastian Holzki on 16.04.19.
  6. //
  7. #include "particleSystem.hpp"
  8. ParticleSystem::ParticleSystem(){
  9. }
  10. // -----------------------------------
  11. ParticleSystem::~ParticleSystem(){
  12. }
  13. // -----------------------------------
  14. void ParticleSystem::setup(){
  15. // xmlParticleSystems.loadFile("xml/particleSystem.xml");
  16. // this->particles.push_back(new Particle);
  17. // std::cout << "breakpoint particleSystem_1 erreicht, particleSystem_setup beendet" << std::endl;
  18. }
  19. // -----------------------------------------------
  20. //*** PARTICLESYSTEM UPDATE *** PARTICLESYSTEM UPDATE *** PARTICLESYSTEM UPDATE *** PARTICLESYSTEM UPDATE ***
  21. void ParticleSystem::update(float deltaT, particleMode currentMode, vector<ObjectPhysics*>* objectPhysics, vector<CheckedInVisitor*>* checkedInVisitors, float sceneWidth, float sceneHeight){
  22. //preparation
  23. double DELTA_T_IN_SECONDS = deltaT/1000;
  24. this->currentMode = currentMode;
  25. // *** PARTICLESYSTEM SWITCH *** PARTICLESYSTEM SWITCH *** PARTICLESYSTEM SWITCH *** PARTICLESYSTEM SWITCH ***
  26. //If the particlesystem itself needs some logic, it will happen in the following switch!
  27. switch ( currentMode )
  28. {
  29. case PARTICLE_MODE_DEFAULT: {
  30. //what does the particle system do if its mode is 'PARTICLE_MODE_DEFAULT'
  31. // value a = xmlParticleSystems.getValue (" default:..." , 0 ) ;
  32. // value b = xmlParticleSystems.getValue (" default:..." , 0 ) ;
  33. // value c = xmlParticleSystems.getValue (" default:..." , 0 ) ;
  34. // value d = xmlParticleSystems.getValue (" default:..." , 0 ) ;
  35. // value e ....
  36. // ...
  37. // PARTICLESYSTEM_BIRTHRATE = xmlParticleSystems.getValue("default:birthrate", 0);
  38. PARTICLESYSTEM_BIRTHRATE = 1; //testing default
  39. // std::cout << "birthrate from xml: " + ofToString(PARTICLESYSTEM_BIRTHRATE) << std::endl;
  40. break;
  41. }
  42. //------------------------
  43. case PARTICLE_MODE_POLYMESH: {
  44. //what does the particle system do if its mode is 'PARTICLE_MODE_POLYMESH'
  45. PARTICLESYSTEM_BIRTHRATE = 0;
  46. updateLines(currentMode, objectPhysics);
  47. // PARTICLESYSTEM_BIRTHRATE = xmlParticleSystems.getValue("polymesh:birthrate", 0);
  48. int PARTICLESYSTEM_BIRTHRATE_CHECKEDINVISITORS = 4;
  49. while (PRODUCED_PER_EMITTER < PARTICLESYSTEM_BIRTHRATE_CHECKEDINVISITORS){
  50. for(int i = 0; i < checkedInVisitors->size(); i++){
  51. if( checkedInVisitors->at(i)->getAge() <= 7){
  52. this->particles.push_back(new Particle);
  53. particles.back()->setup(checkedInVisitors->at(i)->getBottom(), sceneHeight);
  54. particles.back()->setRandomVelocityOnlyGoingUp();
  55. }
  56. }
  57. PRODUCED_PER_EMITTER++;
  58. }
  59. PRODUCED_PER_EMITTER = 0;
  60. break;
  61. }
  62. //------------------------
  63. case PARTICLE_MODE_RFID: {
  64. //what does the particle system do if its mode is 'PARTICLE_MODE_RFID'
  65. while (PRODUCED_PER_EMITTER < PARTICLESYSTEM_BIRTHRATE){
  66. for(int i = 0; i < checkedInVisitors->size(); i++){
  67. if( checkedInVisitors->at(i)->getAge() <= 5){
  68. this->particles.push_back(new Particle);
  69. particles.back()->setup(checkedInVisitors->at(i)->getBottom(), sceneHeight);
  70. particles.back()->setRandomVelocityOnlyGoingUp();
  71. }
  72. }
  73. PRODUCED_PER_EMITTER++;
  74. }
  75. PRODUCED_PER_EMITTER = 0;
  76. break;
  77. }
  78. //------------------------
  79. default: {
  80. //following two lines only help for compiling right now
  81. int a = 0;
  82. a++;
  83. }
  84. } //end of switch
  85. // std::cout << "breakpoint particleSystem_2 erreicht, switch anweisung beendet" << std::endl;
  86. // *** BIRTH CONTROL *** BIRTH CONTROL *** BIRTH CONTROL ***
  87. //the system will emit a new particle for every emitter, as long as the PRODUCED_PER_EMITTER is smaller than the wanted BIRTHRATE
  88. //PPE is growing until it reaches deltaT, so the while statement will be executed more often, if deltaT gets bigger
  89. //as long as the birthcount grows and is lower then the birthrate (depending on the mode (XML) ) it will emit once for every emitter
  90. //that is active (bool emitting = true)
  91. while (PRODUCED_PER_EMITTER < PARTICLESYSTEM_BIRTHRATE){
  92. for(int i = 0; i < objectPhysics->size(); i++){
  93. // if(( (*objectPhysics).at(i)->type == "emitter" ) && (objectPhysics->at(i)->emitting == true)){
  94. if( objectPhysics->at(i)->emitting == true){
  95. this->particles.push_back(new Particle);
  96. particles.back()->setup(objectPhysics->at(i)->getPosition(), sceneHeight);
  97. if(objectPhysics->at(i)->type == "emitterOnStele"){
  98. particles.back()->setRandomVelocityOnlyGoingUp();
  99. }else{
  100. particles.back()->setRandomVelocity();
  101. }
  102. }
  103. }
  104. PRODUCED_PER_EMITTER++;
  105. }
  106. PRODUCED_PER_EMITTER = 0;
  107. // *** PARTICLE SYSTEM UPDATING ITS PARTICLES *** *** PARTICLE SYSTEM UPDATING ITS PARTICLES *** PARTICLE SYSTEM UPDATING ITS PARTICLES ***
  108. //for "alle partikel im system" update in abhängigkeit vom übergebenen "mode"
  109. //The following lines will start working, when Particle::update has been reworked is active and particleMode implemented
  110. for (int p = 0; p < particles.size(); p++){
  111. particles.at(p)->update(deltaT, currentMode, objectPhysics, checkedInVisitors, sceneWidth, sceneHeight);
  112. //Delete particles that reached maxAge
  113. if(currentMode != PARTICLE_MODE_POLYMESH){
  114. if (particles.at(p)->getAgeNorm() > 1) {
  115. delete particles.at(p);
  116. particles.erase(particles.begin() + p);
  117. }
  118. }
  119. }
  120. }
  121. // -----------------------------------------------
  122. void ParticleSystem::draw(typeOfView activeTypeOfView){
  123. for(int i = 0; i < particles.size(); i++){
  124. particles.at(i)->draw(activeTypeOfView);
  125. }
  126. if( currentMode == PARTICLE_MODE_POLYMESH){
  127. for(int i = 0; i < lines.size(); i++){
  128. ofDrawLine(lines.at(i)->point1, lines.at(i)->point2);
  129. }
  130. }
  131. }
  132. // -----------------------------------------------
  133. void ParticleSystem::updateLines(particleMode currentMode, vector<ObjectPhysics*>* objectPhysics){
  134. lines.clear();
  135. if(currentMode == PARTICLE_MODE_POLYMESH){
  136. for(int i = 0; i < objectPhysics->size(); i++){
  137. if(( (*objectPhysics).at(i)->type == "attraktor" ) && (objectPhysics->at(i)->attracting == true)){
  138. for(int p = 0; p < particles.size(); p++){
  139. if(particles.at(p)->particleIsOnGround==true){
  140. ofVec2f distance = particles.at(p)->getPosition() - objectPhysics->at(i)->getPosition();
  141. if(distance.length() < 150){
  142. lines.push_back(new Line(particles.at(p)->getPosition(), objectPhysics->at(i)->getPosition()));
  143. for(int p2 = 0; p2 < particles.size(); p2++){
  144. ofVec2f distance = particles.at(p)->getPosition() - particles.at(p2)->getPosition();
  145. if(distance.length() < 50){
  146. lines.push_back(new Line(particles.at(p)->getPosition(), particles.at(p2)->getPosition()));
  147. }
  148. p2 += 4; //lower value -> worse performance & nicer effect (more connections between the particles)
  149. }
  150. }
  151. // p += 2 ; //if your system shows performance issues you might need to activate this line! (downside: effect seems to jump)
  152. }
  153. } //end of for schleife
  154. }
  155. }
  156. }
  157. }