CircleEmitter.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Cartesian3 from '../Core/Cartesian3.js';
  2. import Check from '../Core/Check.js';
  3. import defaultValue from '../Core/defaultValue.js';
  4. import defineProperties from '../Core/defineProperties.js';
  5. import CesiumMath from '../Core/Math.js';
  6. /**
  7. * A ParticleEmitter that emits particles from a circle.
  8. * Particles will be positioned within a circle and have initial velocities going along the z vector.
  9. *
  10. * @alias CircleEmitter
  11. * @constructor
  12. *
  13. * @param {Number} [radius=1.0] The radius of the circle in meters.
  14. */
  15. function CircleEmitter(radius) {
  16. radius = defaultValue(radius, 1.0);
  17. //>>includeStart('debug', pragmas.debug);
  18. Check.typeOf.number.greaterThan('radius', radius, 0.0);
  19. //>>includeEnd('debug');
  20. this._radius = defaultValue(radius, 1.0);
  21. }
  22. defineProperties(CircleEmitter.prototype, {
  23. /**
  24. * The radius of the circle in meters.
  25. * @memberof CircleEmitter.prototype
  26. * @type {Number}
  27. * @default 1.0
  28. */
  29. radius : {
  30. get : function() {
  31. return this._radius;
  32. },
  33. set : function(value) {
  34. //>>includeStart('debug', pragmas.debug);
  35. Check.typeOf.number.greaterThan('value', value, 0.0);
  36. //>>includeEnd('debug');
  37. this._radius = value;
  38. }
  39. }
  40. });
  41. /**
  42. * Initializes the given {@link Particle} by setting it's position and velocity.
  43. *
  44. * @private
  45. * @param {Particle} particle The particle to initialize.
  46. */
  47. CircleEmitter.prototype.emit = function(particle) {
  48. var theta = CesiumMath.randomBetween(0.0, CesiumMath.TWO_PI);
  49. var rad = CesiumMath.randomBetween(0.0, this._radius);
  50. var x = rad * Math.cos(theta);
  51. var y = rad * Math.sin(theta);
  52. var z = 0.0;
  53. particle.position = Cartesian3.fromElements(x, y, z, particle.position);
  54. particle.velocity = Cartesian3.clone(Cartesian3.UNIT_Z, particle.velocity);
  55. };
  56. export default CircleEmitter;