DragModifier.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 MonoGame.Extended.Particles.Data;
  5. namespace MonoGame.Extended.Particles.Modifiers;
  6. /// <summary>
  7. /// A modifier that applies fluid resistance (drag) to particles, gradually reducing their velocity.
  8. /// </summary>
  9. /// <remarks>
  10. /// The <see cref="DragModifier"/> simulates the effect of particles moving through a fluid medium
  11. /// such as air, water, or another substance. This creates a damping effect that slows particles over time,
  12. /// with the slowdown proportional to their velocity, mass, and the properties of the medium.
  13. /// </remarks>
  14. public class DragModifier : Modifier
  15. {
  16. /// <summary>
  17. /// Gets or sets the drag coefficient, representing the aerodynamic or hydrodynamic properties of particles.
  18. /// </summary>
  19. /// <remarks>
  20. /// The drag coefficient is a dimensionless quantity used in fluid dynamics to model
  21. /// the resistance of an object moving through a fluid. Higher values create stronger
  22. /// drag effects, causing particles to slow down more quickly.
  23. ///
  24. /// For reference to approximate real-world drag cooeficients, see
  25. /// <see href="https://en.wikipedia.org/wiki/Drag_coefficient"/>.
  26. ///
  27. /// The default value is 0.47.
  28. /// </remarks>
  29. public float DragCoefficient { get; set; } = 0.47f;
  30. /// <summary>
  31. /// Gets or sets the density of the fluid medium, affecting the strength of the drag force.
  32. /// </summary>
  33. /// <remarks>
  34. /// This value represents the density of the fluid through which particles are moving.
  35. /// Higher values create stronger drag effects, simulating denser media like water or oil.
  36. ///
  37. /// For reference to approximate real-world density values for various fluids, see
  38. /// <see href="https://en.wikipedia.org/wiki/Density#Various_materials"/>.
  39. ///
  40. /// The default value is 0.5.
  41. /// </remarks>
  42. public float Density { get; set; } = .5f;
  43. /// <summary>
  44. /// Updates all particles by applying drag forces based on their velocity.
  45. /// </summary>
  46. /// <inheritdoc/>
  47. protected internal override unsafe void Update(float elapsedSeconds, ParticleIterator iterator, int particleCount)
  48. {
  49. if (!Enabled) { return; }
  50. for (int i = 0; i < particleCount && iterator.HasNext; i++)
  51. {
  52. Particle* particle = iterator.Next();
  53. var drag = -DragCoefficient * Density * particle->Mass * elapsedSeconds;
  54. particle->Velocity[0] = particle->Velocity[0] + particle->Velocity[0] * drag;
  55. particle->Velocity[1] = particle->Velocity[1] + particle->Velocity[1] * drag;
  56. }
  57. }
  58. }