summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Particles/Modifiers/VelocityModifier.cs
blob: f089abcc965bfe3913671a5c290f63abb729720d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using MonoGame.Extended.Particles.Modifiers.Interpolators;

namespace MonoGame.Extended.Particles.Modifiers
{
    public class VelocityModifier : Modifier
    {
        public List<Interpolator> Interpolators { get; set; } = new List<Interpolator>();

        public float VelocityThreshold { get; set; }

        public override unsafe void Update(float elapsedSeconds, ParticleBuffer.ParticleIterator iterator)
        {
            var velocityThreshold2 = VelocityThreshold*VelocityThreshold;
            var n = Interpolators.Count;

            while (iterator.HasNext)
            {
                var particle = iterator.Next();
                var velocity2 = particle->Velocity.LengthSquared();

                if (velocity2 >= velocityThreshold2)
                {
                    for (var i = 0; i < n; i++)
                    {
                        var interpolator = Interpolators[i];
                        interpolator.Update(1, particle);
                    }
                }
                else
                {
                    var t = (float) Math.Sqrt(velocity2)/VelocityThreshold;
                    for (var i = 0; i < n; i++)
                    {
                        var interpolator = Interpolators[i];
                        interpolator.Update(t, particle);
                    }
                }
            }
        }
    }
}