qparticle.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <qmath.h>
  2. #include "qparticle.h"
  3. float Q_rsqrt( float number )
  4. {
  5. long i;
  6. float x2, y;
  7. const float threehalfs = 1.5F;
  8. x2 = number * 0.5F;
  9. y = number;
  10. i = * ( long * ) &y; // evil floating point bit level hacking
  11. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  12. y = * ( float * ) &i;
  13. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  14. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  15. return y;
  16. }
  17. QParticle::QParticle(const QVector2D & position, qreal aMass) :
  18. Force(0.0, 0.0), Position(position)
  19. {
  20. setMass(aMass);
  21. }
  22. QParticle::QParticle(qreal aMass) :
  23. Force(0.0, 0.0), Position(0.0, 0.0)
  24. {
  25. setMass(aMass);
  26. }
  27. void QParticle::setMass(qreal massInKg)
  28. {
  29. if (massInKg == 0)
  30. {
  31. InvertMass = 0;
  32. }
  33. else
  34. {
  35. InvertMass = 1.0f / massInKg;
  36. }
  37. }
  38. bool QParticle::zeroVelocity(bool dontNull)
  39. {
  40. if(Velocity.isNull())
  41. {
  42. return true;
  43. }
  44. if((qAbs(Velocity.x()) < KZEROVELOCITY) &&
  45. (qAbs(Velocity.y()) < KZEROVELOCITY))
  46. {
  47. if(!dontNull)
  48. {
  49. Velocity.setX(0.0);
  50. Velocity.setY(0.0);
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. float QParticle::FastVelocityLength() const
  57. {
  58. return qSqrt((Velocity.x()*Velocity.x()) + (Velocity.y()*Velocity.y()));
  59. }