示例#1
0
 public CollisionDetector()
 {
     _collisions = new List<Collision>();
     _collisionsWrapper = new ReadOnlyCollection<Collision>(_collisions);
     _geometry = new List<Geometry>();
     _tester = new SatTester();
 }
示例#2
0
文件: Collision.cs 项目: ylyking/Myre
        public void FindContacts(SatTester tester, int maxContacts)
        {
            if (!IsActive)
                return;

            // perform SAT collision detection
            SatResult? r = tester.FindIntersection(A, B);

            if (r == null)
            {
                Contacts.Clear();
                return;
            }

            SatResult result = r.Value;
            _normal = result.NormalAxis;
            PenetrationDepth = result.Penetration;

            // find all vertices in a which are inside b, add them as contacts
            var aVertices = A.GetVertices(result.NormalAxis);
            for (int i = 0; i < aVertices.Length; i++)
            {
                if (_newContacts.Count == maxContacts)
                    break;

                if (B.Contains(aVertices[i]))
                    _newContacts.Add(new Contact(aVertices[i], A, i));
            }

            // find all vertices in b which are inside a, add them as contacts
            var bVertices = B.GetVertices(-result.NormalAxis);
            for (int i = 0; i < bVertices.Length; i++)
            {
                if (Contacts.Count == maxContacts)
                    break;

                if (A.Contains(bVertices[i]))
                    _newContacts.Add(new Contact(bVertices[i], B, i));
            }

            // add the deepest point if we found no others
            if (_newContacts.Count == 0)
                _newContacts.Add(new Contact(result.DeepestPoint, A, 0));

            // merge new contacts with the old ones
            MergeContacts();

            if (!_initialised)
            {
                A.collidingWith.Add(B);
                B.collidingWith.Add(A);
                _initialised = true;
            }
        }