Package org.jbox2d.collision.broadphase

Examples of org.jbox2d.collision.broadphase.TreeNode


    // find the best sibling
    AABB leafAABB = m_nodes[leaf].aabb;
    int index = m_root;
    while (m_nodes[index].isLeaf() == false) {
      final TreeNode node = m_nodes[index];
      int child1 = node.child1;
      int child2 = node.child2;

      float area = node.aabb.getPerimeter();

      combinedAABB.combine(node.aabb, leafAABB);
      float combinedArea = combinedAABB.getPerimeter();

      // Cost of creating a new parent for this node and the new leaf
      float cost = 2.0f * combinedArea;

      // Minimum cost of pushing the leaf further down the tree
      float inheritanceCost = 2.0f * (combinedArea - area);

      // Cost of descending into child1
      float cost1;
      if (m_nodes[child1].isLeaf()) {
        combinedAABB.combine(leafAABB, m_nodes[child1].aabb);
        cost1 = combinedAABB.getPerimeter() + inheritanceCost;
      } else {
        combinedAABB.combine(leafAABB, m_nodes[child1].aabb);
        float oldArea = m_nodes[child1].aabb.getPerimeter();
        float newArea = combinedAABB.getPerimeter();
        cost1 = (newArea - oldArea) + inheritanceCost;
      }

      // Cost of descending into child2
      float cost2;
      if (m_nodes[child2].isLeaf()) {
        combinedAABB.combine(leafAABB, m_nodes[child2].aabb);
        cost2 = combinedAABB.getPerimeter() + inheritanceCost;
      } else {
        combinedAABB.combine(leafAABB, m_nodes[child2].aabb);
        float oldArea = m_nodes[child2].aabb.getPerimeter();
        float newArea = combinedAABB.getPerimeter();
        cost2 = newArea - oldArea + inheritanceCost;
      }

      // Descend according to the minimum cost.
      if (cost < cost1 && cost < cost2) {
        break;
      }

      // Descend
      if (cost1 < cost2) {
        index = child1;
      } else {
        index = child2;
      }
    }

    int sibling = index;
    int oldParent = m_nodes[sibling].parent;
    int newParentId = allocateNode();
    final TreeNode newParent = m_nodes[newParentId];
    newParent.parent = oldParent;
    newParent.userData = null;
    newParent.aabb.combine(leafAABB, m_nodes[sibling].aabb);
    newParent.height = m_nodes[sibling].height + 1;

View Full Code Here


  // Perform a left or right rotation if node A is imbalanced.
  // Returns the new root index.
  private int balance (int iA) {
    assert (iA != NULL_NODE);

    TreeNode A = m_nodes[iA];
    if (A.isLeaf() || A.height < 2) {
      return iA;
    }

    int iB = A.child1;
    int iC = A.child2;
    assert (0 <= iB && iB < m_nodeCapacity);
    assert (0 <= iC && iC < m_nodeCapacity);

    TreeNode B = m_nodes[iB];
    TreeNode C = m_nodes[iC];

    int balance = C.height - B.height;

    // Rotate C up
    if (balance > 1) {
      int iF = C.child1;
      int iG = C.child2;
      TreeNode F = m_nodes[iF];
      TreeNode G = m_nodes[iG];
      assert (0 <= iF && iF < m_nodeCapacity);
      assert (0 <= iG && iG < m_nodeCapacity);

      // Swap A and C
      C.child1 = iA;
      C.parent = A.parent;
      A.parent = iC;

      // A's old parent should point to C
      if (C.parent != NULL_NODE) {
        if (m_nodes[C.parent].child1 == iA) {
          m_nodes[C.parent].child1 = iC;
        } else {
          assert (m_nodes[C.parent].child2 == iA);
          m_nodes[C.parent].child2 = iC;
        }
      } else {
        m_root = iC;
      }

      // Rotate
      if (F.height > G.height) {
        C.child2 = iF;
        A.child2 = iG;
        G.parent = iA;
        A.aabb.combine(B.aabb, G.aabb);
        C.aabb.combine(A.aabb, F.aabb);

        A.height = 1 + MathUtils.max(B.height, G.height);
        C.height = 1 + MathUtils.max(A.height, F.height);
      } else {
        C.child2 = iG;
        A.child2 = iF;
        F.parent = iA;
        A.aabb.combine(B.aabb, F.aabb);
        C.aabb.combine(A.aabb, G.aabb);

        A.height = 1 + MathUtils.max(B.height, F.height);
        C.height = 1 + MathUtils.max(A.height, G.height);
      }

      return iC;
    }

    // Rotate B up
    if (balance < -1) {
      int iD = B.child1;
      int iE = B.child2;
      TreeNode D = m_nodes[iD];
      TreeNode E = m_nodes[iE];
      assert (0 <= iD && iD < m_nodeCapacity);
      assert (0 <= iE && iE < m_nodeCapacity);

      // Swap A and B
      B.child1 = iA;
View Full Code Here

    if (index == m_root) {
      assert (m_nodes[index].parent == NULL_NODE);
    }

    final TreeNode node = m_nodes[index];

    int child1 = node.child1;
    int child2 = node.child2;

    if (node.isLeaf()) {
      assert (child1 == NULL_NODE);
      assert (child2 == NULL_NODE);
      assert (node.height == 0);
      return;
    }
View Full Code Here

  private void validateMetrics (int index) {
    if (index == NULL_NODE) {
      return;
    }

    final TreeNode node = m_nodes[index];

    int child1 = node.child1;
    int child2 = node.child2;

    if (node.isLeaf()) {
      assert (child1 == NULL_NODE);
      assert (child2 == NULL_NODE);
      assert (node.height == 0);
      return;
    }
View Full Code Here

  private final Color3f color = new Color3f();
  private final Vec2 textVec = new Vec2();

  public void drawTree (DebugDraw argDraw, int nodeId, int spot, int height) {
    final TreeNode node = m_nodes[nodeId];
    node.aabb.getVertices(drawVecs);

    color.set(1, (height - spot) * 1f / height, (height - spot) * 1f / height);
    argDraw.drawPolygon(drawVecs, 4, color);

View Full Code Here

TOP

Related Classes of org.jbox2d.collision.broadphase.TreeNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.