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.

particle.cpp 5.1KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // particle.cpp
  2. // particleSystem
  3. //
  4. #include "particle.h"
  5. #include "ofApp.h"
  6. Particle::Particle()
  7. {
  8. uniqueVal = ofRandom(-10000, 10000);
  9. }
  10. //--------------------------------------------------------------------------------------
  11. void Particle::setup(particleMode newMode) {
  12. mode = newMode;
  13. age = 0.0;
  14. //maxLife = 30.0;
  15. frc = ofVec2f(0, 0);
  16. //size = ofRandom(4.0, 0.01);
  17. size = .5;
  18. maxLife = 0.99;
  19. mass = ofRandom(100, 250);
  20. if (mode == PARTICLE_MODE_DEFAULT) {
  21. effect.loadFile("xml/default.xml");
  22. int velMin = effect.getValue("default:velMin", 0);
  23. int velMax = effect.getValue("default:velMax", 0);
  24. int lifeMax = effect.getValue("default:maxLife", 0);
  25. setVel(velMin, velMax);
  26. pos.x = ofRandomWidth();
  27. pos.y = ofRandomHeight();
  28. }
  29. else if (mode == PARTICLE_MODE_RADIAL) {
  30. effect.loadFile("xml/radial.xml");
  31. int velMin = effect.getValue("radial:velMin", 0);
  32. int velMax = effect.getValue("radial:velMax", 0);
  33. setVel(velMin, velMax);
  34. pos.x = ofGetWidth()/2;
  35. pos.y = ofGetHeight()/2;
  36. }
  37. else if (mode == PARTICLE_MODE_RAIN) {
  38. effect.loadFile("xml/rain.xml");
  39. vel.x = ofRandom(-40, 40);
  40. vel.y = ofRandom(-40, 40);
  41. pos.x = ofRandomWidth();
  42. pos.y = ofRandomHeight();
  43. drag = ofRandom(0.97, 0.99);
  44. vel.y = fabs(vel.y) * 6.0;
  45. }
  46. else if (mode == PARTICLE_MODE_ATTRACTOR) {
  47. effect.loadFile("xml/rain.xml");
  48. vel.x = ofRandom(-40, 40);
  49. vel.y = ofRandom(-40, 40);
  50. pos.x = ofRandomWidth();
  51. pos.y = ofRandomHeight();
  52. }
  53. else if (mode == PARTICLE_MODE_DETRACTOR) {
  54. effect.loadFile("xml/rain.xml");
  55. vel.x = ofRandom(-40, 40);
  56. vel.y = ofRandom(-40, 40);
  57. pos.x = ofRandomWidth();
  58. pos.y = ofRandomHeight();
  59. }
  60. else {
  61. drag = ofRandom(0.95, 0.998);
  62. }
  63. tex.load("img/br.png");
  64. }
  65. //--------------------------------------------------------------------------------------
  66. void Particle::update(float deltaT, vector<Attractor*>* attractors,vector<Particle*> system) {
  67. if (mode == PARTICLE_MODE_DEFAULT) {
  68. }
  69. else if (mode == PARTICLE_MODE_RAIN) {
  70. float wind = ofSignedNoise(pos.x * 0.003, pos.y * 0.0006, ofGetElapsedTimef() * 0.6);
  71. frc.x = wind * 0.25 + ofSignedNoise(uniqueVal, pos.y * 0.08) * 0.6;
  72. frc.y = ofSignedNoise(uniqueVal, pos.x * 0.06, ofGetElapsedTimef()*0.2) * 0.09 + 0.18;
  73. vel *= drag;
  74. vel += frc * 0.2;
  75. if (pos.y + vel.y > ofGetHeight()) {
  76. pos.y -= ofGetHeight();
  77. }
  78. }
  79. else if (mode == PARTICLE_MODE_ATTRACTOR) {
  80. ofVec2f force;
  81. counterOfActiveAttractors = 0;
  82. for (int i = 0; i < attractors->size(); i++) {
  83. if (attractors->at(i)->getX() == 0 && attractors->at(i)->getY() == 0) {
  84. vel += force;
  85. vel = mass * vel.getNormalized();
  86. break;
  87. }
  88. counterOfActiveAttractors++;
  89. force.set(attractors->at(i)->getX() - pos.x, attractors->at(i)->getY() - pos.y);
  90. if (force.length() < 250) {
  91. force = 40 * force.getNormalized();
  92. size = size + ofRandom(.03, 0.01);
  93. vel += force;
  94. vel = mass * vel.getNormalized();
  95. }
  96. else {
  97. force = 0 * force.getNormalized();
  98. vel += force;
  99. vel = mass * vel.getNormalized();
  100. }
  101. }
  102. }
  103. else if (mode == PARTICLE_MODE_DETRACTOR) {
  104. ofVec2f force;
  105. counterOfActiveAttractors = 0;
  106. for (int i = 0; i < attractors->size(); i++) {
  107. if (attractors->at(i)->getX() == 0 && attractors->at(i)->getY() == 0) {
  108. //force = 0 * force.getNormalized();
  109. vel += force;
  110. vel = mass * vel.getNormalized();
  111. break;
  112. }
  113. counterOfActiveAttractors++;
  114. force.set(attractors->at(i)->getX() - pos.x, attractors->at(i)->getY() - pos.y);
  115. if (force.length() < 600) {
  116. force.normalize();
  117. size = size + ofRandom(.01, 0.01);
  118. vel += -force*2000;
  119. vel = mass * vel.getNormalized();
  120. }
  121. else {
  122. force = 0 * force.getNormalized();
  123. vel += force;
  124. vel = mass * vel.getNormalized();
  125. }
  126. }
  127. }
  128. //Update particle pos
  129. age += deltaT* .5;
  130. pos += (vel * deltaT);
  131. // *** COLLISION *** COLLISION *** COLLISION *** COLLISION *** COLLISION *** COLLISION
  132. //if (position.x <= 0 || position.x >= ofGetWidth()) {
  133. //vel.x = -vel.x;
  134. //position += (vel * deltaT);
  135. //}
  136. //else if (position.y <= 0 || position.y >= ofGetHeight()) {
  137. //vel.y = -vel.y;
  138. //position += (vel * deltaT);
  139. //}
  140. //else {
  141. //position += (vel * deltaT);
  142. //}
  143. }
  144. //--------------------------------------------------------------------------------------
  145. void Particle::draw() {
  146. color.set(getAgeNorm() * 241,241/ getAgeNorm() ,219);
  147. ofSetColor(color, (1 - getAgeNorm()) * 255);
  148. if (mode == PARTICLE_MODE_BRUNIG) {
  149. tex.setColor(color);
  150. tex.draw(pos.x, pos.y, size);
  151. }
  152. else {
  153. ofDrawCircle(pos.x, pos.y, size);
  154. }
  155. }
  156. //------------------------------------------------------------------
  157. void Particle::setMode(particleMode newMode) {
  158. mode = newMode;
  159. }
  160. //--------------------------------------------------------------------------------------
  161. float Particle::getAgeNorm() {
  162. return age / maxLife;
  163. }
  164. //--------------------------------------------------------------------------------------
  165. void Particle::setVel(float min, float max) {
  166. vel.x = ofRandom(min,max);
  167. vel.y = ofRandom(min,max);
  168. }
  169. float Particle::getMaxLife() {
  170. return 1;
  171. }
  172. float Particle::getY() {
  173. return pos.y;
  174. }
  175. float Particle::getX() {
  176. return pos.x;
  177. }