Projektordner für das Team Deutsches Museum (FORUM).
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.

imageParticleSystem.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "imageParticleSystem.h"
  2. ImageParticleSystem::ImageParticleSystem(int sceneSizeX, int sceneSizeY, ofImage fileImageHex, string imageName) {
  3. this->imageToDraw = new DrawableImage(imageName, sceneSizeX, sceneSizeY);
  4. this->imageHeight = imageToDraw->getHeight();
  5. this->imageWidth = imageToDraw->getWidth();
  6. this->sceneSizeX = sceneSizeX;
  7. this->sceneSizeY = sceneSizeY;
  8. this->fileImageHex = fileImageHex;
  9. setAttractorsFromHexagonFromPicture();
  10. maxParticle = 50;
  11. setSymbolAttractorIsSet(true);
  12. setCloudAttractorIsSet(false);
  13. ticksToMoveImageToTop = 200;
  14. counterToMoveImageToTop = 0;
  15. fileImageCloud.loadImage("Hexagon.png");
  16. imageReachedTopAndAttractorIsChanged = false;
  17. }
  18. //----------------------------------------------------------
  19. void ImageParticleSystem::updateParticleSystem() {
  20. double deltaT = ofGetLastFrameTime();
  21. time += deltaT;
  22. if ((cloudAttractorIsSet == false) && (particles.size() < picPix / 7) && (this->imageToDraw->imageIsOnTop(sceneSizeY) == false)) { //Creating particles for symbol on bottom
  23. createParticlesForHexagonInSymbol();
  24. }
  25. else if ((cloudAttractorIsSet == false) && (particles.size() < picPix / 7) && (this->imageToDraw->imageIsOnTop(sceneSizeY))) { //Creating particles for symbol in cloud
  26. createParticlesForHexagonInCloud();
  27. }
  28. else if ((cloudAttractorIsSet == false) && (particles.size() > picPix / 7)) { //Deleting unused particles for hexagon on bottom
  29. deleteParticlesForHexagon();
  30. }
  31. else if ((cloudAttractorIsSet == true) && (particles.size() > picPix / 7)) { //Deleting unused particles for rocketeffect
  32. //deleteParticlesForRocketEffect();
  33. }
  34. //Movement
  35. for (int p = 0; p < particles.size(); p++) {
  36. if (p * 7 < attractors.size()) {
  37. if (cloudAttractorIsSet == true) { //Movement at rocketeffect
  38. particles.at(p)->updateParticle(deltaT, attractors[p * 7],
  39. cloudAttractorIsSet, this->imageToDraw->imageIsOnTop(sceneSizeY), true, imageHeight, imageWidth, sceneSizeX, sceneSizeY);
  40. }
  41. else if (symbolAttractorIsSet == true) //Movement at Symbol at the bottom
  42. {
  43. particles.at(p)->updateParticle(deltaT, attractors[p * 7],
  44. cloudAttractorIsSet, this->imageToDraw->imageIsOnTop(sceneSizeY), true, imageHeight, imageWidth, sceneSizeX, sceneSizeY);
  45. if (this->imageToDraw->imageIsOnTop(sceneSizeY)) //Deleting the particle after they left scene at right
  46. {
  47. deleteParticleAfterLeavingOntheRightAndCreateThemOnTheLeft(p);
  48. }
  49. }
  50. }
  51. }
  52. if (counterToMoveImageToTop < ticksToMoveImageToTop) { //Delay (every Frame) before the symbol and particle pass to the rocket effect
  53. counterToMoveImageToTop++;
  54. }
  55. else if (counterToMoveImageToTop == ticksToMoveImageToTop) { //Symbol and particles do over in rocketeffect
  56. changeAttractorImage(fileImageCloud);
  57. setCloudAttractorIsSet(true);
  58. }
  59. if (this->imageToDraw->imageIsOnTop(sceneSizeY)) { //Symbol and particles reached max. y-position and attractor gets changed from rocketeffect to hexagon
  60. setAttractorsFromHexagonFromPicture();
  61. cloudAttractorIsSet = false;
  62. }
  63. }
  64. //----------------------------------------------------------
  65. void ImageParticleSystem::createParticlesForHexagonInSymbol()
  66. {
  67. int newPix = (picPix / 7) - particles.size();
  68. for (int i = 1; i <= newPix; i++) { //Go through pixel i = 1 (there is no pixel 0)
  69. particles.push_back(new Particle);
  70. int x = sceneSizeX / 2;
  71. int y = sceneSizeY;
  72. particles.back()->setup(ofVec2f(x, y), 20);
  73. }
  74. }
  75. //----------------------------------------------------------
  76. void ImageParticleSystem::createParticlesForHexagonInCloud()
  77. {
  78. int newPix = (picPix / 7) - particles.size();
  79. for (int i = 1; i <= newPix; i++) { //Go through pixel i = 1 (there is no pixel 0)
  80. particles.push_back(new Particle);
  81. int x = sceneSizeX / 2;
  82. int y = imageToDraw->getImagePosY(sceneSizeY) + imageHeight;
  83. particles.back()->setup(ofVec2f(x, y), 20);
  84. }
  85. }
  86. //----------------------------------------------------------
  87. void ImageParticleSystem::deleteParticlesForRocketEffect()
  88. {
  89. int newPix = (particles.size() - (picPix / 7));
  90. for (int i = 0; i < newPix; i++) {
  91. delete particles.at(0); //Deleting particle object
  92. particles.erase(particles.begin()); //Deleting pointer to particle
  93. }
  94. }
  95. //----------------------------------------------------------
  96. void ImageParticleSystem::deleteParticlesForHexagon()
  97. {
  98. int newPix = (particles.size() - (picPix / 7));
  99. for (int i = 0; i < newPix; i++) {
  100. delete particles.at(0); //Deleting particle object
  101. particles.erase(particles.begin()); //Deleting pointer to particle
  102. }
  103. }
  104. //----------------------------------------------------------
  105. void ImageParticleSystem::deleteParticleAfterLeavingOntheRightAndCreateThemOnTheLeft(int p)
  106. {
  107. bool particleToDelete = particles.at(p)->deleteAfterLeavingSceneX();
  108. if (particleToDelete) {
  109. delete particles.at(0); //Deleting particle object
  110. particles.erase(particles.begin()); //Deleting pointer to particle
  111. //Durchgehen ab Partikel i = 1 da es kein Pixel 0 gibt
  112. particles.push_back(new Particle);
  113. int x = -50;
  114. int y = imageToDraw->getHeight();
  115. particles.back()->setup(ofVec2f(x, y), 20);
  116. }
  117. }
  118. //----------------------------------------------------------
  119. void ImageParticleSystem::changeAttractorImage(ofImage newAttractorImage) { //Attractor is changed between hexagon and cloud
  120. attractors = pixelInVector(newAttractorImage);
  121. }
  122. //----------------------------------------------------------
  123. void ImageParticleSystem::setAttractorsFromHexagonFromPicture() { //Hexagon is attracot (pixel from hexagon get converted in attractors)
  124. int picWidth = fileImageHex.getWidth();
  125. int picHeight = fileImageHex.getHeight();
  126. ofPixels pix;
  127. pix = fileImageHex.getPixels();
  128. vector<ofVec2f> pxPos;
  129. picPix = 0;
  130. for (int i = 3; i <= pix.size(); i += 4) { //i specifys that every fourth color information of the pixel is handled (rgba)
  131. if (pix[i] > 0) {
  132. int width = pix.getWidth();
  133. int y = i / 4 / width;
  134. int x = i / 4 % width;
  135. ofVec2f vec;
  136. vec.set(x + imageToDraw->getImagePosX(sceneSizeX), y + imageToDraw->getImagePosY(sceneSizeY)); //Gets position of image and so that the attractor follows movement
  137. pxPos.push_back(vec);
  138. picPix++;
  139. }
  140. }
  141. attractors = pxPos;
  142. }
  143. //----------------------------------------------------------
  144. vector<ofVec2f> ImageParticleSystem::pixelInVector(ofImage a) { //Read in all the coloured pixels of image and vonvert them in vectors
  145. int picWidth = a.getWidth();
  146. int picHeight = a.getHeight();
  147. ofPixels pix;
  148. pix = a.getPixels();
  149. vector<ofVec2f> pxPos;
  150. picPix = 0;
  151. for (int i = 3; i <= pix.size(); i += 4) { //i specifys that every fourth color information of the pixel is handled (rgba)
  152. if (pix[i] > 0) {
  153. int width = pix.getWidth();
  154. int y = i / 4 / width;
  155. int x = i / 4 % width;
  156. ofVec2f vec;
  157. vec.set(x + ((sceneSizeX / 2) - picWidth / 2), y - ((sceneSizeY)-picHeight - 7));
  158. pxPos.push_back(vec);
  159. picPix++;
  160. }
  161. }
  162. return pxPos;
  163. }
  164. //----------------------------------------------------------
  165. void ImageParticleSystem::drawImageParticleSystem() { //Drawing of symbols and particles
  166. imageToDraw->updateImage(sceneSizeX, sceneSizeY);
  167. for (int i = 0; i < particles.size(); i++) {
  168. particles.at(i)->draw();
  169. }
  170. }
  171. //----------------------------------------------------------
  172. void ImageParticleSystem::setSymbolAttractorIsSet(bool value) {
  173. imageToDraw->symbolAttractorIsSet = value;
  174. symbolAttractorIsSet = value;
  175. }
  176. //----------------------------------------------------------
  177. void ImageParticleSystem::setCloudAttractorIsSet(bool value) {
  178. imageToDraw->cloudAttractorIsSet = value;
  179. cloudAttractorIsSet = value;
  180. }