private bool IsVisible(int a, int b, TriangulatorDataSet ds) { Vector2 aPos = ds.NodePosV2(a); Vector2 bPos = ds.NodePosV2(b); Vector2 dir = bPos - aPos; float curSqrDist = SomeMath.SqrDistance(aPos, bPos); foreach (var edge in ds.edges) { if (edge.Contains(a, b)) { continue; } Vector2 intersection; if (SomeMath.RayIntersectXZ( aPos, dir, //from, direction ds.NodePosV2(edge.a), ds.NodePosV2(edge.b), //a, b out intersection) && SomeMath.SqrDistance(aPos, intersection) < curSqrDist) { //Debuger3.AddRay(new Vector3(intersection.x, 0, intersection.y), Vector3.up, Color.magenta); //Debuger3.AddLine(ds.NodePosV3(a), new Vector3(intersection.x, 0, intersection.y), Color.magenta); //Debuger3.AddLine(ds.NodePosV3(b), new Vector3(intersection.x, 0, intersection.y), Color.magenta); return(false); } } return(true); }
/// <summary> /// return ray intersection by top projection (if it occured) /// </summary> public bool RayIntersectXZ(float rayOriginX, float rayOriginY, float rayDirectionX, float rayDirectionZ, out Vector3 result) { float Rx, Ry, Rz; bool Rb = SomeMath.RayIntersectXZ(rayOriginX, rayOriginY, rayDirectionX, rayDirectionZ, xLeft, yLeft, zLeft, xRight, yRight, zRight, out Rx, out Ry, out Rz); result = new Vector3(Rx, Ry, Rz); return(Rb); }
private void Raycast(Vector3 origin, Vector3 direction, out RaycastHitNavMesh hit, float length, int maxIterations, Passability expectedPassability, Area expectedArea, Cell cell) { HashSet <CellContentData> raycastExclude = new HashSet <CellContentData>(); List <RaycastSomeData> raycastTempData = new List <RaycastSomeData>(); float maxLengthSqr = length * length; for (int iteration = 0; iteration < maxIterations; iteration++) { raycastTempData.Clear();//iteration data cleared foreach (var pair in cell.dataContentPairs) { CellContentData curData = pair.Key; if (!raycastExclude.Add(curData))//mean it's already contain this { continue; } Vector3 intersect; if (SomeMath.RayIntersectXZ(origin, direction, curData.leftV3, curData.rightV3, out intersect)) { if (pair.Value != null) { Cell otherCell = pair.Value.connection; if (otherCell == cell | !otherCell.canBeUsed) { continue; } if (cell.passability != otherCell.passability || cell.area != otherCell.area) { hit = new RaycastHitNavMesh(intersect, SomeMath.SqrDistance(origin, intersect) < maxLengthSqr);//!!! return; } raycastTempData.Add(new RaycastSomeData(intersect, otherCell)); } else { raycastTempData.Add(new RaycastSomeData(intersect, null)); } } } //check if there possible connection for (int i = 0; i < raycastTempData.Count; i++) { if (raycastTempData[i].cell != null) { cell = raycastTempData[i].cell; goto CONTINUE; } } //now we definetly hit something and now find furthest float furthestSqrDist = 0f; Vector3 furthest = origin; for (int i = 0; i < raycastTempData.Count; i++) { float curSqrDist = SomeMath.SqrDistance(raycastTempData[i].point, origin); if (curSqrDist > furthestSqrDist) { furthestSqrDist = curSqrDist; furthest = raycastTempData[i].point; } } hit = new RaycastHitNavMesh(furthest, SomeMath.SqrDistance(origin, furthest) < maxLengthSqr); return; CONTINUE : { continue; } } hit = new RaycastHitNavMesh(origin, true, true); return; }
public bool RayIntersectXZ(float rayOriginX, float rayOriginY, float rayDirectionX, float rayDirectionZ, out float resultX, out float resultY, out float resultZ) { return(SomeMath.RayIntersectXZ(rayOriginX, rayOriginY, rayDirectionX, rayDirectionZ, xLeft, yLeft, zLeft, xRight, yRight, zRight, out resultX, out resultY, out resultZ)); }