public void FindPotentialCollisionsWith(List<Contact> Potentials, BVHNode other) { if (!CollidesWith(other)) { if (!this.isLeaf()) this.FindPotentialCollisions(Potentials); if (!other.isLeaf()) other.FindPotentialCollisions(Potentials); return; } if (this.isLeaf() && other.isLeaf()) { Contact temp = new Contact(this.Body, other.Body); if (!temp.BothFixed()) Potentials.Add(temp); } else if (other.isLeaf() && !this.isLeaf()) { this.Children[0].FindPotentialCollisionsWith(Potentials, other); this.Children[1].FindPotentialCollisionsWith(Potentials, other); this.FindPotentialCollisions(Potentials); } else if (this.isLeaf() && !other.isLeaf()) { other.Children[0].FindPotentialCollisionsWith(Potentials, this); other.Children[1].FindPotentialCollisionsWith(Potentials, this); } else { other.Children[0].FindPotentialCollisionsWith(Potentials, this.Children[0]); other.Children[0].FindPotentialCollisionsWith(Potentials, this.Children[1]); other.Children[1].FindPotentialCollisionsWith(Potentials, this.Children[0]); other.Children[1].FindPotentialCollisionsWith(Potentials, this.Children[1]); this.FindPotentialCollisions(Potentials); other.FindPotentialCollisions(Potentials); } }
/// <summary> /// Since we are dealing with primatives only now, there is no need for a fine and /// coarse collision phases, just using ReDetect to generate the list, but when we /// expand the project to accept more complex structures, the whole algorithm will /// need reassembling. /// </summary> public List<Contact> ReDetect() { //detections.Clear(); //foreach (Collidable body0 in Shapes) //{ // foreach (Collidable body1 in Shapes) // { // if (body0 != body1) // { // Contact temp = new Contact(body0, body1); // if ( // //(!detections.Any((contact) => (temp.Equals(temp)))) && // !temp.BothFixed() // ) // detections.Add(temp); // } // } //} //return detections; didDetect = true; BVHNode Hierarchy = new BVHNode(null, Shapes.First.Value); /// The following statements guarantee that the LinkedList is not modified. /// Other ways: defining a class for the hierarcy (will consume O(n) checks). /// AsList() (will consume a lot). Collidable saver = Shapes.First.Value; Shapes.RemoveFirst(); foreach (Collidable shape in Shapes) { Hierarchy.Insert(shape); } Shapes.AddFirst(saver); detections.Clear(); Hierarchy.FindPotentialCollisions(detections); // Detect Collidables Collisions DetectPlaneCollisions(Hierarchy); // Detect Plane-Collidable Collisions return detections; }