CParticleAttractionAffector.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CParticleAttractionAffector.h"
  5. #include "IAttributes.h"
  6. namespace irr
  7. {
  8. namespace scene
  9. {
  10. //! constructor
  11. CParticleAttractionAffector::CParticleAttractionAffector(
  12. const core::vector3df& point, f32 speed, bool attract,
  13. bool affectX, bool affectY, bool affectZ )
  14. : Point(point), Speed(speed), AffectX(affectX), AffectY(affectY),
  15. AffectZ(affectZ), Attract(attract), LastTime(0)
  16. {
  17. #ifdef _DEBUG
  18. setDebugName("CParticleAttractionAffector");
  19. #endif
  20. }
  21. //! Affects an array of particles.
  22. void CParticleAttractionAffector::affect(u32 now, SParticle* particlearray, u32 count)
  23. {
  24. if( LastTime == 0 )
  25. {
  26. LastTime = now;
  27. return;
  28. }
  29. f32 timeDelta = ( now - LastTime ) / 1000.0f;
  30. LastTime = now;
  31. if( !Enabled )
  32. return;
  33. for(u32 i=0; i<count; ++i)
  34. {
  35. core::vector3df direction = (Point - particlearray[i].pos).normalize();
  36. direction *= Speed * timeDelta;
  37. if( !Attract )
  38. direction *= -1.0f;
  39. if( AffectX )
  40. particlearray[i].pos.X += direction.X;
  41. if( AffectY )
  42. particlearray[i].pos.Y += direction.Y;
  43. if( AffectZ )
  44. particlearray[i].pos.Z += direction.Z;
  45. }
  46. }
  47. //! Writes attributes of the object.
  48. void CParticleAttractionAffector::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  49. {
  50. out->addVector3d("Point", Point);
  51. out->addFloat("Speed", Speed);
  52. out->addBool("AffectX", AffectX);
  53. out->addBool("AffectY", AffectY);
  54. out->addBool("AffectZ", AffectZ);
  55. out->addBool("Attract", Attract);
  56. }
  57. //! Reads attributes of the object.
  58. void CParticleAttractionAffector::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  59. {
  60. Point = in->getAttributeAsVector3d("Point");
  61. Speed = in->getAttributeAsFloat("Speed");
  62. AffectX = in->getAttributeAsBool("AffectX");
  63. AffectY = in->getAttributeAsBool("AffectY");
  64. AffectZ = in->getAttributeAsBool("AffectZ");
  65. Attract = in->getAttributeAsBool("Attract");
  66. }
  67. } // end namespace scene
  68. } // end namespace irr