ParticleSystem.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /**
  2. * Copyright (c) 2006-2016 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. //LOVE
  21. #include "common/config.h"
  22. #include "ParticleSystem.h"
  23. #include "common/math.h"
  24. #include "modules/math/RandomGenerator.h"
  25. // STD
  26. #include <algorithm>
  27. #include <cmath>
  28. #include <cstdlib>
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. namespace
  34. {
  35. love::math::RandomGenerator rng;
  36. float calculate_variation(float inner, float outer, float var)
  37. {
  38. float low = inner - (outer/2.0f)*var;
  39. float high = inner + (outer/2.0f)*var;
  40. float r = (float) rng.random();
  41. return low*(1-r)+high*r;
  42. }
  43. } // anonymous namespace
  44. ParticleSystem::ParticleSystem(Texture *texture, uint32 size)
  45. : pMem(nullptr)
  46. , pFree(nullptr)
  47. , pHead(nullptr)
  48. , pTail(nullptr)
  49. , texture(texture)
  50. , active(true)
  51. , insertMode(INSERT_MODE_TOP)
  52. , maxParticles(0)
  53. , activeParticles(0)
  54. , emissionRate(0)
  55. , emitCounter(0)
  56. , areaSpreadDistribution(DISTRIBUTION_NONE)
  57. , lifetime(-1)
  58. , life(0)
  59. , particleLifeMin(0)
  60. , particleLifeMax(0)
  61. , direction(0)
  62. , spread(0)
  63. , speedMin(0)
  64. , speedMax(0)
  65. , linearAccelerationMin(0, 0)
  66. , linearAccelerationMax(0, 0)
  67. , radialAccelerationMin(0)
  68. , radialAccelerationMax(0)
  69. , tangentialAccelerationMin(0)
  70. , tangentialAccelerationMax(0)
  71. , linearDampingMin(0.0f)
  72. , linearDampingMax(0.0f)
  73. , sizeVariation(0)
  74. , rotationMin(0)
  75. , rotationMax(0)
  76. , spinStart(0)
  77. , spinEnd(0)
  78. , spinVariation(0)
  79. , offset(float(texture->getWidth())*0.5f, float(texture->getHeight())*0.5f)
  80. , defaultOffset(true)
  81. , relativeRotation(false)
  82. {
  83. if (size == 0 || size > MAX_PARTICLES)
  84. throw love::Exception("Invalid ParticleSystem size.");
  85. sizes.push_back(1.0f);
  86. colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
  87. setBufferSize(size);
  88. }
  89. ParticleSystem::ParticleSystem(const ParticleSystem &p)
  90. : pMem(nullptr)
  91. , pFree(nullptr)
  92. , pHead(nullptr)
  93. , pTail(nullptr)
  94. , texture(p.texture)
  95. , active(p.active)
  96. , insertMode(p.insertMode)
  97. , maxParticles(p.maxParticles)
  98. , activeParticles(0)
  99. , emissionRate(p.emissionRate)
  100. , emitCounter(0.0f)
  101. , position(p.position)
  102. , prevPosition(p.prevPosition)
  103. , areaSpreadDistribution(p.areaSpreadDistribution)
  104. , areaSpread(p.areaSpread)
  105. , lifetime(p.lifetime)
  106. , life(p.lifetime) // Initialize with the maximum life time.
  107. , particleLifeMin(p.particleLifeMin)
  108. , particleLifeMax(p.particleLifeMax)
  109. , direction(p.direction)
  110. , spread(p.spread)
  111. , speedMin(p.speedMin)
  112. , speedMax(p.speedMax)
  113. , linearAccelerationMin(p.linearAccelerationMin)
  114. , linearAccelerationMax(p.linearAccelerationMax)
  115. , radialAccelerationMin(p.radialAccelerationMin)
  116. , radialAccelerationMax(p.radialAccelerationMax)
  117. , tangentialAccelerationMin(p.tangentialAccelerationMin)
  118. , tangentialAccelerationMax(p.tangentialAccelerationMax)
  119. , linearDampingMin(p.linearDampingMin)
  120. , linearDampingMax(p.linearDampingMax)
  121. , sizes(p.sizes)
  122. , sizeVariation(p.sizeVariation)
  123. , rotationMin(p.rotationMin)
  124. , rotationMax(p.rotationMax)
  125. , spinStart(p.spinStart)
  126. , spinEnd(p.spinEnd)
  127. , spinVariation(p.spinVariation)
  128. , offset(p.offset)
  129. , defaultOffset(p.defaultOffset)
  130. , colors(p.colors)
  131. , quads(p.quads)
  132. , relativeRotation(p.relativeRotation)
  133. {
  134. setBufferSize(maxParticles);
  135. }
  136. ParticleSystem::~ParticleSystem()
  137. {
  138. deleteBuffers();
  139. }
  140. void ParticleSystem::resetOffset()
  141. {
  142. if (quads.empty())
  143. offset = love::Vector(float(texture->getWidth())*0.5f, float(texture->getHeight())*0.5f);
  144. else
  145. {
  146. Quad::Viewport v = quads[0]->getViewport();
  147. offset = love::Vector(v.x*0.5f, v.y*0.5f);
  148. }
  149. }
  150. void ParticleSystem::createBuffers(size_t size)
  151. {
  152. try
  153. {
  154. pFree = pMem = new Particle[size];
  155. maxParticles = (uint32) size;
  156. }
  157. catch (std::bad_alloc &)
  158. {
  159. deleteBuffers();
  160. throw love::Exception("Out of memory");
  161. }
  162. }
  163. void ParticleSystem::deleteBuffers()
  164. {
  165. // Clean up for great gracefulness!
  166. delete[] pMem;
  167. pMem = nullptr;
  168. maxParticles = 0;
  169. activeParticles = 0;
  170. }
  171. void ParticleSystem::setBufferSize(uint32 size)
  172. {
  173. if (size == 0 || size > MAX_PARTICLES)
  174. throw love::Exception("Invalid buffer size");
  175. deleteBuffers();
  176. createBuffers(size);
  177. reset();
  178. }
  179. uint32 ParticleSystem::getBufferSize() const
  180. {
  181. return maxParticles;
  182. }
  183. void ParticleSystem::addParticle(float t)
  184. {
  185. if (isFull())
  186. return;
  187. // Gets a free particle and updates the allocation pointer.
  188. Particle *p = pFree++;
  189. initParticle(p, t);
  190. switch (insertMode)
  191. {
  192. default:
  193. case INSERT_MODE_TOP:
  194. insertTop(p);
  195. break;
  196. case INSERT_MODE_BOTTOM:
  197. insertBottom(p);
  198. break;
  199. case INSERT_MODE_RANDOM:
  200. insertRandom(p);
  201. break;
  202. }
  203. activeParticles++;
  204. }
  205. void ParticleSystem::initParticle(Particle *p, float t)
  206. {
  207. float min,max;
  208. // Linearly interpolate between the previous and current emitter position.
  209. love::Vector pos = prevPosition + (position - prevPosition) * t;
  210. min = particleLifeMin;
  211. max = particleLifeMax;
  212. if (min == max)
  213. p->life = min;
  214. else
  215. p->life = (float) rng.random(min, max);
  216. p->lifetime = p->life;
  217. p->position = pos;
  218. float rand_x, rand_y;
  219. switch (areaSpreadDistribution)
  220. {
  221. case DISTRIBUTION_UNIFORM:
  222. p->position.x += (float) rng.random(-areaSpread.getX(), areaSpread.getX());
  223. p->position.y += (float) rng.random(-areaSpread.getY(), areaSpread.getY());
  224. break;
  225. case DISTRIBUTION_NORMAL:
  226. p->position.x += (float) rng.randomNormal(areaSpread.getX());
  227. p->position.y += (float) rng.randomNormal(areaSpread.getY());
  228. break;
  229. case DISTRIBUTION_ELLIPSE:
  230. rand_x = (float) rng.random(-1, 1);
  231. rand_y = (float) rng.random(-1, 1);
  232. p->position.x += areaSpread.getX() * (rand_x * sqrt(1 - 0.5f*pow(rand_y, 2)));
  233. p->position.y += areaSpread.getY() * (rand_y * sqrt(1 - 0.5f*pow(rand_x, 2)));
  234. break;
  235. case DISTRIBUTION_NONE:
  236. default:
  237. break;
  238. }
  239. p->origin = pos;
  240. min = speedMin;
  241. max = speedMax;
  242. float speed = (float) rng.random(min, max);
  243. min = direction - spread/2.0f;
  244. max = direction + spread/2.0f;
  245. float dir = (float) rng.random(min, max);
  246. p->velocity = love::Vector(cosf(dir), sinf(dir)) * speed;
  247. p->linearAcceleration.x = (float) rng.random(linearAccelerationMin.x, linearAccelerationMax.x);
  248. p->linearAcceleration.y = (float) rng.random(linearAccelerationMin.y, linearAccelerationMax.y);
  249. min = radialAccelerationMin;
  250. max = radialAccelerationMax;
  251. p->radialAcceleration = (float) rng.random(min, max);
  252. min = tangentialAccelerationMin;
  253. max = tangentialAccelerationMax;
  254. p->tangentialAcceleration = (float) rng.random(min, max);
  255. min = linearDampingMin;
  256. max = linearDampingMax;
  257. p->linearDamping = (float) rng.random(min, max);
  258. p->sizeOffset = (float) rng.random(sizeVariation); // time offset for size change
  259. p->sizeIntervalSize = (1.0f - (float) rng.random(sizeVariation)) - p->sizeOffset;
  260. p->size = sizes[(size_t)(p->sizeOffset - .5f) * (sizes.size() - 1)];
  261. min = rotationMin;
  262. max = rotationMax;
  263. p->spinStart = calculate_variation(spinStart, spinEnd, spinVariation);
  264. p->spinEnd = calculate_variation(spinEnd, spinStart, spinVariation);
  265. p->rotation = (float) rng.random(min, max);
  266. p->angle = p->rotation;
  267. if (relativeRotation)
  268. p->angle += atan2f(p->velocity.y, p->velocity.x);
  269. p->color = colors[0];
  270. p->quadIndex = 0;
  271. }
  272. void ParticleSystem::insertTop(Particle *p)
  273. {
  274. if (pHead == nullptr)
  275. {
  276. pHead = p;
  277. p->prev = nullptr;
  278. }
  279. else
  280. {
  281. pTail->next = p;
  282. p->prev = pTail;
  283. }
  284. p->next = nullptr;
  285. pTail = p;
  286. }
  287. void ParticleSystem::insertBottom(Particle *p)
  288. {
  289. if (pTail == nullptr)
  290. {
  291. pTail = p;
  292. p->next = nullptr;
  293. }
  294. else
  295. {
  296. pHead->prev = p;
  297. p->next = pHead;
  298. }
  299. p->prev = nullptr;
  300. pHead = p;
  301. }
  302. void ParticleSystem::insertRandom(Particle *p)
  303. {
  304. // Nonuniform, but 64-bit is so large nobody will notice. Hopefully.
  305. uint64 pos = rng.rand() % ((int64) activeParticles + 1);
  306. // Special case where the particle gets inserted before the head.
  307. if (pos == activeParticles)
  308. {
  309. Particle *pA = pHead;
  310. if (pA)
  311. pA->prev = p;
  312. p->prev = nullptr;
  313. p->next = pA;
  314. pHead = p;
  315. return;
  316. }
  317. // Inserts the particle after the randomly selected particle.
  318. Particle *pA = pMem + pos;
  319. Particle *pB = pA->next;
  320. pA->next = p;
  321. if (pB)
  322. pB->prev = p;
  323. else
  324. pTail = p;
  325. p->prev = pA;
  326. p->next = pB;
  327. }
  328. ParticleSystem::Particle *ParticleSystem::removeParticle(Particle *p)
  329. {
  330. // The linked list is updated in this function and old pointers may be
  331. // invalidated. The returned pointer will inform the caller of the new
  332. // pointer to the next particle.
  333. Particle *pNext = nullptr;
  334. // Removes the particle from the linked list.
  335. if (p->prev)
  336. p->prev->next = p->next;
  337. else
  338. pHead = p->next;
  339. if (p->next)
  340. {
  341. p->next->prev = p->prev;
  342. pNext = p->next;
  343. }
  344. else
  345. pTail = p->prev;
  346. // The (in memory) last particle can now be moved into the free slot.
  347. // It will skip the moving if it happens to be the removed particle.
  348. pFree--;
  349. if (p != pFree)
  350. {
  351. *p = *pFree;
  352. if (pNext == pFree)
  353. pNext = p;
  354. if (p->prev)
  355. p->prev->next = p;
  356. else
  357. pHead = p;
  358. if (p->next)
  359. p->next->prev = p;
  360. else
  361. pTail = p;
  362. }
  363. activeParticles--;
  364. return pNext;
  365. }
  366. void ParticleSystem::setTexture(Texture *tex)
  367. {
  368. texture.set(tex);
  369. if (defaultOffset)
  370. resetOffset();
  371. }
  372. Texture *ParticleSystem::getTexture() const
  373. {
  374. return texture.get();
  375. }
  376. void ParticleSystem::setInsertMode(InsertMode mode)
  377. {
  378. insertMode = mode;
  379. }
  380. ParticleSystem::InsertMode ParticleSystem::getInsertMode() const
  381. {
  382. return insertMode;
  383. }
  384. void ParticleSystem::setEmissionRate(float rate)
  385. {
  386. if (rate < 0.0f)
  387. throw love::Exception("Invalid emission rate");
  388. emissionRate = rate;
  389. // Prevent an explosion when dramatically increasing the rate
  390. emitCounter = std::min(emitCounter, 1.0f/rate);
  391. }
  392. float ParticleSystem::getEmissionRate() const
  393. {
  394. return emissionRate;
  395. }
  396. void ParticleSystem::setEmitterLifetime(float life)
  397. {
  398. this->life = lifetime = life;
  399. }
  400. float ParticleSystem::getEmitterLifetime() const
  401. {
  402. return lifetime;
  403. }
  404. void ParticleSystem::setParticleLifetime(float min, float max)
  405. {
  406. particleLifeMin = min;
  407. if (max == 0)
  408. particleLifeMax = min;
  409. else
  410. particleLifeMax = max;
  411. }
  412. void ParticleSystem::getParticleLifetime(float &min, float &max) const
  413. {
  414. min = particleLifeMin;
  415. max = particleLifeMax;
  416. }
  417. void ParticleSystem::setPosition(float x, float y)
  418. {
  419. position = love::Vector(x, y);
  420. prevPosition = position;
  421. }
  422. const love::Vector &ParticleSystem::getPosition() const
  423. {
  424. return position;
  425. }
  426. void ParticleSystem::moveTo(float x, float y)
  427. {
  428. position = love::Vector(x, y);
  429. }
  430. void ParticleSystem::setAreaSpread(AreaSpreadDistribution distribution, float x, float y)
  431. {
  432. areaSpread = love::Vector(x, y);
  433. areaSpreadDistribution = distribution;
  434. }
  435. ParticleSystem::AreaSpreadDistribution ParticleSystem::getAreaSpreadDistribution() const
  436. {
  437. return areaSpreadDistribution;
  438. }
  439. const love::Vector &ParticleSystem::getAreaSpreadParameters() const
  440. {
  441. return areaSpread;
  442. }
  443. void ParticleSystem::setDirection(float direction)
  444. {
  445. this->direction = direction;
  446. }
  447. float ParticleSystem::getDirection() const
  448. {
  449. return direction;
  450. }
  451. void ParticleSystem::setSpread(float spread)
  452. {
  453. this->spread = spread;
  454. }
  455. float ParticleSystem::getSpread() const
  456. {
  457. return spread;
  458. }
  459. void ParticleSystem::setSpeed(float speed)
  460. {
  461. speedMin = speedMax = speed;
  462. }
  463. void ParticleSystem::setSpeed(float min, float max)
  464. {
  465. speedMin = min;
  466. speedMax = max;
  467. }
  468. void ParticleSystem::getSpeed(float &min, float &max) const
  469. {
  470. min = speedMin;
  471. max = speedMax;
  472. }
  473. void ParticleSystem::setLinearAcceleration(float x, float y)
  474. {
  475. linearAccelerationMin.x = linearAccelerationMax.x = x;
  476. linearAccelerationMin.y = linearAccelerationMax.y = y;
  477. }
  478. void ParticleSystem::setLinearAcceleration(float xmin, float ymin, float xmax, float ymax)
  479. {
  480. linearAccelerationMin = love::Vector(xmin, ymin);
  481. linearAccelerationMax = love::Vector(xmax, ymax);
  482. }
  483. void ParticleSystem::getLinearAcceleration(love::Vector &min, love::Vector &max) const
  484. {
  485. min = linearAccelerationMin;
  486. max = linearAccelerationMax;
  487. }
  488. void ParticleSystem::setRadialAcceleration(float acceleration)
  489. {
  490. radialAccelerationMin = radialAccelerationMax = acceleration;
  491. }
  492. void ParticleSystem::setRadialAcceleration(float min, float max)
  493. {
  494. radialAccelerationMin = min;
  495. radialAccelerationMax = max;
  496. }
  497. void ParticleSystem::getRadialAcceleration(float &min, float &max) const
  498. {
  499. min = radialAccelerationMin;
  500. max = radialAccelerationMax;
  501. }
  502. void ParticleSystem::setTangentialAcceleration(float acceleration)
  503. {
  504. tangentialAccelerationMin = tangentialAccelerationMax = acceleration;
  505. }
  506. void ParticleSystem::setTangentialAcceleration(float min, float max)
  507. {
  508. tangentialAccelerationMin = min;
  509. tangentialAccelerationMax = max;
  510. }
  511. void ParticleSystem::getTangentialAcceleration(float &min, float &max) const
  512. {
  513. min = tangentialAccelerationMin;
  514. max = tangentialAccelerationMax;
  515. }
  516. void ParticleSystem::setLinearDamping(float min, float max)
  517. {
  518. linearDampingMin = min;
  519. linearDampingMax = max;
  520. }
  521. void ParticleSystem::getLinearDamping(float &min, float &max) const
  522. {
  523. min = linearDampingMin;
  524. max = linearDampingMax;
  525. }
  526. void ParticleSystem::setSize(float size)
  527. {
  528. sizes.resize(1);
  529. sizes[0] = size;
  530. }
  531. void ParticleSystem::setSizes(const std::vector<float> &newSizes)
  532. {
  533. sizes = newSizes;
  534. }
  535. const std::vector<float> &ParticleSystem::getSizes() const
  536. {
  537. return sizes;
  538. }
  539. void ParticleSystem::setSizeVariation(float variation)
  540. {
  541. sizeVariation = variation;
  542. }
  543. float ParticleSystem::getSizeVariation() const
  544. {
  545. return sizeVariation;
  546. }
  547. void ParticleSystem::setRotation(float rotation)
  548. {
  549. rotationMin = rotationMax = rotation;
  550. }
  551. void ParticleSystem::setRotation(float min, float max)
  552. {
  553. rotationMin = min;
  554. rotationMax = max;
  555. }
  556. void ParticleSystem::getRotation(float &min, float &max) const
  557. {
  558. min = rotationMin;
  559. max = rotationMax;
  560. }
  561. void ParticleSystem::setSpin(float spin)
  562. {
  563. spinStart = spin;
  564. spinEnd = spin;
  565. }
  566. void ParticleSystem::setSpin(float start, float end)
  567. {
  568. spinStart = start;
  569. spinEnd = end;
  570. }
  571. void ParticleSystem::getSpin(float &start, float &end) const
  572. {
  573. start = spinStart;
  574. end = spinEnd;
  575. }
  576. void ParticleSystem::setSpinVariation(float variation)
  577. {
  578. spinVariation = variation;
  579. }
  580. float ParticleSystem::getSpinVariation() const
  581. {
  582. return spinVariation;
  583. }
  584. void ParticleSystem::setOffset(float x, float y)
  585. {
  586. offset = love::Vector(x, y);
  587. defaultOffset = false;
  588. }
  589. love::Vector ParticleSystem::getOffset() const
  590. {
  591. return offset;
  592. }
  593. void ParticleSystem::setColor(const std::vector<Colorf> &newColors)
  594. {
  595. colors = newColors;
  596. for (Colorf &c : colors)
  597. {
  598. // We want to store the colors as [0, 1], rather than [0, 255].
  599. c.r /= 255.0f;
  600. c.g /= 255.0f;
  601. c.b /= 255.0f;
  602. c.a /= 255.0f;
  603. }
  604. }
  605. std::vector<Colorf> ParticleSystem::getColor() const
  606. {
  607. // The particle system stores colors in the range of [0, 1]...
  608. std::vector<Colorf> ncolors(colors);
  609. for (Colorf &c : ncolors)
  610. {
  611. c.r *= 255.0f;
  612. c.g *= 255.0f;
  613. c.b *= 255.0f;
  614. c.a *= 255.0f;
  615. }
  616. return ncolors;
  617. }
  618. void ParticleSystem::setQuads(const std::vector<Quad *> &newQuads)
  619. {
  620. std::vector<StrongRef<Quad>> quadlist;
  621. quadlist.reserve(newQuads.size());
  622. for (Quad *q : newQuads)
  623. quadlist.push_back(q);
  624. quads = quadlist;
  625. if (defaultOffset)
  626. resetOffset();
  627. }
  628. void ParticleSystem::setQuads()
  629. {
  630. quads.clear();
  631. }
  632. std::vector<Quad *> ParticleSystem::getQuads() const
  633. {
  634. std::vector<Quad *> quadlist;
  635. quadlist.reserve(quads.size());
  636. for (const StrongRef<Quad> &q : quads)
  637. quadlist.push_back(q.get());
  638. return quadlist;
  639. }
  640. void ParticleSystem::setRelativeRotation(bool enable)
  641. {
  642. relativeRotation = enable;
  643. }
  644. bool ParticleSystem::hasRelativeRotation() const
  645. {
  646. return relativeRotation;
  647. }
  648. uint32 ParticleSystem::getCount() const
  649. {
  650. return activeParticles;
  651. }
  652. void ParticleSystem::start()
  653. {
  654. active = true;
  655. }
  656. void ParticleSystem::stop()
  657. {
  658. active = false;
  659. life = lifetime;
  660. emitCounter = 0;
  661. }
  662. void ParticleSystem::pause()
  663. {
  664. active = false;
  665. }
  666. void ParticleSystem::reset()
  667. {
  668. if (pMem == nullptr)
  669. return;
  670. pFree = pMem;
  671. pHead = nullptr;
  672. pTail = nullptr;
  673. activeParticles = 0;
  674. life = lifetime;
  675. emitCounter = 0;
  676. }
  677. void ParticleSystem::emit(uint32 num)
  678. {
  679. if (!active)
  680. return;
  681. num = std::min(num, maxParticles - activeParticles);
  682. while (num--)
  683. addParticle(1.0f);
  684. }
  685. bool ParticleSystem::isActive() const
  686. {
  687. return active;
  688. }
  689. bool ParticleSystem::isPaused() const
  690. {
  691. return !active && life < lifetime;
  692. }
  693. bool ParticleSystem::isStopped() const
  694. {
  695. return !active && life >= lifetime;
  696. }
  697. bool ParticleSystem::isEmpty() const
  698. {
  699. return activeParticles == 0;
  700. }
  701. bool ParticleSystem::isFull() const
  702. {
  703. return activeParticles == maxParticles;
  704. }
  705. void ParticleSystem::update(float dt)
  706. {
  707. if (pMem == nullptr || dt == 0.0f)
  708. return;
  709. // Traverse all particles and update.
  710. Particle *p = pHead;
  711. while (p)
  712. {
  713. // Decrease lifespan.
  714. p->life -= dt;
  715. if (p->life <= 0)
  716. p = removeParticle(p);
  717. else
  718. {
  719. // Temp variables.
  720. love::Vector radial, tangential;
  721. love::Vector ppos = p->position;
  722. // Get vector from particle center to particle.
  723. radial = ppos - p->origin;
  724. radial.normalize();
  725. tangential = radial;
  726. // Resize radial acceleration.
  727. radial *= p->radialAcceleration;
  728. // Calculate tangential acceleration.
  729. {
  730. float a = tangential.getX();
  731. tangential.setX(-tangential.getY());
  732. tangential.setY(a);
  733. }
  734. // Resize tangential.
  735. tangential *= p->tangentialAcceleration;
  736. // Update velocity.
  737. p->velocity += (radial + tangential + p->linearAcceleration) * dt;
  738. // Apply damping.
  739. p->velocity *= 1.0f / (1.0f + p->linearDamping * dt);
  740. // Modify position.
  741. ppos += p->velocity * dt;
  742. p->position = ppos;
  743. const float t = 1.0f - p->life / p->lifetime;
  744. // Rotate.
  745. p->rotation += (p->spinStart * (1.0f - t) + p->spinEnd * t) * dt;
  746. p->angle = p->rotation;
  747. if (relativeRotation)
  748. p->angle += atan2f(p->velocity.y, p->velocity.x);
  749. // Change size according to given intervals:
  750. // i = 0 1 2 3 n-1
  751. // |-------|-------|------|--- ... ---|
  752. // t = 0 1/(n-1) 3/(n-1) 1
  753. //
  754. // `s' is the interpolation variable scaled to the current
  755. // interval width, e.g. if n = 5 and t = 0.3, then the current
  756. // indices are 1,2 and s = 0.3 - 0.25 = 0.05
  757. float s = p->sizeOffset + t * p->sizeIntervalSize; // size variation
  758. s *= (float)(sizes.size() - 1); // 0 <= s < sizes.size()
  759. size_t i = (size_t)s;
  760. size_t k = (i == sizes.size() - 1) ? i : i + 1; // boundary check (prevents failing on t = 1.0f)
  761. s -= (float)i; // transpose s to be in interval [0:1]: i <= s < i + 1 ~> 0 <= s < 1
  762. p->size = sizes[i] * (1.0f - s) + sizes[k] * s;
  763. // Update color according to given intervals (as above)
  764. s = t * (float)(colors.size() - 1);
  765. i = (size_t)s;
  766. k = (i == colors.size() - 1) ? i : i + 1;
  767. s -= (float)i; // 0 <= s <= 1
  768. p->color = colors[i] * (1.0f - s) + colors[k] * s;
  769. // Update the quad index.
  770. k = quads.size();
  771. if (k > 0)
  772. {
  773. s = t * (float) k; // [0:numquads-1] (clamped below)
  774. i = (s > 0.0f) ? (size_t) s : 0;
  775. p->quadIndex = (int) ((i < k) ? i : k - 1);
  776. }
  777. // Next particle.
  778. p = p->next;
  779. }
  780. }
  781. // Make some more particles.
  782. if (active)
  783. {
  784. float rate = 1.0f / emissionRate; // the amount of time between each particle emit
  785. emitCounter += dt;
  786. float total = emitCounter - rate;
  787. while (emitCounter > rate)
  788. {
  789. addParticle(1.0f - (emitCounter - rate) / total);
  790. emitCounter -= rate;
  791. }
  792. life -= dt;
  793. if (lifetime != -1 && life < 0)
  794. stop();
  795. }
  796. prevPosition = position;
  797. }
  798. bool ParticleSystem::getConstant(const char *in, AreaSpreadDistribution &out)
  799. {
  800. return distributions.find(in, out);
  801. }
  802. bool ParticleSystem::getConstant(AreaSpreadDistribution in, const char *&out)
  803. {
  804. return distributions.find(in, out);
  805. }
  806. bool ParticleSystem::getConstant(const char *in, InsertMode &out)
  807. {
  808. return insertModes.find(in, out);
  809. }
  810. bool ParticleSystem::getConstant(InsertMode in, const char *&out)
  811. {
  812. return insertModes.find(in, out);
  813. }
  814. StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM>::Entry ParticleSystem::distributionsEntries[] =
  815. {
  816. { "none", DISTRIBUTION_NONE },
  817. { "uniform", DISTRIBUTION_UNIFORM },
  818. { "normal", DISTRIBUTION_NORMAL },
  819. { "ellipse", DISTRIBUTION_ELLIPSE },
  820. };
  821. StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM> ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries));
  822. StringMap<ParticleSystem::InsertMode, ParticleSystem::INSERT_MODE_MAX_ENUM>::Entry ParticleSystem::insertModesEntries[] =
  823. {
  824. { "top", INSERT_MODE_TOP },
  825. { "bottom", INSERT_MODE_BOTTOM },
  826. { "random", INSERT_MODE_RANDOM },
  827. };
  828. StringMap<ParticleSystem::InsertMode, ParticleSystem::INSERT_MODE_MAX_ENUM> ParticleSystem::insertModes(ParticleSystem::insertModesEntries, sizeof(ParticleSystem::insertModesEntries));
  829. } // graphics
  830. } // love