// Copyright (c) Craftwork Games. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
using Microsoft.Xna.Framework;
using MonoGame.Extended.Particles.Data;
namespace MonoGame.Extended.Particles.Modifiers;
///
/// A modifier that applies a constant directional force to particles, simulating gravity or wind.
///
///
/// The applies a uniform acceleration in a specified direction
/// to all particles, creating effects such as gravity, wind, or other constant forces. The force
/// is applied proportionally to each particle's mass, simulating realistic physical behavior.
///
/// Note that this modifier only changes particle velocities; the actual position changes
/// occur during the standard particle update cycle.
///
public class LinearGravityModifier : Modifier
{
///
/// Gets or sets the direction vector of the gravitational force.
///
///
/// This vector defines both the direction and the relative magnitude of the force.
///
public Vector2 Direction;
///
/// Gets or sets the strength of the gravitational force, in units per second squared.
///
///
/// This value scales the overall magnitude of the force. Higher values create
/// stronger acceleration effects, causing particles to change velocity more rapidly.
///
public float Strength;
///
/// Updates all particles by applying a linear gravitational force.
///
///
protected internal override unsafe void Update(float elapsedSeconds, ParticleIterator iterator, int particleCount)
{
if (!Enabled) { return; }
Vector2 vector = Direction * (Strength * elapsedSeconds);
for (int i = 0; i < particleCount && iterator.HasNext; i++)
{
Particle* particle = iterator.Next();
particle->Velocity[0] = particle->Velocity[0] + vector.X * particle->Mass;
particle->Velocity[1] = particle->Velocity[1] + vector.Y * particle->Mass;
}
}
}