summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Particles/Modifiers/Containers/RectangleContainerModifier.cs
blob: a56e0721b242200ef8bbcfe6f9a36a4e411e1fcf (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
53
54
55
56
57
58
59
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Particles.Modifiers.Containers
{
    public sealed class RectangleContainerModifier : Modifier
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public float RestitutionCoefficient { get; set; } = 1;

        public override unsafe void Update(float elapsedSeconds, ParticleBuffer.ParticleIterator iterator)
        {
            while (iterator.HasNext)
            {
                var particle = iterator.Next();

                var left = particle->TriggerPos.X + Width*-0.5f;
                var right = particle->TriggerPos.X + Width*0.5f;
                var top = particle->TriggerPos.Y + Height*-0.5f;
                var bottom = particle->TriggerPos.Y + Height*0.5f;

                var xPos = particle->Position.X;
                var xVel = particle->Velocity.X;
                var yPos = particle->Position.Y;
                var yVel = particle->Velocity.Y;

                if ((int) particle->Position.X < left)
                {
                    xPos = left + (left - xPos);
                    xVel = -xVel*RestitutionCoefficient;
                }
                else
                {
                    if (particle->Position.X > right)
                    {
                        xPos = right - (xPos - right);
                        xVel = -xVel*RestitutionCoefficient;
                    }
                }

                if (particle->Position.Y < top)
                {
                    yPos = top + (top - yPos);
                    yVel = -yVel*RestitutionCoefficient;
                }
                else
                {
                    if ((int) particle->Position.Y > bottom)
                    {
                        yPos = bottom - (yPos - bottom);
                        yVel = -yVel*RestitutionCoefficient;
                    }
                }
                particle->Position = new Vector2(xPos, yPos);
                particle->Velocity = new Vector2(xVel, yVel);
            }
        }
    }
}