Open Source Tomb Raider Engine
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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. * \file src/Particle.cpp
  3. * \brief Particle system base implementation
  4. *
  5. * \author Mongoose
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <Particle.h>
  10. #ifdef DEBUG_MEMORY
  11. #include <memory_test.h>
  12. #endif
  13. Particle::Particle()
  14. {
  15. setActive(true);
  16. TextureId(0);
  17. Speed(2000.0, 2000.0, 2000.0);
  18. Color(1.0, 1.0, 1.0);
  19. Force(0.0, 0.8, 0.0);
  20. Reset();
  21. }
  22. Particle::~Particle()
  23. {
  24. }
  25. void Particle::TextureId(int id)
  26. {
  27. _texture = id;
  28. }
  29. void Particle::setActive(bool active)
  30. {
  31. _active = active;
  32. }
  33. void Particle::Speed(float x, float y, float z)
  34. {
  35. _speed[0] = x;
  36. _speed[1] = y;
  37. _speed[2] = z;
  38. }
  39. void Particle::Color(float r, float g, float b)
  40. {
  41. _color[0] = r;
  42. _color[1] = g;
  43. _color[2] = b;
  44. }
  45. void Particle::Force(float x, float y, float z)
  46. {
  47. _force[0] = x;
  48. _force[1] = y;
  49. _force[2] = z;
  50. }
  51. void Particle::Reset()
  52. {
  53. // Mongoose 2002.01.01, Ah, how old is that code?
  54. #ifdef OBSOLOETE
  55. _active = true;
  56. _life = 1.0;
  57. _blend = (float)(rand() % 100) / 1000.0 + 0.003;
  58. _pos[0] = _pos[1] = _pos[2] = 0.0;
  59. _dir[0] = (float)((rand() % 50) - 26.0) * 10.0;
  60. _dir[1] = (float)((rand() % 50) - 25.0) * 10.0;
  61. _dir[2] = _dir[1];
  62. _force[0] = 0.0;
  63. _force[1] = -0.8;
  64. _force[2] = 0.0;
  65. #else
  66. //! \fixme _blend prob should have nonstatic range
  67. _blend = (float)(0.003 + (0.1 * rand() / (RAND_MAX + 1.0))); // high order
  68. //_blend = (float)(rand() % 100) / 1000.0 + 0.003;
  69. //! \fixme Reset these using some nonstatic functions and values later
  70. _life = 1.0;
  71. _pos[0] = _pos[1] = _pos[2] = 0.0;
  72. _dir[0] = (float)(-26.0 + (50.0 * rand() / (RAND_MAX + 1.0))); // high order
  73. _dir[0] *= 10.0;
  74. //_dir[0] = (float)((rand() % 50) - 26.0) * 10.0;
  75. _dir[1] = (float)(-25.0 + (50.0 * rand() / (RAND_MAX + 1.0))); // high order
  76. _dir[1] *= 10.0;
  77. //_dir[1] = (float)((rand() % 50) - 25.0) * 10.0;
  78. _dir[2] = _dir[1];
  79. #endif
  80. }
  81. void Particle::Pos(float *x, float *y, float *z)
  82. {
  83. *x = _pos[0];
  84. *y = _pos[1];
  85. *z = _pos[2];
  86. }
  87. void Particle::Color(float *r, float *g, float *b)
  88. {
  89. *r = _color[0];
  90. *g = _color[1];
  91. *b = _color[2];
  92. }
  93. float Particle::Life()
  94. {
  95. return _life;
  96. }
  97. void Particle::Update()
  98. {
  99. // Adjust particle position
  100. _pos[0] += _dir[0] / _speed[0];
  101. _pos[1] += _dir[1] / _speed[1];
  102. _pos[2] += _dir[2] / _speed[2];
  103. // Adjust particle direction
  104. _dir[0] += _force[0];
  105. _dir[1] += _force[1];
  106. _dir[2] += _force[2];
  107. // Adjust particle blending/life
  108. _life -= _blend;
  109. // Reset 'dead' OR fully blended particles
  110. if (_life < 0.0)
  111. {
  112. Reset();
  113. }
  114. }
  115. int Particle::Texture()
  116. {
  117. return _texture;
  118. }
  119. bool Particle::isActive()
  120. {
  121. return _active;
  122. }