VelocityModifier.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Collections.Generic;
  6. using MonoGame.Extended.Particles.Data;
  7. using MonoGame.Extended.Particles.Modifiers.Interpolators;
  8. namespace MonoGame.Extended.Particles.Modifiers;
  9. /// <summary>
  10. /// A modifier that applies interpolators to particles based on their velocity magnitude.
  11. /// </summary>
  12. /// <remarks>
  13. /// The <see cref="VelocityModifier"/> controls how particle properties change based on their speed
  14. /// by applying a collection of <see cref="Interpolator"/> objects to each particle. The intensity
  15. /// of the effect is determined by comparing the particle's velocity magnitude to a threshold value.
  16. /// </remarks>
  17. public class VelocityModifier : Modifier
  18. {
  19. /// <summary>
  20. /// Gets or sets the collection of interpolators that will be applied to particles.
  21. /// </summary>
  22. public List<Interpolator> Interpolators { get; set; } = new List<Interpolator>();
  23. /// <summary>
  24. /// Gets or sets the velocity magnitude at which particles reach the maximum interpolation effect.
  25. /// </summary>
  26. /// <remarks>
  27. /// This value defines the speed threshold that determines when a particle should
  28. /// receive the full interpolation effect (amount = 1.0). Particles moving slower than this
  29. /// threshold will receive a proportionally reduced effect based on their velocity magnitude.
  30. /// </remarks>
  31. public float VelocityThreshold;
  32. /// <summary>
  33. /// Updates all particles by applying interpolators with an amount based on each particle's velocity.
  34. /// </summary>
  35. /// <inheritdoc />
  36. protected internal override unsafe void Update(float elapsedSeconds, ParticleIterator iterator, int particleCount)
  37. {
  38. if (!Enabled) { return; }
  39. float velocityThreshold2 = VelocityThreshold * VelocityThreshold;
  40. for (int i = 0; i < particleCount && iterator.HasNext; i++)
  41. {
  42. Particle* particle = iterator.Next();
  43. float velocitySquared = particle->Velocity[0] * particle->Velocity[0] +
  44. particle->Velocity[1] * particle->Velocity[1];
  45. if (velocitySquared >= velocityThreshold2)
  46. {
  47. for (int j = 0; j < Interpolators.Count; j++)
  48. {
  49. Interpolator interpolator = Interpolators[j];
  50. interpolator.Update(1, particle);
  51. }
  52. }
  53. else
  54. {
  55. float t = (float)Math.Sqrt(velocitySquared) / VelocityThreshold;
  56. for (int j = 0; j < Interpolators.Count; j++)
  57. {
  58. Interpolator interpolator = Interpolators[j];
  59. interpolator.Update(t, particle);
  60. }
  61. }
  62. }
  63. }
  64. }