com.jme.bounding
Class CollisionTree

java.lang.Object
  extended by com.jme.bounding.CollisionTree
All Implemented Interfaces:
java.io.Serializable

public class CollisionTree
extends java.lang.Object
implements java.io.Serializable

CollisionTree defines a well balanced red black tree used for triangle accurate collision detection. The CollisionTree supports three types: Oriented Bounding Box, Axis-Aligned Bounding Box and Sphere. The tree is composed of a heirarchy of nodes, all but leaf nodes have two children, a left and a right, where the children contain half of the triangles of the parent. This "half split" is executed down the tree until the node is maintaining a set maximum of triangles. This node is called the leaf node. Intersection checks are handled as follows:
1. The bounds of the node is checked for intersection. If no intersection occurs here, no further processing is needed, the children (nodes or triangles) do not intersect.
2a. If an intersection occurs and we have children left/right nodes, pass the intersection information to the children.
2b. If an intersection occurs and we are a leaf node, pass each triangle individually for intersection checking.
Optionally, during creation of the collision tree, sorting can be applied. Sorting will attempt to optimize the order of the triangles in such a way as to best split for left and right sub-trees. This function can lead to faster intersection tests, but increases the creation time for the tree. The number of triangles a leaf node is responsible for is defined in CollisionTreeManager. It is actually recommended to allow CollisionTreeManager to maintain the collision trees for a scene.

Author:
Mark Powell
See Also:
CollisionTreeManager, Serialized Form

Nested Class Summary
static class CollisionTree.Type
           
 
Field Summary
protected static TreeComparator comparator
           
protected  TriMesh mesh
           
 
Constructor Summary
CollisionTree(CollisionTree.Type type)
          Constructor creates a new instance of CollisionTree.
 
Method Summary
 void construct(int childIndex, Node parent, boolean doSort)
          Recreate this Collision Tree for the given Node and child index.
 void construct(TriMesh mesh, boolean doSort)
          Recreate this Collision Tree for the given mesh.
 boolean containsAnyLeaf(java.util.ArrayList<java.lang.Integer> triangleIndices)
          Checks if this branch or one of its subbranches/leaves contains any of the given triangleIndices
 void createTree(int start, int end, boolean doSort)
          Creates a Collision Tree by recursively creating children nodes, splitting the triangles this node is responsible for in half until the desired triangle count is reached.
 BoundingVolume getBounds()
          Returns the bounding volume for this tree node in local space.
 BoundingVolume getWorldBounds()
          Returns the bounding volume for this tree node in world space.
 boolean intersect(CollisionTree collisionTree)
          Determines if this Collision Tree intersects the given CollisionTree.
 boolean intersect(CollisionTree collisionTree, java.util.ArrayList<java.lang.Integer> aList, java.util.ArrayList<java.lang.Integer> bList)
          Determines if this Collision Tree intersects the given CollisionTree.
 void intersect(Ray ray, java.util.ArrayList<java.lang.Integer> triList)
          intersect checks for collisions between this collision tree and a provided Ray.
 boolean intersectsBounding(BoundingVolume volume)
          Tests if the world bounds of the node at this level intersects a provided bounding volume.
 void rebuildLeaves(java.util.ArrayList<java.lang.Integer> triangleIndices, int startLevel)
          Rebuild all the leaves listed in triangleIndices, and any branches leading up to them.
 void sortTris()
          sortTris attempts to optimize the ordering of the subsection of the array of triangles this node is responsible for.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mesh

protected TriMesh mesh

comparator

protected static final TreeComparator comparator
Constructor Detail

CollisionTree

public CollisionTree(CollisionTree.Type type)
Constructor creates a new instance of CollisionTree.

Parameters:
type - the type of collision tree to make
See Also:
CollisionTree.Type
Method Detail

construct

public void construct(int childIndex,
                      Node parent,
                      boolean doSort)
Recreate this Collision Tree for the given Node and child index.

Parameters:
childIndex - the index of the child to generate the tree for.
parent - The Node that this OBBTree should represent.
doSort - true to sort triangles during creation, false otherwise

construct

public void construct(TriMesh mesh,
                      boolean doSort)
Recreate this Collision Tree for the given mesh.

Parameters:
mesh - The trimesh that this OBBTree should represent.
doSort - true to sort triangles during creation, false otherwise

createTree

public void createTree(int start,
                       int end,
                       boolean doSort)
Creates a Collision Tree by recursively creating children nodes, splitting the triangles this node is responsible for in half until the desired triangle count is reached.

Parameters:
start - The start index of the tris array, inclusive.
end - The end index of the tris array, exclusive.
doSort - True if the triangles should be sorted at each level, false otherwise.

intersectsBounding

public boolean intersectsBounding(BoundingVolume volume)
Tests if the world bounds of the node at this level intersects a provided bounding volume. If an intersection occurs, true is returned, otherwise false is returned. If the provided volume is invalid, false is returned.

Parameters:
volume - the volume to intersect with.
Returns:
true if there is an intersect, false otherwise.

intersect

public boolean intersect(CollisionTree collisionTree)
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.

Parameters:
collisionTree - The Tree to test.
Returns:
True if they intersect, false otherwise.

intersect

public boolean intersect(CollisionTree collisionTree,
                         java.util.ArrayList<java.lang.Integer> aList,
                         java.util.ArrayList<java.lang.Integer> bList)
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 triangles for this mesh and bList is the triangles for the test tree.

Parameters:
collisionTree - The Tree to test.
aList - a list to contain the colliding triangles of this mesh.
bList - a list to contain the colliding triangles of the testing mesh.
Returns:
True if they intersect, false otherwise.

intersect

public void intersect(Ray ray,
                      java.util.ArrayList<java.lang.Integer> triList)
intersect checks for collisions between this collision tree and a provided Ray. Any collisions are stored in a provided list. The ray is assumed to have a normalized direction for accurate calculations.

Parameters:
ray - the ray to test for intersections.
triList - the list to store instersections with.

getBounds

public BoundingVolume getBounds()
Returns the bounding volume for this tree node in local space.

Returns:
the bounding volume for this tree node in local space.

getWorldBounds

public BoundingVolume getWorldBounds()
Returns the bounding volume for this tree node in world space.

Returns:
the bounding volume for this tree node in world space.

sortTris

public void sortTris()
sortTris attempts to optimize the ordering of the subsection of the array of triangles this node is responsible for. The sorting is based on the most efficient method along an axis. Using the TreeComparator and quick sort, the subsection of the array is sorted.


rebuildLeaves

public void rebuildLeaves(java.util.ArrayList<java.lang.Integer> triangleIndices,
                          int startLevel)
Rebuild all the leaves listed in triangleIndices, and any branches leading up to them.

Parameters:
triangleIndices - a list of all the leaves to rebuild
startLevel - how many trunk levels to ignore, for none put zero (ignoring the first 2-3 levels increases speed greatly)

containsAnyLeaf

public boolean containsAnyLeaf(java.util.ArrayList<java.lang.Integer> triangleIndices)
Checks if this branch or one of its subbranches/leaves contains any of the given triangleIndices

Parameters:
triangleIndices - the indices to look for
Returns:
true if the index is contained, false otherwise