Example #1
0
        private void DoubleDensityRelaxation()
        {
            float q;

            for (int i = 0; i < Particles.Count; i++)
            {
                Particle particle = Particles[i];
                particle.Density     = 0;
                particle.NearDensity = 0;

                _tempParticles = Particles.GetNearby(particle);

                int len2 = _tempParticles.Count;
                if (len2 > MaxNeighbors)
                {
                    len2 = MaxNeighbors;
                }

                for (int j = 0; j < len2; j++)
                {
                    Particle tempParticle = _tempParticles[j];

                    Vector2.DistanceSquared(ref particle.Position, ref tempParticle.Position, out q);
                    if (q < InfluenceRadiusSquared && q != 0)
                    {
                        q  = (float)Math.Sqrt(q);
                        q /= InfluenceRadius;
                        float qq = ((1 - q) * (1 - q));
                        particle.Density     += qq;
                        particle.NearDensity += qq * (1 - q);
                    }
                }

                particle.Pressure     = (Stiffness * (particle.Density - DensityRest));
                particle.NearPressure = (StiffnessNear * particle.NearDensity);
                _dx = Vector2.Zero;

                for (int j = 0; j < len2; j++)
                {
                    Particle tempParticle = _tempParticles[j];

                    Vector2.DistanceSquared(ref particle.Position, ref tempParticle.Position, out q);
                    if ((q < InfluenceRadiusSquared) && (q != 0))
                    {
                        q = (float)Math.Sqrt(q);
                        Vector2.Subtract(ref tempParticle.Position, ref particle.Position, out _rij);
                        Vector2.Divide(ref _rij, q, out _rij);
                        q /= InfluenceRadius;

                        float D = (_deltaTime2 * (particle.Pressure * (1 - q) + particle.NearPressure * (1 - q) * (1 - q)));
                        Vector2.Multiply(ref _rij, (D * 0.5f), out _rij);
                        tempParticle.Position = new Vector2(tempParticle.Position.X + _rij.X, tempParticle.Position.Y + _rij.Y);
                        Vector2.Subtract(ref _dx, ref _rij, out _dx);
                    }
                }
                particle.Position = particle.Position + _dx;
            }
        }
Example #2
0
        private void ApplyViscosity(float deltaTime)
        {
            float u, q;

            for (int i = 0; i < Particles.Count; i++)
            {
                Particle particle = Particles[i];

                _tempParticles = Particles.GetNearby(particle);

                int len2 = _tempParticles.Count;
                if (len2 > MaxNeighbors)
                {
                    len2 = MaxNeighbors;
                }

                for (int j = 0; j < len2; j++)
                {
                    Particle tempParticle = _tempParticles[j];

                    Vector2.DistanceSquared(ref particle.Position, ref tempParticle.Position, out q);
                    if ((q < InfluenceRadiusSquared) && (q != 0))
                    {
                        q = (float)Math.Sqrt(q);
                        Vector2.Subtract(ref tempParticle.Position, ref particle.Position, out _rij);
                        Vector2.Divide(ref _rij, q, out _rij);

                        Vector2.Subtract(ref particle.Velocity, ref tempParticle.Velocity, out _tempVect);
                        Vector2.Dot(ref _tempVect, ref _rij, out u);
                        if (u <= 0.0f)
                        {
                            continue;
                        }

                        q /= InfluenceRadius;

                        float I = (deltaTime * (1 - q) * (ViscositySigma * u + ViscosityBeta * u * u));
                        Vector2.Multiply(ref _rij, (I * 0.5f), out _rij);
                        Vector2.Subtract(ref particle.Velocity, ref _rij, out _tempVect);
                        particle.Velocity     = _tempVect;
                        _tempVect             = tempParticle.Velocity;
                        _tempVect            += _rij;
                        tempParticle.Velocity = _tempVect;
                    }
                }
            }
        }
Example #3
0
        public static Vector2 Divide(Vector2 vector, float scale)
        {
            var result = MonoGameVector2.Divide(vector.MonoGameVector, scale);

            return(new Vector2(result));
        }