Box2DX.Collision.PairManager.Commit C# (CSharp) Метод

Commit() публичный Метод

public Commit ( ) : void
Результат void
		public void Commit()
		{
			int removeCount = 0;

			Proxy[] proxies = _broadPhase._proxyPool;

			for (int i = 0; i < _pairBufferCount; ++i)
			{
				Pair pair = Find(_pairBuffer[i].ProxyId1, _pairBuffer[i].ProxyId2);
				Box2DXDebug.Assert(pair.IsBuffered());
				pair.ClearBuffered();

				Box2DXDebug.Assert(pair.ProxyId1 < Settings.MaxProxies && pair.ProxyId2 < Settings.MaxProxies);

				Proxy proxy1 = proxies[pair.ProxyId1];
				Proxy proxy2 = proxies[pair.ProxyId2];

				Box2DXDebug.Assert(proxy1.IsValid);
				Box2DXDebug.Assert(proxy2.IsValid);

				if (pair.IsRemoved())
				{
					// It is possible a pair was added then removed before a commit. Therefore,
					// we should be careful not to tell the user the pair was removed when the
					// the user didn't receive a matching add.
					if (pair.IsFinal() == true)
					{
						_callback.PairRemoved(proxy1.UserData, proxy2.UserData, pair.UserData);
					}

					// Store the ids so we can actually remove the pair below.
					_pairBuffer[removeCount].ProxyId1 = pair.ProxyId1;
					_pairBuffer[removeCount].ProxyId2 = pair.ProxyId2;
					++removeCount;
				}
				else
				{
					Box2DXDebug.Assert(_broadPhase.TestOverlap(proxy1, proxy2) == true);

					if (pair.IsFinal() == false)
					{
						pair.UserData = _callback.PairAdded(proxy1.UserData, proxy2.UserData);
						pair.SetFinal();
					}
				}
			}

			for (int i = 0; i < removeCount; ++i)
			{
				RemovePair(_pairBuffer[i].ProxyId1, _pairBuffer[i].ProxyId2);
			}

			_pairBufferCount = 0;

			if (BroadPhase.IsValidate)
			{
				ValidateTable();
			}
		}

Usage Example

Пример #1
0
        // Create and destroy proxies. These call Flush first.
        public ushort CreateProxy(AABB aabb, object userData)
        {
            Box2DXDebug.Assert(_proxyCount < Settings.MaxProxies);
            Box2DXDebug.Assert(_freeProxy != PairManager.NullProxy);

            ushort proxyId = _freeProxy;
            Proxy  proxy   = _proxyPool[proxyId];

            _freeProxy = proxy.Next;

            proxy.OverlapCount = 0;
            proxy.UserData     = userData;

            int boundCount = 2 * _proxyCount;

            ushort[] lowerValues = new ushort[2], upperValues = new ushort[2];
            ComputeBounds(out lowerValues, out upperValues, aabb);

            for (int axis = 0; axis < 2; ++axis)
            {
                Bound[] bounds = _bounds[axis];
                int     lowerIndex, upperIndex;
                Query(out lowerIndex, out upperIndex, lowerValues[axis], upperValues[axis], bounds, boundCount, axis);

#warning "Check this"
                //memmove(bounds + upperIndex + 2, bounds + upperIndex, (boundCount - upperIndex) * sizeof(b2Bound));
                Bound[] tmp = new Bound[boundCount - upperIndex];
                for (int i = 0; i < (boundCount - upperIndex); i++)
                {
                    tmp[i] = bounds[upperIndex + i].Clone();
                }
                for (int i = 0; i < (boundCount - upperIndex); i++)
                {
                    bounds[upperIndex + 2 + i] = tmp[i];
                }

                //memmove(bounds + lowerIndex + 1, bounds + lowerIndex, (upperIndex - lowerIndex) * sizeof(b2Bound));
                tmp = new Bound[upperIndex - lowerIndex];
                for (int i = 0; i < (upperIndex - lowerIndex); i++)
                {
                    tmp[i] = bounds[lowerIndex + i].Clone();
                }
                for (int i = 0; i < (upperIndex - lowerIndex); i++)
                {
                    bounds[lowerIndex + 1 + i] = tmp[i];
                }

                // The upper index has increased because of the lower bound insertion.
                ++upperIndex;

                // Copy in the new bounds.
                bounds[lowerIndex].Value   = lowerValues[axis];
                bounds[lowerIndex].ProxyId = proxyId;
                bounds[upperIndex].Value   = upperValues[axis];
                bounds[upperIndex].ProxyId = proxyId;

                bounds[lowerIndex].StabbingCount = lowerIndex == 0 ? (ushort)0 : bounds[lowerIndex - 1].StabbingCount;
                bounds[upperIndex].StabbingCount = bounds[upperIndex - 1].StabbingCount;

                // Adjust the stabbing count between the new bounds.
                for (int index = lowerIndex; index < upperIndex; ++index)
                {
                    ++bounds[index].StabbingCount;
                }

                // Adjust the all the affected bound indices.
                for (int index = lowerIndex; index < boundCount + 2; ++index)
                {
                    Proxy proxy_ = _proxyPool[bounds[index].ProxyId];
                    if (bounds[index].IsLower)
                    {
                        proxy_.LowerBounds[axis] = (ushort)index;
                    }
                    else
                    {
                        proxy_.UpperBounds[axis] = (ushort)index;
                    }
                }
            }

            ++_proxyCount;

            Box2DXDebug.Assert(_queryResultCount < Settings.MaxProxies);

            // Create pairs if the AABB is in range.
            for (int i = 0; i < _queryResultCount; ++i)
            {
                Box2DXDebug.Assert(_queryResults[i] < Settings.MaxProxies);
                Box2DXDebug.Assert(_proxyPool[_queryResults[i]].IsValid);

                _pairManager.AddBufferedPair(proxyId, _queryResults[i]);
            }

            _pairManager.Commit();

            if (IsValidate)
            {
                Validate();
            }

            // Prepare for next query.
            _queryResultCount = 0;
            IncrementTimeStamp();

            return(proxyId);
        }