VortexModifier.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Microsoft.Xna.Framework;
  6. using MonoGame.Extended.Particles.Data;
  7. namespace MonoGame.Extended.Particles.Modifiers;
  8. /// <summary>
  9. /// A modifier that creates a gravitational vortex effect, pulling particles toward a central point.
  10. /// </summary>
  11. /// <remarks>
  12. /// The <see cref="VortexModifier"/> simulates a gravitational attraction between a central point and
  13. /// each particle. The strength of the attraction is based on gravitational principles, where the force
  14. /// is proportional to the masses and inversely proportional to the square of the distance.
  15. ///
  16. /// The actual acceleration applied to each particle is clamped to prevent particles from moving
  17. /// too quickly when they get close to the center of the vortex.
  18. /// </remarks>
  19. public unsafe class VortexModifier : Modifier
  20. {
  21. private const float GRAVITY = 100000f;
  22. /// <summary>
  23. /// Gets or sets the center position of the vortex in 2D space.
  24. /// </summary>
  25. /// <remarks>
  26. /// Particles will be attracted toward this point.
  27. /// </remarks>
  28. public Vector2 Position;
  29. /// <summary>
  30. /// Gets or sets the mass of the vortex center.
  31. /// </summary>
  32. /// <remarks>
  33. /// Higher mass values create stronger attraction forces.
  34. /// </remarks>
  35. public float Mass;
  36. /// <summary>
  37. /// Gets or sets the maximum speed that the vortex can impart on particles.
  38. /// </summary>
  39. /// <remarks>
  40. /// This value limits how quickly particles can be accelerated by the vortex,
  41. /// preventing extreme velocities when particles get very close to the center.
  42. /// </remarks>
  43. public float MaxSpeed;
  44. /// <summary>
  45. /// Updates all particles by applying a gravitational force towards the vortex center.
  46. /// </summary>
  47. /// <inheritdoc/>
  48. public override unsafe void Update(float elapsedSeconds, ParticleIterator iterator)
  49. {
  50. if (!Enabled) { return; }
  51. while (iterator.HasNext)
  52. {
  53. Particle* particle = iterator.Next();
  54. float distx = Position.X - particle->Position[0];
  55. float disty = Position.Y - particle->Position[1];
  56. float distance2 = (distx * distx) + (disty * disty);
  57. float distance = (float)Math.Sqrt(distance2);
  58. float m = (GRAVITY * Mass * particle->Mass) / distance2;
  59. m = Math.Max(Math.Min(m, MaxSpeed), -MaxSpeed) * elapsedSeconds;
  60. distx = (distx / distance) * m;
  61. disty = (disty / distance) * m;
  62. particle->Velocity[0] += distx;
  63. particle->Velocity[1] += disty;
  64. }
  65. }
  66. }