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.

particle.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // particle.cpp
  3. // emptyExample
  4. //
  5. // Created by Sebastian Holzki on 16.04.19.
  6. //
  7. #include "particle.h"
  8. Particle::Particle()
  9. {
  10. }
  11. // -----------------------------------
  12. Particle::~Particle()
  13. {
  14. }
  15. // -----------------------------------
  16. void Particle::setup(ofVec2f pos, float maxAge) {
  17. this->position = pos; //Pointer to Position ofVec2f position
  18. velocity.set(0, 0);
  19. //vel.set(ofRandom(-20.0, 20.0), ofRandom(-90, -100)); //Movement direction
  20. age = 0.0;
  21. maxLife = 12.0;
  22. //maxLife = ofRandom(maxAge - 5, maxAge); //Max life of a particle
  23. color.set(5, 241, 219);
  24. //size = 2.0;
  25. //mass = 100;
  26. size = ofRandom(4.0, 0.01);
  27. mass = ofRandom(100, 250); //Changes the particle velocity
  28. valueToMoveToTop = 0; //Counter which causes the particle and the attractor to move to top
  29. valueToMoveToRight = 0; //Counter which causes the particle and the attractor to move to right
  30. ticksToMoveParticlesToRight = 70; //Framerate for movement velocity
  31. counterToMoveParticlesToRight = 0; //Counter for delay on top
  32. particleLeftScene = false; //Particle are out of ScenesizeX on the right side
  33. }
  34. // -----------------------------------
  35. void Particle::update(float deltaT) {
  36. }
  37. // --------------------------------------------------------------------------------
  38. void Particle::updateParticle(double deltaT, ofVec2f attractor, bool cloudAttractorIsSet, bool imageIsOnTop, bool tornadoIsFinished, int imageHeight, int imageWidth, float sceneSizeX, float sceneSizeY) {
  39. //Movement of particle in the different settings
  40. doMovementOfParticlesAtRain(tornadoIsFinished, deltaT, sceneSizeX);
  41. if (cloudAttractorIsSet == true) {
  42. doMovementOfParticlesAtRocketEffect(sceneSizeY, imageHeight, imageWidth, sceneSizeX, attractor, deltaT);
  43. }
  44. if (tornadoIsFinished == true && cloudAttractorIsSet == false) {
  45. doMovementOfParticlesAtSymbols(deltaT, attractor);
  46. }
  47. if (imageIsOnTop == true) {
  48. doMovementOfHexagonOnTheTop(attractor, sceneSizeX, deltaT);
  49. }
  50. }
  51. //--------------------------------------------------------------
  52. void Particle::doMovementOfParticlesAtSymbols(double deltaT, ofVec2f &attractor)
  53. {
  54. valueToMoveToTop = 0;
  55. valueToMoveToRight = 0;
  56. counterToMoveParticlesToRight = 0;
  57. age += deltaT;
  58. vel *= 0.1;
  59. ofVec2f force = attractor - position; //Attraction
  60. if (50 < force.length() < 150) { //Movement of the particle which is located at a radius of 50 to 150 around the attractor
  61. force = 10 * force.getNormalized();
  62. vel += force; //Movement to attractor
  63. vel = mass * vel.getNormalized();
  64. }
  65. else if (150 < force.length() < 500) { //Movement of the particle which is located at a radius of 150 to 500 around the attractor
  66. force = 8 * force.getNormalized();
  67. vel += force;
  68. vel = mass * vel.getNormalized();
  69. }
  70. else { //Movement of the particle which is located at a radius more than 500 around the attractor
  71. force = 2 * force.getNormalized();
  72. vel += force;
  73. vel = mass / 1.2* vel.getNormalized();
  74. }
  75. position += (vel / 1.5 * deltaT); //Position = m/s * s [particle placed static]
  76. }
  77. //--------------------------------------------------------------
  78. void Particle::doMovementOfParticlesAtRocketEffect(float sceneSizeY, int imageHeight, int imageWidth, float sceneSizeX, ofVec2f &attractor, double deltaT)
  79. {
  80. int y = ((sceneSizeY / 2) + imageHeight); //Beginning height for attractor
  81. int x = ofRandom(sceneSizeX / 2 - imageWidth / 2, sceneSizeX / 2 + imageWidth / 2); //Width for attractor
  82. if (y - valueToMoveToTop - imageHeight > 200) { //Increase counter depending on velocity for movement to top
  83. valueToMoveToTop += 3; //Movement by 3 to top (pro Frame)
  84. }
  85. else if (y - valueToMoveToTop - imageHeight > 10) { //Increase counter depending on velocity for movement to top
  86. valueToMoveToTop += 2; //Movement by 3 to top (pro Frame)
  87. }
  88. else if (counterToMoveParticlesToRight < ticksToMoveParticlesToRight) { //Delay on top
  89. counterToMoveParticlesToRight++;
  90. }
  91. else if (y - valueToMoveToTop - imageHeight <= 10) { //Increase counter depending on velocity for movement to right( Movement by 3 to right (pro Frame))
  92. valueToMoveToRight += 3;
  93. }
  94. attractor.set(x + valueToMoveToRight, y - valueToMoveToTop);
  95. age += deltaT;
  96. vel *= 0.1;
  97. ofVec2f force = (attractor - position); //Attraction
  98. if (30 < force.length() < 150) { //Movement of the particle which is located at a radius of 30 to 150 around the attractor
  99. force = 17 * force.getNormalized();
  100. vel += force; //Movement to attractor
  101. vel = (mass / 1.2) * vel.getNormalized(); //Particle don't pass the symbol
  102. }
  103. else if (150 < force.length() < 250) { //Movement of the particle which is located at a radius of 150 to 250 around the attractor
  104. force = 14 * force.getNormalized();
  105. vel += force;
  106. vel = mass * 10 * vel.getNormalized();
  107. }
  108. else if (250 < force.length() < 500) { //Movement of the particle which is located at a radius of 250 to 500 around the attractor
  109. force = 14 * force.getNormalized();
  110. vel += force;
  111. vel = mass * 4 * vel.getNormalized();
  112. }
  113. else { //Movement of the particle which is located at a radius more than 500 around the attractor
  114. force = 20 * force.getNormalized();
  115. vel += force;
  116. vel = mass * vel.getNormalized();
  117. }
  118. position += (vel / 1.7 * deltaT); //Position = m/s * s [particle placed static]
  119. }
  120. //--------------------------------------------------------------
  121. void Particle::doMovementOfHexagonOnTheTop(ofVec2f &attractor, float sceneSizeX, double deltaT)
  122. {
  123. if (attractor.x + valueToMoveToRight >= sceneSizeX + 120) {
  124. particleLeftScene = true;
  125. }
  126. else {
  127. particleLeftScene = false;
  128. }
  129. age += deltaT;
  130. vel *= 0.1;
  131. ofVec2f force = attractor - position; //Attraction
  132. if (50 < force.length() < 150) { //Movement of the particle which is located at a radius of 50 to 150 around the attractor
  133. force = 60 * force.getNormalized(); //Anziehungskraft des Attraktors auf die Partikel
  134. vel += force; //Bewegung zum Attraktor
  135. vel = mass * vel.getNormalized();
  136. }
  137. else { //Movement of the particle which is located at a radius of more than 150 around the attractor
  138. force = 100 * force.getNormalized();
  139. vel += force;
  140. vel = mass / 2 * vel.getNormalized();
  141. }
  142. position += (vel * deltaT); //Position = m/s * s [particle placed static]
  143. }
  144. // -----------------------------------
  145. void Particle::draw() {
  146. if (position.x > 0 || position.x < 300) {
  147. ofSetColor(this->color); //To make particle turquoise
  148. color.set(getAgeNorm() * 241, 241 / getAgeNorm(), 219); //Color (Disco)
  149. }
  150. else {
  151. ofSetColor(255, 255, 255);
  152. }
  153. ofDrawCircle(position, size);
  154. }
  155. //--------------------------------------------------------------
  156. float Particle::getAgeNorm() {
  157. return age / maxLife;
  158. }
  159. //--------------------------------------------------------------
  160. float Particle::deleteAfterLeavingSceneY() {
  161. return position.y < 0 || position.y > ofGetHeight();
  162. }
  163. //--------------------------------------------------------------
  164. bool Particle::deleteAfterLeavingSceneX() {
  165. return particleLeftScene;
  166. }
  167. //------------------------------------------------------------------
  168. void Particle::setMode(particleMode newMode) {
  169. mode = newMode;
  170. }
  171. //-----------------------------------
  172. float Particle::getMaxLife() {
  173. return maxLife;
  174. }
  175. //-----------------------------------
  176. float Particle::getAge() {
  177. return age;
  178. }
  179. //-----------------------------------
  180. void Particle::mapParticle() {
  181. /*
  182. Put an if Statement before it:
  183. if(borderCollission == true){mapParticle()}
  184. The particle will be mapped to a new position, using information about:
  185. - old position
  186. - velocity (direction)
  187. - defined borders in the projection --> globals like window size, angle between "stelen", width of stelen, etc.
  188. if the particle hits a border
  189. */
  190. }