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

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