Home Reference Source

src/initializer/Velocity/Velocity.js

  1. import { MEASURE, PI } from '../../constants';
  2. import { MathUtils, Vector3D } from '../../math';
  3.  
  4. import Initializer from '../Initializer';
  5.  
  6. /**
  7. * Abstract class for Velocity initializers.
  8. *
  9. */
  10. export default class Velocity extends Initializer {
  11. /**
  12. * Constructs a Velocity intitializer instance.
  13. *
  14. * @return void
  15. */
  16. constructor(type, isEnabled = true) {
  17. super(type, isEnabled);
  18.  
  19. /**
  20. * @desc Directional vector
  21. * @type {Vector3D}
  22. */
  23. this.dirVec = new Vector3D(0, 0, 0);
  24. }
  25.  
  26. normalize(vr) {
  27. return vr * MEASURE;
  28. }
  29. }
  30.  
  31. /**
  32. * Sets the particle's initial velocity.
  33. *
  34. * @singleton
  35. * @param {Particle} particle - the particle to initialize the property on
  36. * @return void
  37. */
  38. Velocity.prototype.initialize = (function() {
  39. var tha;
  40. var normal = new Vector3D(0, 0, 1);
  41. var v = new Vector3D(0, 0, 0);
  42.  
  43. return function initialize(particle) {
  44. tha = this.tha * Math.random();
  45. this._useV && this.dirVec.copy(this.dir).scalar(this.radiusPan.getValue());
  46.  
  47. MathUtils.getNormal(this.dirVec, normal);
  48. v.copy(this.dirVec).applyAxisAngle(normal, tha);
  49. v.applyAxisAngle(this.dirVec.normalize(), Math.random() * PI * 2);
  50.  
  51. particle.velocity.copy(v);
  52.  
  53. return this;
  54. };
  55. })();