Example #1
0
  public void destroyProxy(int proxyId) {
    assert (0 < m_proxyCount && m_proxyCount <= Settings.maxProxies);
    Proxy proxy = m_proxyPool[proxyId];
    assert (proxy.isValid());

    int boundCount = 2 * m_proxyCount;

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

      int lowerIndex = proxy.lowerBounds[axis];
      int upperIndex = proxy.upperBounds[axis];
      int lowerValue = bounds[lowerIndex].value;
      int upperValue = bounds[upperIndex].value;

      // memmove(bounds + lowerIndex, bounds + lowerIndex + 1,
      // (upperIndex - lowerIndex - 1) * sizeof(b2Bound));
      // memmove(bounds[lowerIndex + 1], bounds[lowerIndex],
      // (upperIndex - lowerIndex) * sizeof(b2Bound));
      System.arraycopy(
          m_bounds[axis], lowerIndex + 1, m_bounds[axis], lowerIndex, upperIndex - lowerIndex - 1);
      for (int i = 0; i < upperIndex - lowerIndex - 1; i++) {
        m_bounds[axis][lowerIndex + i] = new Bound(m_bounds[axis][lowerIndex + i]);
      }
      // memmove(bounds + upperIndex-1, bounds + upperIndex + 1,
      // (edgeCount - upperIndex - 1) * sizeof(b2Bound));
      System.arraycopy(
          m_bounds[axis],
          upperIndex + 1,
          m_bounds[axis],
          upperIndex - 1,
          boundCount - upperIndex - 1);
      for (int i = 0; i < boundCount - upperIndex - 1; i++) {
        m_bounds[axis][upperIndex - 1 + i] = new Bound(m_bounds[axis][upperIndex - 1 + i]);
      }

      // Fix bound indices.
      for (int index = lowerIndex; index < boundCount - 2; ++index) {
        Proxy proxyn = m_proxyPool[bounds[index].proxyId];
        if (bounds[index].isLower()) {
          proxyn.lowerBounds[axis] = index;
        } else {
          proxyn.upperBounds[axis] = index;
        }
      }

      // Fix stabbing count.
      for (int index = lowerIndex; index < upperIndex - 1; ++index) {
        --bounds[index].stabbingCount;
      }

      // Query for pairs to be removed. lowerIndex and upperIndex are not
      // needed.
      int[] ignored = new int[2];
      query(ignored, lowerValue, upperValue, bounds, boundCount - 2, axis);
    }

    assert (m_queryResultCount < Settings.maxProxies);

    for (int i = 0; i < m_queryResultCount; ++i) {
      assert (m_proxyPool[m_queryResults[i]].isValid());
      m_pairManager.removeBufferedPair(proxyId, m_queryResults[i]);
    }

    m_pairManager.commit();

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

    // Return the proxy to the pool.
    proxy.userData = null;
    proxy.overlapCount = BroadPhase.INVALID;
    proxy.lowerBounds[0] = BroadPhase.INVALID;
    proxy.lowerBounds[1] = BroadPhase.INVALID;
    proxy.upperBounds[0] = BroadPhase.INVALID;
    proxy.upperBounds[1] = BroadPhase.INVALID;

    // Return the proxy to the pool.
    proxy.setNext(m_freeProxy);
    m_freeProxy = proxyId;
    --m_proxyCount;

    if (s_validate) {
      validate();
    }
  }
Example #2
0
 public void commit() {
   m_pairManager.commit();
 }
Example #3
0
  // Create and destroy proxies. These call Flush first.
  int createProxy(
      AABB aabb, // int groupIndex, int categoryBits, int maskBits,
      Object userData) {
    if (debugPrint) {
      System.out.println("CreateProxy()");
    }

    assert (m_proxyCount < Settings.maxProxies);
    assert (m_freeProxy != PairManager.NULL_PROXY);

    int proxyId = m_freeProxy;
    Proxy proxy = m_proxyPool[proxyId];
    m_freeProxy = proxy.getNext();

    proxy.overlapCount = 0;
    proxy.userData = userData;
    //        proxy.groupIndex = groupIndex;
    //        proxy.categoryBits = categoryBits;
    //        proxy.maskBits = maskBits;

    // assert m_proxyCount < Settings.maxProxies;

    int boundCount = 2 * m_proxyCount;

    int lowerValues[] = new int[2];
    int upperValues[] = new int[2];
    computeBounds(lowerValues, upperValues, aabb);

    for (int axis = 0; axis < 2; ++axis) {
      Bound[] bounds = m_bounds[axis];
      int[] indexes = new int[2];

      query(indexes, lowerValues[axis], upperValues[axis], bounds, boundCount, axis);
      int lowerIndex = indexes[0];
      int upperIndex = indexes[1];

      // System.out.println(edgeCount + ", "+lowerValues[axis] + ",
      // "+upperValues[axis]);
      // memmove(bounds[upperIndex + 2], bounds[upperIndex],
      // (edgeCount - upperIndex) * sizeof(b2Bound));

      System.arraycopy(
          m_bounds[axis], upperIndex, m_bounds[axis], upperIndex + 2, boundCount - upperIndex);
      for (int i = 0; i < boundCount - upperIndex; i++) {
        m_bounds[axis][upperIndex + 2 + i] = new Bound(m_bounds[axis][upperIndex + 2 + i]);
      }

      // memmove(bounds[lowerIndex + 1], bounds[lowerIndex],
      // (upperIndex - lowerIndex) * sizeof(b2Bound));
      // System.out.println(lowerIndex+" "+upperIndex);
      System.arraycopy(
          m_bounds[axis], lowerIndex, m_bounds[axis], lowerIndex + 1, upperIndex - lowerIndex);
      for (int i = 0; i < upperIndex - lowerIndex; i++) {
        m_bounds[axis][lowerIndex + 1 + i] = new Bound(m_bounds[axis][lowerIndex + 1 + i]);
      }

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

      // Copy in the new bounds.

      // if (bounds[lowerIndex] == null)
      assert (bounds[lowerIndex] != null) : "Null pointer (lower)";
      // if (bounds[upperIndex] == null)
      assert (bounds[upperIndex] != null) : "Null pointer (upper)";

      bounds[lowerIndex].value = lowerValues[axis];
      bounds[lowerIndex].proxyId = proxyId;
      bounds[upperIndex].value = upperValues[axis];
      bounds[upperIndex].proxyId = proxyId;

      bounds[lowerIndex].stabbingCount = lowerIndex == 0 ? 0 : bounds[lowerIndex - 1].stabbingCount;
      bounds[upperIndex].stabbingCount = bounds[upperIndex - 1].stabbingCount;

      // System.out.printf("lv: %d , lid: %d, uv: %d, uid: %d
      // \n",lowerValues[axis],proxyId,upperValues[axis],proxyId);

      // 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 proxyn = m_proxyPool[bounds[index].proxyId];
        if (bounds[index].isLower()) {
          proxyn.lowerBounds[axis] = index;
        } else {
          proxyn.upperBounds[axis] = index;
        }
      }
    }

    ++m_proxyCount;

    assert m_queryResultCount < Settings.maxProxies;
    // Create pairs if the AABB is in range.
    for (int i = 0; i < m_queryResultCount; ++i) {
      assert (m_queryResults[i] < Settings.maxProxies);
      assert (m_proxyPool[m_queryResults[i]].isValid());

      m_pairManager.addBufferedPair(proxyId, m_queryResults[i]);
    }

    m_pairManager.commit();

    if (s_validate) {
      validate();
    }

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

    return proxyId;
  }