public static bool LineIntersection(myAABB box, Vector3 StartPoint, Vector3 EndPoint, out Vector3 IntersectionPoint) { //define initial lowest and highest float Lowest = 0.0f; float Highest = 1.0f; //default value for intersection point is needed IntersectionPoint = Vector3.zero; //we do an intersection check on every axis by resuing the IntersectingAxis function if (!IntersectingAxis(Vector3.right, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.up, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.forward, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } //caulculate intersection point through interpolation IntersectionPoint = VectorMaths.LERP(StartPoint, EndPoint, Lowest); return(true); }
public static bool Intersects(myAABB b1, myAABB b2) { //checks each individual face in the bounding box to see if there are intersections return(!(b2.Left > b1.Right || b2.Right < b1.Left || b2.Top < b1.Bottom || b2.Bottom > b1.Top || b2.Back > b1.Front || b2.Front < b1.Back)); }
//will move slowly towards the sun until it collides void Update() { distance += Time.deltaTime; transform.position = new Vector3(-40 + distance, 0, -10); Vector3 temp = transform.position; //creating the bounding box for the UFO Vector3 minextent = new Vector3(temp.x - 4, temp.y - 4, temp.z - 4); Vector3 maxextent = new Vector3(temp.x + 4, temp.y + 4, temp.z + 4); UFOBOX = new myAABB(minextent, maxextent); }
public static bool IntersectingAxis(Vector3 Axis, myAABB box, Vector3 StartPoint, Vector3 EndPoint, ref float Lowest, ref float Highest) { //calculate minimum and maximum based on current axis float Minimum = 0.0f, Maximum = 1.0f; if (Axis == Vector3.right) { Minimum = (box.Left - StartPoint.x) / (EndPoint.x - StartPoint.x); Maximum = (box.Right - StartPoint.x) / (EndPoint.x - StartPoint.x); } else if (Axis == Vector3.up) { Minimum = (box.Bottom - StartPoint.y) / (EndPoint.y - StartPoint.y); Maximum = (box.Top - StartPoint.y) / (EndPoint.y - StartPoint.y); } else if (Axis == Vector3.forward) { Minimum = (box.Back - StartPoint.z) / (EndPoint.z - StartPoint.z); Maximum = (box.Front - StartPoint.z) / (EndPoint.z - StartPoint.z); } if (Maximum < Minimum) { //swap the values float temp = Maximum; Maximum = Minimum; Minimum = temp; } //eliminate non-intersections early if (Maximum < Lowest) { return(false); } if (Minimum > Highest) { return(false); } Lowest = Mathf.Max(Minimum, Lowest); Highest = Mathf.Min(Maximum, Highest); if (Lowest > Highest) { return(false); } return(true); }
void Update() { if (destroyed == false) { //gets the value of the bounding box on the UFO and compares it to a box created around this object. UFOStuff UFOScript = UFO.GetComponent <UFOStuff>(); myAABB UFObox = UFOScript.UFOBOX; Vector3 temp = transform.position; Vector3 minextent = new Vector3(temp.x - 15, temp.y - 15, temp.z - 15); Vector3 maxextent = new Vector3(temp.x + 15, temp.y + 15, temp.z + 15); myAABB box = new myAABB(minextent, maxextent); //if the bounding box of the UFO intersects with this bounding box the UFO is destroyed if (myAABB.Intersects(box, UFObox)) { Destroy(UFO); destroyed = true; } } }