b2Particle.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2013 Google, Inc.
  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. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Particle/b2Particle.h>
  19. #include <Box2D/Common/b2Draw.h>
  20. #define B2PARTICLECOLOR_BITS_PER_COMPONENT (sizeof(uint8) << 3)
  21. // Maximum value of a b2ParticleColor component.
  22. #define B2PARTICLECOLOR_MAX_VALUE \
  23. ((1U << B2PARTICLECOLOR_BITS_PER_COMPONENT) - 1)
  24. /// Number of bits used to store each b2ParticleColor component.
  25. const uint8 b2ParticleColor::k_bitsPerComponent =
  26. B2PARTICLECOLOR_BITS_PER_COMPONENT;
  27. const float32 b2ParticleColor::k_maxValue = (float)B2PARTICLECOLOR_MAX_VALUE;
  28. const float32 b2ParticleColor::k_inverseMaxValue =
  29. 1.0f / (float)B2PARTICLECOLOR_MAX_VALUE;
  30. b2ParticleColor b2ParticleColor_zero(0, 0, 0, 0);
  31. b2ParticleColor::b2ParticleColor(const b2Color& color)
  32. {
  33. Set(color);
  34. }
  35. b2Color b2ParticleColor::GetColor() const
  36. {
  37. return b2Color(k_inverseMaxValue * r,
  38. k_inverseMaxValue * g,
  39. k_inverseMaxValue * b);
  40. }
  41. void b2ParticleColor::Set(const b2Color& color)
  42. {
  43. Set((uint8)(k_maxValue * color.r),
  44. (uint8)(k_maxValue * color.g),
  45. (uint8)(k_maxValue * color.b),
  46. B2PARTICLECOLOR_MAX_VALUE);
  47. }
  48. int32 b2CalculateParticleIterations(
  49. float32 gravity, float32 radius, float32 timeStep)
  50. {
  51. // In some situations you may want more particle iterations than this,
  52. // but to avoid excessive cycle cost, don't recommend more than this.
  53. const int32 B2_MAX_RECOMMENDED_PARTICLE_ITERATIONS = 8;
  54. const float32 B2_RADIUS_THRESHOLD = 0.01f;
  55. int32 iterations =
  56. (int32) ceilf(b2Sqrt(gravity / (B2_RADIUS_THRESHOLD * radius)) * timeStep);
  57. return b2Clamp(iterations, 1, B2_MAX_RECOMMENDED_PARTICLE_ITERATIONS);
  58. }