예제 #1
0
        public override void AffectOther(Node other) //only for links (no handlers)
        {
            if (!active)
            {
                return;
            }
            Manifold m = new Manifold(parent.body, other.body);

            m.Solve();
            if (m.contact_count > 0)
            {
                room.CollisionManager.AddManifold(m);
            }
        }
예제 #2
0
        public void Step()
        {
            // Generate new collision info
            contacts.Clear();
            for (int i = 0; i < bodies.Count; ++i)
            {
                Body A = bodies[i];

                for (int j = i + 1; j < bodies.Count; ++j)
                {
                    Body B = bodies[j];

                    if (A.invMass == 0 && B.invMass == 0)
                    {
                        continue;
                    }

                    Manifold m = new Manifold(A, B);
                    m.Solve();

                    if (m.contactCount > 0)
                    {
                        contacts.Add(m);
                    }
                }
            }

            // Integrate forces
            for (int i = 0; i < bodies.Count; ++i)
            {
                IntegrateForces(bodies[i], dt);
            }

            // Initialize collision
            for (int i = 0; i < contacts.Count; ++i)
            {
                contacts[i].Initialize();
            }

            // Solve collisions
            for (int j = 0; j < iterations; ++j)
            {
                for (int i = 0; i < contacts.Count; ++i)
                {
                    contacts[i].ApplyImpulse();
                }
            }

            // Integrate velocities
            for (int i = 0; i < bodies.Count; ++i)
            {
                IntegrateVelocity(bodies[i], dt);
            }

            // Correct positions
            for (int i = 0; i < contacts.Count; ++i)
            {
                contacts[i].PositionalCorrection();
            }

            // Clear all forces
            for (int i = 0; i < bodies.Count; ++i)
            {
                Body b = bodies[i];
                b.force.Set(0, 0);
                b.torque = 0;
            }
        }
예제 #3
0
        private static void Step()
        {
            Contacts.Clear();

            for (int i = 0; i < Rigidbodies.Count; ++i)
            {
                Rigidbody.Rigidbody a = Rigidbodies[i];
                for (int j = i + 1; j < Rigidbodies.Count; ++j)
                {
                    Rigidbody.Rigidbody b = Rigidbodies[j];

                    if (Math.Abs(a.InverseMass) < Mathf.Epsilon && Math.Abs(b.InverseMass) < Mathf.Epsilon)
                    {
                        continue;
                    }

                    Manifold manifold = new Manifold(ref a, ref b);
                    manifold.Solve();

                    if (manifold.ContactCount > 0)
                    {
                        Contacts.Add(manifold);
                    }
                }
            }

            for (int i = 0; i < Rigidbodies.Count; ++i)
            {
                IntegrateForces(Rigidbodies[i], DeltaTime);
            }

            for (int i = 0; i < Contacts.Count; ++i)
            {
                Contacts[i].Initialize();
            }

            for (int j = 0; j < Iterations; ++j)
            {
                for (int i = 0; i < Contacts.Count; ++i)
                {
                    Contacts[i].ApplyImpulse();
                }
            }

            for (int i = 0; i < Rigidbodies.Count; ++i)
            {
                IntegrateVelocity(Rigidbodies[i], DeltaTime);
            }

            for (int i = 0; i < Contacts.Count; ++i)
            {
                Contacts[i].PositionalCorrection();
            }

            for (int i = 0; i < Rigidbodies.Count; ++i)
            {
                Rigidbody.Rigidbody rigidbody = Rigidbodies[i];

                rigidbody.SetForce(Vector2.zero);
                rigidbody.SetTorque(0f);
            }

            for (int i = 0; i < Contacts.Count; i++)
            {
                if (!Array.Exists(_previousFrameContacts, item => item.Equals(Contacts[i])))
                {
                    Contacts[i].A.Shape.OnCollisionEnter?.Invoke(Contacts[i].B.Shape);
                    Contacts[i].B.Shape.OnCollisionEnter?.Invoke(Contacts[i].A.Shape);
                }
            }

            _previousFrameContacts = Contacts.ToArray();
        }