public ParticleWorld(int maxContacts, int iterations = 0)
        {
            this.registry          = new ParticleForceRegistry();
            this.particles         = new List <Particlef>();
            this.contactGenerators = new List <IParticleContactGenerator>();
            this.resolver          = new ParticleContactResolver(iterations);
            this.maxContacts       = maxContacts;

            contacts = new ParticleContact[maxContacts];
            for (int i = 0; i < maxContacts; ++i)
            {
                contacts[i] = new ParticleContact();
            }

            calculateIterations = (iterations == 0);
        }
Example #2
0
        public int addContact(ParticleContact[] contacts, int current, int limit)
        {
            int             count   = 0;
            ParticleContact contact = contacts[current];

            foreach (Particlef p1 in particles)
            {
                foreach (Particlef p2 in particles)
                {
                    if (p2 != p1)
                    {
                        Vec3f particle1Pos = p1.Position;
                        Vec3f particle2Pos = p2.Position;
                        float rad1         = p1.Radius;
                        float rad2         = p2.Radius;

                        Vec3f contactTrace = particle1Pos - particle2Pos;
                        float distance     = contactTrace.len();

                        if (distance < (rad1 + rad2))
                        {
                            contact.contactNormal = contactTrace.normal();
                            contact.particle1     = p1;
                            contact.particle2     = p2;
                            contact.penetration   = (rad1 + rad2) - distance;
                            contact.restitution   = 1.0f;
                            current++;
                            contact = contacts[current];
                            count++;
                        }

                        if (count >= limit)
                        {
                            return(count);
                        }
                    }
                }
            }
            return(count);
        }
        public int addContact(ParticleContact[] contacts, int current, int limit)
        {
            int             count   = 0;
            ParticleContact contact = contacts[current];

            foreach (Particlef p in _particles)
            {
                float y = p.Position.y;
                float x = p.Position.x;
                float r = p.Radius * 2.0f;
                if (y + r > _h)
                {
                    contact.contactNormal = new Vec3f(0.0f, -1.0f, 0.0f);
                    contact.particle1     = p;
                    contact.particle2     = null;
                    contact.penetration   = y - _h;
                    contact.restitution   = 0.4f;
                    current++;
                    contact = contacts[current];
                    count++;
                }

                if (y < 0.0f)
                {
                    contact.contactNormal = new Vec3f(0.0f, 1.0f, 0.0f);
                    contact.particle1     = p;
                    contact.particle2     = null;
                    contact.penetration   = -y - r;
                    contact.restitution   = 0.4f;
                    current++;
                    contact = contacts[current];
                    count++;
                }

                if (x + r > _w)
                {
                    contact.contactNormal = new Vec3f(-1.0f, 0.0f, 0.0f);
                    contact.particle1     = p;
                    contact.particle2     = null;
                    contact.penetration   = x - _w;
                    contact.restitution   = 0.7f;
                    current++;
                    contact = contacts[current];
                    count++;
                }

                if (x < 0.0f)
                {
                    contact.contactNormal = new Vec3f(1.0f, 0.0f, 0.0f);
                    contact.particle1     = p;
                    contact.particle2     = null;
                    contact.penetration   = -x - r;
                    contact.restitution   = 0.4f;
                    current++;
                    contact = contacts[current];
                    count++;
                }

                if (count >= limit)
                {
                    return(count);
                }
            }
            return(count);
        }