예제 #1
0
  /**
   * Determines if this Collision Tree intersects the given CollisionTree. If a collision occurs,
   * true is returned, otherwise false is returned. If the provided collisionTree is invalid, false
   * is returned. All collisions that occur are stored in lists as an integer index into the mesh's
   * triangle buffer. where aList is the primitives for this mesh and bList is the primitives for
   * the test tree.
   *
   * @param collisionTree The Tree to test.
   * @param aList a list to contain the colliding primitives of this mesh.
   * @param bList a list to contain the colliding primitives of the testing mesh.
   * @return True if they intersect, false otherwise.
   */
  public boolean intersect(
      final CollisionTree collisionTree,
      final List<PrimitiveKey> aList,
      final List<PrimitiveKey> bList) {

    if (collisionTree == null) {
      return false;
    }

    collisionTree._worldBounds =
        collisionTree._bounds.transform(
            collisionTree.getMesh().getWorldTransform(), collisionTree._worldBounds);

    // our two collision bounds do not intersect, therefore, our primitives
    // must not intersect. Return false.
    if (!intersectsBounding(collisionTree._worldBounds)) {
      return false;
    }

    // if our node is not a leaf send the children (both left and right) to
    // the test tree.
    if (_left != null) { // This is not a leaf
      boolean test = collisionTree.intersect(_left, bList, aList);
      test = collisionTree.intersect(_right, bList, aList) || test;
      return test;
    }

    // This node is a leaf, but the testing tree node is not. Therefore,
    // continue processing the testing tree until we find its leaves.
    if (collisionTree._left != null) {
      boolean test = intersect(collisionTree._left, aList, bList);
      test = intersect(collisionTree._right, aList, bList) || test;
      return test;
    }

    // both this node and the testing node are leaves. Therefore, we can
    // switch to checking the contained primitives with each other. Any
    // that are found to intersect are placed in the appropriate list.
    final ReadOnlyTransform transformA = getMesh().getWorldTransform();
    final ReadOnlyTransform transformB = collisionTree.getMesh().getWorldTransform();

    final MeshData dataA = getMesh().getMeshData();
    final MeshData dataB = collisionTree.getMesh().getMeshData();

    Vector3[] storeA = null;
    Vector3[] storeB = null;

    boolean test = false;

    for (int i = _start; i < _end; i++) {
      storeA = dataA.getPrimitiveVertices(_primitiveIndices[i], _section, storeA);
      // to world space
      for (int t = 0; t < storeA.length; t++) {
        transformA.applyForward(storeA[t]);
      }
      for (int j = collisionTree._start; j < collisionTree._end; j++) {
        storeB =
            dataB.getPrimitiveVertices(
                collisionTree._primitiveIndices[j], collisionTree._section, storeB);
        // to world space
        for (int t = 0; t < storeB.length; t++) {
          transformB.applyForward(storeB[t]);
        }
        if (Intersection.intersection(storeA, storeB)) {
          test = true;
          aList.add(new PrimitiveKey(_primitiveIndices[i], _section));
          bList.add(new PrimitiveKey(collisionTree._primitiveIndices[j], collisionTree._section));
        }
      }
    }

    return test;
  }
예제 #2
0
  /**
   * Determines if this Collision Tree intersects the given CollisionTree. If a collision occurs,
   * true is returned, otherwise false is returned. If the provided collisionTree is invalid, false
   * is returned.
   *
   * @param collisionTree The Tree to test.
   * @return True if they intersect, false otherwise.
   */
  public boolean intersect(final CollisionTree collisionTree) {
    if (collisionTree == null) {
      return false;
    }

    collisionTree._worldBounds =
        collisionTree._bounds.transform(
            collisionTree.getMesh().getWorldTransform(), collisionTree._worldBounds);

    // our two collision bounds do not intersect, therefore, our primitives
    // must not intersect. Return false.
    if (!intersectsBounding(collisionTree._worldBounds)) {
      return false;
    }

    // check children
    if (_left != null) { // This is not a leaf
      if (collisionTree.intersect(_left)) {
        return true;
      }
      if (collisionTree.intersect(_right)) {
        return true;
      }
      return false;
    }

    // This is a leaf
    if (collisionTree._left != null) {
      // but collision isn't
      if (intersect(collisionTree._left)) {
        return true;
      }
      if (intersect(collisionTree._right)) {
        return true;
      }
      return false;
    }

    // both are leaves
    final ReadOnlyTransform transformA = getMesh().getWorldTransform();
    final ReadOnlyTransform transformB = collisionTree.getMesh().getWorldTransform();

    final MeshData dataA = getMesh().getMeshData();
    final MeshData dataB = collisionTree.getMesh().getMeshData();

    Vector3[] storeA = null;
    Vector3[] storeB = null;

    // for every primitive to compare, put them into world space and check for intersections
    for (int i = _start; i < _end; i++) {
      storeA = dataA.getPrimitiveVertices(_primitiveIndices[i], _section, storeA);
      // to world space
      for (int t = 0; t < storeA.length; t++) {
        transformA.applyForward(storeA[t]);
      }
      for (int j = collisionTree._start; j < collisionTree._end; j++) {
        storeB =
            dataB.getPrimitiveVertices(
                collisionTree._primitiveIndices[j], collisionTree._section, storeB);
        // to world space
        for (int t = 0; t < storeB.length; t++) {
          transformB.applyForward(storeB[t]);
        }
        if (Intersection.intersection(storeA, storeB)) {
          return true;
        }
      }
    }

    return false;
  }