public BVHNode(BVHNode Parent, Collidable Body) { Children[0] = null; Children[1] = null; this.Body = Body; this.Volume = Body.GetBoundingSphere(); this.Parent = Parent; }
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; }
private void DetectPlaneCollisions(BVHNode Hierarchy) { foreach (HalfSpace P in planes) Hierarchy.FindPotentialCollisionsWithPlane(detections, P); }
protected bool CollidesWith(BVHNode other) { return Volume.Intersects(other.Volume); }
public void Insert(Collidable Body) { if (this.isLeaf()) { Children[0] = new BVHNode(this, this.Body); Children[1] = new BVHNode(this, Body); this.Body = null; RecalculateBoundingVolume(); } else { BoundingSphere Temp = Body.GetBoundingSphere(); if (BoundingSphere.CreateMerged(Children[0].Volume, Temp).Radius < BoundingSphere.CreateMerged(Children[1].Volume, Temp).Radius) Children[0].Insert(Body); else Children[1].Insert(Body); } }