///<summary> /// Constructs a new StaticGroupShape. ///</summary> ///<param name="collidables">List of collidables in the StaticGroup.</param> ///<param name="owner">StaticGroup directly associated with this shape.</param> public StaticGroupShape(IList <Collidable> collidables, StaticGroup owner) { this.StaticGroup = owner; CollidableTree = new BoundingBoxTree <Collidable>(collidables); //Rather than hooking up a bunch of ShapeChanged events here that don't capture the full capacity of change //in our child collidables, we will rely on the user telling the collidable tree to reformat itself directly. }
internal override void GetOverlaps <TElement>(BoundingBoxTree <TElement> .Node opposingNode, IList <TreeOverlapPair <T, TElement> > outputOverlappedElements) { bool intersects; if (opposingNode.IsLeaf) { //We're both leaves! Our parents have already done the testing for us, so we know we're overlapping. outputOverlappedElements.Add(new TreeOverlapPair <T, TElement>(element, opposingNode.Element)); } else { var opposingChildA = opposingNode.ChildA; var opposingChildB = opposingNode.ChildB; //If it's not a leaf, try to go deeper in the opposing hierarchy. BoundingBox.Intersects(ref opposingChildA.BoundingBox, out intersects); if (intersects) { GetOverlaps <TElement>(opposingChildA, outputOverlappedElements); } BoundingBox.Intersects(ref opposingChildB.BoundingBox, out intersects); if (intersects) { GetOverlaps <TElement>(opposingChildB, outputOverlappedElements); } } }
/// <summary> /// Gets the pairs of elements in each tree with overlapping bounding boxes. /// </summary> /// <typeparam name="TElement">Type of the elements in the opposing tree.</typeparam> /// <param name="tree">Other tree to test.</param> /// <param name="outputOverlappedElements">List of overlaps found by the query.</param> /// <returns>Whether or not any overlaps were found.</returns> public bool GetOverlaps <TElement>(BoundingBoxTree <TElement> tree, IList <TreeOverlapPair <T, TElement> > outputOverlappedElements) where TElement : IBoundingBoxOwner { bool intersects; root.BoundingBox.Intersects(ref tree.root.BoundingBox, out intersects); if (intersects) { root.GetOverlaps <TElement>(tree.root, outputOverlappedElements); } return(outputOverlappedElements.Count > 0); }
///<summary> /// Constructs a new compound hierarchy. ///</summary> ///<param name="owner">Owner of the hierarchy.</param> public CompoundHierarchy(CompoundCollidable owner) { this.owner = owner; var children = new CompoundChild[owner.children.Count]; Array.Copy(owner.children.Elements, children, owner.children.Count); //In order to initialize a good tree, the local space bounding boxes should first be computed. //Otherwise, the tree would try to create a hierarchy based on a bunch of zeroed out bounding boxes! for (int i = 0; i < children.Length; i++) { children[i].CollisionInformation.worldTransform = owner.Shape.shapes.Elements[i].LocalTransform; children[i].CollisionInformation.UpdateBoundingBoxInternal(0); } tree = new BoundingBoxTree <CompoundChild>(children); }
internal override void GetOverlaps <TElement>(BoundingBoxTree <TElement> .Node opposingNode, IList <TreeOverlapPair <T, TElement> > outputOverlappedElements) { bool intersects; if (opposingNode.IsLeaf) { //If it's a leaf, go deeper in our hierarchy, but not the opposition. childA.BoundingBox.Intersects(ref opposingNode.BoundingBox, out intersects); if (intersects) { childA.GetOverlaps(opposingNode, outputOverlappedElements); } childB.BoundingBox.Intersects(ref opposingNode.BoundingBox, out intersects); if (intersects) { childB.GetOverlaps(opposingNode, outputOverlappedElements); } } else { var opposingChildA = opposingNode.ChildA; var opposingChildB = opposingNode.ChildB; //If it's not a leaf, try to go deeper in both hierarchies. childA.BoundingBox.Intersects(ref opposingChildA.BoundingBox, out intersects); if (intersects) { childA.GetOverlaps(opposingChildA, outputOverlappedElements); } childA.BoundingBox.Intersects(ref opposingChildB.BoundingBox, out intersects); if (intersects) { childA.GetOverlaps(opposingChildB, outputOverlappedElements); } childB.BoundingBox.Intersects(ref opposingChildA.BoundingBox, out intersects); if (intersects) { childB.GetOverlaps(opposingChildA, outputOverlappedElements); } childB.BoundingBox.Intersects(ref opposingChildB.BoundingBox, out intersects); if (intersects) { childB.GetOverlaps(opposingChildB, outputOverlappedElements); } } }
internal abstract void GetOverlaps <TElement>(BoundingBoxTree <TElement> .Node opposingNode, IList <TreeOverlapPair <T, TElement> > outputOverlappedElements) where TElement : IBoundingBoxOwner;