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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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::doMovementOfParticlesAtRain(bool tornadoIsFinished, double deltaT, float sceneSizeX)
  53. {
  54. if (tornadoIsFinished == false) { //Movement of partile from bottom to top
  55. position += vel * deltaT;
  56. age += deltaT;
  57. if (position.x >= sceneSizeX) {
  58. position.x = ofRandom(-1, -5);
  59. }
  60. }
  61. }
  62. //--------------------------------------------------------------
  63. void Particle::doMovementOfParticlesAtSymbols(double deltaT, ofVec2f &attractor)
  64. {
  65. valueToMoveToTop = 0;
  66. valueToMoveToRight = 0;
  67. counterToMoveParticlesToRight = 0;
  68. age += deltaT;
  69. vel *= 0.1;
  70. ofVec2f force = attractor - position; //Attraction
  71. if (50 < force.length() < 150) { //Movement of the particle which is located at a radius of 50 to 150 around the attractor
  72. force = 10 * force.getNormalized();
  73. vel += force; //Movement to attractor
  74. vel = mass * vel.getNormalized();
  75. }
  76. else if (150 < force.length() < 500) { //Movement of the particle which is located at a radius of 150 to 500 around the attractor
  77. force = 8 * force.getNormalized();
  78. vel += force;
  79. vel = mass * vel.getNormalized();
  80. }
  81. else { //Movement of the particle which is located at a radius more than 500 around the attractor
  82. force = 2 * force.getNormalized();
  83. vel += force;
  84. vel = mass / 1.2* vel.getNormalized();
  85. }
  86. position += (vel / 1.5 * deltaT); //Position = m/s * s [particle placed static]
  87. }
  88. //--------------------------------------------------------------
  89. void Particle::doMovementOfParticlesAtRocketEffect(float sceneSizeY, int imageHeight, int imageWidth, float sceneSizeX, ofVec2f &attractor, double deltaT)
  90. {
  91. int y = ((sceneSizeY / 2) + imageHeight); //Beginning height for attractor
  92. int x = ofRandom(sceneSizeX / 2 - imageWidth / 2, sceneSizeX / 2 + imageWidth / 2); //Width for attractor
  93. if (y - valueToMoveToTop - imageHeight > 200) { //Increase counter depending on velocity for movement to top
  94. valueToMoveToTop += 3; //Movement by 3 to top (pro Frame)
  95. }
  96. else if (y - valueToMoveToTop - imageHeight > 10) { //Increase counter depending on velocity for movement to top
  97. valueToMoveToTop += 2; //Movement by 3 to top (pro Frame)
  98. }
  99. else if (counterToMoveParticlesToRight < ticksToMoveParticlesToRight) { //Delay on top
  100. counterToMoveParticlesToRight++;
  101. }
  102. else if (y - valueToMoveToTop - imageHeight <= 10) { //Increase counter depending on velocity for movement to right( Movement by 3 to right (pro Frame))
  103. valueToMoveToRight += 3;
  104. }
  105. attractor.set(x + valueToMoveToRight, y - valueToMoveToTop);
  106. age += deltaT;
  107. vel *= 0.1;
  108. ofVec2f force = (attractor - position); //Attraction
  109. if (30 < force.length() < 150) { //Movement of the particle which is located at a radius of 30 to 150 around the attractor
  110. force = 17 * force.getNormalized();
  111. vel += force; //Movement to attractor
  112. vel = (mass / 1.2) * vel.getNormalized(); //Particle don't pass the symbol
  113. }
  114. else if (150 < force.length() < 250) { //Movement of the particle which is located at a radius of 150 to 250 around the attractor
  115. force = 14 * force.getNormalized();
  116. vel += force;
  117. vel = mass * 10 * vel.getNormalized();
  118. }
  119. else if (250 < force.length() < 500) { //Movement of the particle which is located at a radius of 250 to 500 around the attractor
  120. force = 14 * force.getNormalized();
  121. vel += force;
  122. vel = mass * 4 * vel.getNormalized();
  123. }
  124. else { //Movement of the particle which is located at a radius more than 500 around the attractor
  125. force = 20 * force.getNormalized();
  126. vel += force;
  127. vel = mass * vel.getNormalized();
  128. }
  129. position += (vel / 1.7 * deltaT); //Position = m/s * s [particle placed static]
  130. }
  131. //--------------------------------------------------------------
  132. void Particle::doMovementOfHexagonOnTheTop(ofVec2f &attractor, float sceneSizeX, double deltaT)
  133. {
  134. if (attractor.x + valueToMoveToRight >= sceneSizeX + 120) {
  135. particleLeftScene = true;
  136. }
  137. else {
  138. particleLeftScene = false;
  139. }
  140. age += deltaT;
  141. vel *= 0.1;
  142. ofVec2f force = attractor - position; //Attraction
  143. if (50 < force.length() < 150) { //Movement of the particle which is located at a radius of 50 to 150 around the attractor
  144. force = 60 * force.getNormalized(); //Anziehungskraft des Attraktors auf die Partikel
  145. vel += force; //Bewegung zum Attraktor
  146. vel = mass * vel.getNormalized();
  147. }
  148. else { //Movement of the particle which is located at a radius of more than 150 around the attractor
  149. force = 100 * force.getNormalized();
  150. vel += force;
  151. vel = mass / 2 * vel.getNormalized();
  152. }
  153. position += (vel * deltaT); //Position = m/s * s [particle placed static]
  154. }
  155. // -----------------------------------
  156. void Particle::draw() {
  157. if (position.x > 0 || position.x < 300) {
  158. ofSetColor(this->color); //To make particle turquoise
  159. color.set(getAgeNorm() * 241, 241 / getAgeNorm(), 219); //Color (Disco)
  160. }
  161. else {
  162. ofSetColor(255, 255, 255);
  163. }
  164. ofDrawCircle(position, size);
  165. }
  166. //--------------------------------------------------------------
  167. float Particle::getAgeNorm() {
  168. return age / maxLife;
  169. }
  170. //--------------------------------------------------------------
  171. float Particle::deleteAfterLeavingSceneY() {
  172. return position.y < 0 || position.y > ofGetHeight();
  173. }
  174. //--------------------------------------------------------------
  175. bool Particle::deleteAfterLeavingSceneX() {
  176. return particleLeftScene;
  177. }
  178. //------------------------------------------------------------------
  179. void Particle::setMode(particleMode newMode) {
  180. mode = newMode;
  181. }
  182. //-----------------------------------
  183. float Particle::getMaxLife() {
  184. return maxLife;
  185. }
  186. //-----------------------------------
  187. float Particle::getAge() {
  188. return age;
  189. }
  190. //-----------------------------------
  191. void Particle::mapParticle() {
  192. /*
  193. Put an if Statement before it:
  194. if(borderCollission == true){mapParticle()}
  195. The particle will be mapped to a new position, using information about:
  196. - old position
  197. - velocity (direction)
  198. - defined borders in the projection --> globals like window size, angle between "stelen", width of stelen, etc.
  199. if the particle hits a border
  200. */
  201. }