Ejemplo n.º 1
0
        static public uint BinarySearch(List <b2Bound> bounds, int count, uint value)
        {
            int low  = 0;
            int high = count - 1;

            while (low <= high)
            {
                int     mid   = ((low + high) / 2);
                b2Bound bound = bounds[mid];
                if (bound.value > value)
                {
                    high = mid - 1;
                }
                else if (bound.value < value)
                {
                    low = mid + 1;
                }
                else
                {
                    return((uint)(mid));
                }
            }

            return((uint)(low));
        }
Ejemplo n.º 2
0
        // This one is only used for validation.
        private bool TestOverlapValidate(b2Proxy p1, b2Proxy p2)
        {
            for (int axis = 0; axis < 2; ++axis)
            {
                List <b2Bound> bounds = m_bounds[axis];

                //b2Settings.b2Assert(p1.lowerBounds[axis] < 2 * m_proxyCount);
                //b2Settings.b2Assert(p1.upperBounds[axis] < 2 * m_proxyCount);
                //b2Settings.b2Assert(p2.lowerBounds[axis] < 2 * m_proxyCount);
                //b2Settings.b2Assert(p2.upperBounds[axis] < 2 * m_proxyCount);

                b2Bound bound1 = bounds[(int)p1.lowerBounds[axis]];
                b2Bound bound2 = bounds[(int)p2.upperBounds[axis]];
                if (bound1.value > bound2.value)
                {
                    return(false);
                }

                bound1 = bounds[(int)p1.upperBounds[axis]];
                bound2 = bounds[(int)p2.lowerBounds[axis]];
                if (bound1.value < bound2.value)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public void Validate()
        {
            b2Pair  pair;
            b2Proxy proxy1;
            b2Proxy proxy2;
            bool    overlap;

            for (int axis = 0; axis < 2; ++axis)
            {
                List <b2Bound> bounds = m_bounds[axis];

                uint boundCount    = (uint)(2 * m_proxyCount);
                uint stabbingCount = 0;

                for (uint i = 0; i < boundCount; ++i)
                {
                    b2Bound bound = bounds[(int)i];
                    //b2Settings.b2Assert(i == 0 || bounds[i-1].value <= bound->value);
                    //b2Settings.b2Assert(bound->proxyId != b2_nullProxy);
                    //b2Settings.b2Assert(m_proxyPool[bound->proxyId].IsValid());

                    if (bound.IsLower() == true)
                    {
                        //b2Settings.b2Assert(m_proxyPool[bound.proxyId].lowerBounds[axis] == i);
                        stabbingCount++;
                    }
                    else
                    {
                        //b2Settings.b2Assert(m_proxyPool[bound.proxyId].upperBounds[axis] == i);
                        stabbingCount--;
                    }

                    //b2Settings.b2Assert(bound.stabbingCount == stabbingCount);
                }
            }
        }
Ejemplo n.º 4
0
        public bool TestOverlapBound(b2BoundValues b, b2Proxy p)
        {
            for (int axis = 0; axis < 2; ++axis)
            {
                List <b2Bound> bounds = m_bounds[axis];

                //b2Settings.b2Assert(p.lowerBounds[axis] < 2 * m_proxyCount);
                //b2Settings.b2Assert(p.upperBounds[axis] < 2 * m_proxyCount);

                b2Bound bound = bounds[(int)p.upperBounds[axis]];
                if (b.lowerValues[axis] > bound.value)
                {
                    return(false);
                }

                bound = bounds[(int)p.lowerBounds[axis]];
                if (b.upperValues[axis] < bound.value)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 5
0
        // Create and destroy proxies. These call Flush first.
        public object CreateProxy(b2AABB aabb, object userData)
        {
            uint    index;
            b2Proxy proxy;
            int     i;
            int     j;

            //b2Settings.b2Assert(m_proxyCount < b2_maxProxies);
            //b2Settings.b2Assert(m_freeProxy != b2Pair.b2_nullProxy);

            if (m_freeProxy == null)
            {
                // As all proxies are allocated, m_proxyCount == m_proxyPool.length
                m_freeProxy              = m_proxyPool[m_proxyCount] = new b2Proxy();
                m_freeProxy.next         = null;
                m_freeProxy.timeStamp    = 0;
                m_freeProxy.overlapCount = b2_invalid;
                m_freeProxy.userData     = null;

                for (i = 0; i < 2; i++)
                {
                    j = m_proxyCount * 2;
                    m_bounds[i][j++] = new b2Bound();
                    m_bounds[i][j]   = new b2Bound();
                }
            }

            proxy       = m_freeProxy;
            m_freeProxy = proxy.next;

            proxy.overlapCount = 0;
            proxy.userData     = userData;

            uint boundCount = (uint)(2 * m_proxyCount);

            List <float> lowerValues = new List <float>();
            List <float> upperValues = new List <float>();

            ComputeBounds(lowerValues, upperValues, aabb);

            for (int axis = 0; axis < 2; ++axis)
            {
                List <b2Bound> bounds        = m_bounds[axis];
                uint           lowerIndex    = 0;
                uint           upperIndex    = 0;
                List <uint>    lowerIndexOut = new List <uint>();
                lowerIndexOut.Add(lowerIndex);
                List <uint> upperIndexOut = new List <uint>();
                upperIndexOut.Add(upperIndex);
                QueryAxis(lowerIndexOut, upperIndexOut, (uint)lowerValues[axis], (uint)upperValues[axis], bounds, boundCount, axis);
                lowerIndex = lowerIndexOut[0];
                upperIndex = upperIndexOut[0];

                //bounds.splice(upperIndex, 0, bounds[bounds.length - 1]);
                //bounds.length--;
                //bounds.splice(lowerIndex, 0, bounds[bounds.length - 1]);
                //bounds.length--;
                bounds.Insert((int)upperIndex, bounds[bounds.Count - 1]);
                bounds.RemoveAt(bounds.Count - 1);
                bounds.Insert((int)lowerIndex, bounds[bounds.Count - 1]);
                bounds.RemoveAt(bounds.Count - 1);

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

                // Copy in the new bounds.
                b2Bound tBound1 = bounds[(int)lowerIndex];
                b2Bound tBound2 = bounds[(int)upperIndex];
                tBound1.value = (uint)lowerValues[axis];
                tBound1.proxy = proxy;
                tBound2.value = (uint)upperValues[axis];
                tBound2.proxy = proxy;

                b2Bound tBoundAS3 = bounds[(int)(lowerIndex - 1)];
                tBound1.stabbingCount = lowerIndex == 0 ? 0 : tBoundAS3.stabbingCount;
                tBoundAS3             = bounds[(int)(upperIndex - 1)];
                tBound2.stabbingCount = tBoundAS3.stabbingCount;

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

                // Adjust the all the affected bound indices.
                for (index = lowerIndex; index < boundCount + 2; ++index)
                {
                    tBound1 = bounds[(int)index];
                    b2Proxy proxy2 = tBound1.proxy;
                    if (tBound1.IsLower())
                    {
                        proxy2.lowerBounds[axis] = index;
                    }
                    else
                    {
                        proxy2.upperBounds[axis] = index;
                    }
                }
            }

            ++m_proxyCount;

            //b2Settings.b2Assert(m_queryResultCount < b2Settings.b2_maxProxies);

            for (i = 0; i < m_queryResultCount; ++i)
            {
                //b2Settings.b2Assert(m_queryResults[i] < b2_maxProxies);
                //b2Settings.b2Assert(m_proxyPool[m_queryResults[i]].IsValid());

                m_pairManager.AddBufferedPair(proxy, (b2Proxy)m_queryResults[i]);
            }

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

            return(proxy);
        }