Pathfinding.Polygon.LineIntersectsBounds C# (CSharp) Method

LineIntersectsBounds() public static method

public static LineIntersectsBounds ( Bounds bounds, Vector3 a, Vector3 b ) : bool
bounds UnityEngine.Bounds
a Vector3
b Vector3
return bool
		public static bool LineIntersectsBounds (Bounds bounds, Vector3 a, Vector3 b) {	
			// Put line in box space
			//CMatrix MInv = m_M.InvertSimple();
			//CVec3 LB1 = MInv * L1;
			//CVec3 LB2 = MInv * L2;
			a -= bounds.center;
			b -= bounds.center;
			
			// Get line midpoint and extent
			Vector3 LMid = (a + b) * 0.5F; 
			Vector3 L = (a - LMid);
			Vector3 LExt = new Vector3 ( Math.Abs(L.x), Math.Abs(L.y), Math.Abs(L.z) );
			
			Vector3 extent = bounds.extents;
			
			// Use Separating Axis Test
			// Separation vector from box center to line center is LMid, since the line is in box space
			if ( Math.Abs( LMid.x ) > extent.x + LExt.x ) return false;
			if ( Math.Abs( LMid.y ) > extent.y + LExt.y ) return false;
			if ( Math.Abs( LMid.z ) > extent.z + LExt.z ) return false;
			// Crossproducts of line and each axis
			if ( Math.Abs( LMid.y * L.z - LMid.z * L.y)  >  (extent.y * LExt.z + extent.z * LExt.y) ) return false;
			if ( Math.Abs( LMid.x * L.z - LMid.z * L.x)  >  (extent.x * LExt.z + extent.z * LExt.x) ) return false;
			if ( Math.Abs( LMid.x * L.y - LMid.y * L.x)  >  (extent.x * LExt.y + extent.y * LExt.x) ) return false;
			// No separating axis, the line intersects
			return true;
		}
		

Usage Example

        /** Updates an area in the list graph.
         * Recalculates possibly affected connections, i.e all connectionlines passing trough the bounds of the \a guo will be recalculated
         * \astarpro */
        public void UpdateArea(GraphUpdateObject guo)
        {
            if (nodes == null)
            {
                return;
            }

            for (int i = 0; i < nodeCount; i++)
            {
                if (guo.bounds.Contains((Vector3)nodes[i].position))
                {
                    guo.WillUpdateNode(nodes[i]);
                    guo.Apply(nodes[i]);
                }
            }

            if (guo.updatePhysics)
            {
                //Use a copy of the bounding box, we should not change the GUO's bounding box since it might be used for other graph updates
                Bounds bounds = guo.bounds;

                if (thickRaycast)
                {
                    //Expand the bounding box to account for the thick raycast
                    bounds.Expand(thickRaycastRadius * 2);
                }

                //Create two temporary arrays used for holding new connections and costs
                List <GraphNode> tmp_arr = Pathfinding.Util.ListPool <GraphNode> .Claim();

                List <uint> tmp_arr2 = Pathfinding.Util.ListPool <uint> .Claim();

                for (int i = 0; i < nodeCount; i++)
                {
                    PointNode node = nodes[i] as PointNode;
                    Vector3   a    = (Vector3)node.position;

                    List <GraphNode> conn  = null;
                    List <uint>      costs = null;

                    for (int j = 0; j < nodeCount; j++)
                    {
                        if (j == i)
                        {
                            continue;
                        }

                        Vector3 b = (Vector3)nodes[j].position;
                        if (Polygon.LineIntersectsBounds(bounds, a, b))
                        {
                            float     dist;
                            PointNode other    = nodes[j] as PointNode;
                            bool      contains = node.ContainsConnection(other);

                            //Note, the IsValidConnection test will actually only be done once
                            //no matter what,so there is no performance penalty there
                            if (!contains && IsValidConnection(node, other, out dist))
                            {
                                //Debug.DrawLine (a+Vector3.up*0.1F,b+Vector3.up*0.1F,Color.green);
                                if (conn == null)
                                {
                                    tmp_arr.Clear();
                                    tmp_arr2.Clear();
                                    conn  = tmp_arr;
                                    costs = tmp_arr2;
                                    conn.AddRange(node.connections);
                                    costs.AddRange(node.connectionCosts);
                                }

                                uint cost = (uint)Mathf.RoundToInt(dist * Int3.FloatPrecision);
                                conn.Add(other);
                                costs.Add(cost);
                            }
                            else if (contains && !IsValidConnection(node, other, out dist))
                            {
                                //Debug.DrawLine (a+Vector3.up*0.5F*Random.value,b+Vector3.up*0.5F*Random.value,Color.red);
                                if (conn == null)
                                {
                                    tmp_arr.Clear();
                                    tmp_arr2.Clear();
                                    conn  = tmp_arr;
                                    costs = tmp_arr2;
                                    conn.AddRange(node.connections);
                                    costs.AddRange(node.connectionCosts);
                                }

                                int p = conn.IndexOf(other);

                                //Shouldn't have to check for it, but who knows what might go wrong
                                if (p != -1)
                                {
                                    conn.RemoveAt(p);
                                    costs.RemoveAt(p);
                                }
                            }
                        }
                    }

                    if (conn != null)
                    {
                        node.connections     = conn.ToArray();
                        node.connectionCosts = costs.ToArray();
                    }
                }

                Pathfinding.Util.ListPool <GraphNode> .Release(tmp_arr);

                Pathfinding.Util.ListPool <uint> .Release(tmp_arr2);
            }
        }
All Usage Examples Of Pathfinding.Polygon::LineIntersectsBounds