public void SetCellToEdge(GenerationCellInfo cell) { Vector3 closest; SomeMath.ClosestToSegmentTopProjection(data.leftV3, data.rightV3, cell.centerV2, out closest); if (SomeMath.LinePointSideMathf(data.leftV2, data.rightV2, cell.centerV2) > 0) { //Debuger_K.AddLine(closest, cell.centerV3, Color.white); //Debuger_K.AddLabel(SomeMath.MidPoint(closest, cell.centerV3), "Up"); upCell = cell; } else { //Debuger_K.AddLine(closest, cell.centerV3, Color.white); //Debuger_K.AddLabel(SomeMath.MidPoint(closest, cell.centerV3), "Down"); downCell = cell; } if (upCell != null & downCell != null) { Vector3 intersection; SomeMath.ClampedRayIntersectXZ(upCell.centerV3, downCell.centerV3 - upCell.centerV3, data.leftV3, data.rightV3, out intersection); float upCellCost = Vector3.Distance(upCell.centerV3, intersection) * upCell.cell.area.cost; float downCellCost = Vector3.Distance(downCell.centerV3, intersection) * downCell.cell.area.cost; downCell.SetConnection(new CellContentData(data.leftV3, data.rightV3), upCell.cell, downCellCost, upCellCost, intersection); upCell.SetConnection(new CellContentData(data.rightV3, data.leftV3), downCell.cell, upCellCost, downCellCost, intersection); } }
//Vector2 public void GetClosestPointToCell(Vector2 targetPos, out Vector3 closestPoint, out bool isOutsideCell) { float closestSqrDistance = float.MaxValue; closestPoint = Vector3.zero; foreach (var edgeData in _contentDictionary.Keys) { if (SomeMath.PointInTriangle(edgeData.leftV2, edgeData.rightV2, centerVector2, targetPos)) { closestPoint = new Vector3(targetPos.x, SomeMath.CalculateHeight(edgeData.leftV3, edgeData.rightV3, centerVector3, targetPos.x, targetPos.y), targetPos.y); isOutsideCell = false; return; } else { Vector3 curInte; SomeMath.ClosestToSegmentTopProjection(edgeData.leftV3, edgeData.rightV3, targetPos, true, out curInte); float curSqrDist = SomeMath.SqrDistance(targetPos.x, targetPos.y, curInte.x, curInte.z); if (curSqrDist < closestSqrDistance) { closestSqrDistance = curSqrDist; closestPoint = curInte; } } } isOutsideCell = true; return; }
//takes edges and axis. check if edge exist, if exist add closest point to cell public void AddPortal(IEnumerable <EdgeAbstract> edges, Vector3 axis) { Vector2 axisV2 = new Vector2(axis.x, axis.z); Dictionary <Cell, Vector3> cellMountPoints = new Dictionary <Cell, Vector3>(); foreach (var abstractEdge in edges) { CellContentData data = new CellContentData(abstractEdge); Vector3 intersection; SomeMath.ClosestToSegmentTopProjection(data.a, data.b, axisV2, true, out intersection); foreach (var cell in _cells) { if (cell.Contains(data)) { if (cellMountPoints.ContainsKey(cell)) { if (SomeMath.SqrDistance(cellMountPoints[cell], axis) > SomeMath.SqrDistance(intersection, axis)) { cellMountPoints[cell] = intersection; } } else { cellMountPoints.Add(cell, intersection); } } } } Vector2 normalRaw; switch (cellMountPoints.Count) { case 0: return; case 1: normalRaw = ToV2((cellMountPoints.First().Value - axis)).normalized * -1; break; case 2: normalRaw = ( ToV2(cellMountPoints.First().Value - axis).normalized + ToV2(cellMountPoints.Last().Value - axis).normalized).normalized * -1; break; default: normalRaw = Vector2.left; Dictionary <Cell, float> cellAngles = new Dictionary <Cell, float>(); Cell first = cellMountPoints.First().Key; cellAngles.Add(first, 0f); Vector3 firstDirV3 = cellMountPoints.First().Value - axis; Vector2 firstDirV2 = ToV2(firstDirV3); foreach (var pair in cellMountPoints) { if (pair.Key == first) { continue; } Vector2 curDir = new Vector2(pair.Value.x - axis.x, pair.Value.z - axis.z); cellAngles.Add(pair.Key, Vector2.Angle(firstDirV2, curDir) * Mathf.Sign(SomeMath.V2Cross(firstDirV2, curDir))); } normalRaw = ( ToV2(cellMountPoints[cellAngles.Aggregate((l, r) => l.Value > r.Value ? l : r).Key] - axis).normalized + ToV2(cellMountPoints[cellAngles.Aggregate((l, r) => l.Value < r.Value ? l : r).Key] - axis).normalized).normalized * -1; break; } portalBases.Add(new JumpPortalBase(cellMountPoints, axis, new Vector3(normalRaw.x, 0, normalRaw.y))); }