summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Particles/Modifiers/Containers/CircleContainerModifier.cs
blob: 30d280203a2dd2aeffb30b55dc988a6f11b28b3e (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
44
45
46
47
48
49
50
51
52
using System;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Particles.Modifiers.Containers
{
    public class CircleContainerModifier : Modifier
    {
        public float Radius { get; set; }
        public bool Inside { get; set; } = true;
        public float RestitutionCoefficient { get; set; } = 1;

        public override unsafe void Update(float elapsedSeconds, ParticleBuffer.ParticleIterator iterator)
        {
            var radiusSq = Radius*Radius;
            while (iterator.HasNext)
            {
                var particle = iterator.Next();
                var localPos = particle->Position - particle->TriggerPos;

                var distSq = localPos.LengthSquared();
                var normal = localPos;
                normal.Normalize();

                if (Inside)
                {
                    if (distSq < radiusSq) continue;

                    SetReflected(distSq, particle, normal);
                }
                else
                {
                    if (distSq > radiusSq) continue;

                    SetReflected(distSq, particle, -normal);
                }
            }
        }

        private unsafe void SetReflected(float distSq, Particle* particle, Vector2 normal)
        {
            var dist = (float) Math.Sqrt(distSq);
            var d = dist - Radius; // how far outside the circle is the particle

            var twoRestDot = 2*RestitutionCoefficient*
                             Vector2.Dot(particle->Velocity, normal);
            particle->Velocity -= twoRestDot*normal;

            // exact computation requires sqrt or goniometrics
            particle->Position -= normal*d;
        }
    }
}