/// <summary> /// Returns distance from first node to given point along stage path. /// </summary> /// <param name="pos"></param> /// <returns></returns> public float GetDistanceFromBeginning(Vector3 pos) { if (nodes == null || nodes.Count == 0) { return(0f); } int closestNodeIndex = 0; float dist = float.MaxValue; Vector3 closestPoint = pos; for (int i = 0; i < nodes.Count - 1; i++) { Vector3 closesetTMP = StageUtilities.FindClosestPointOnLineSegment(nodes [i].Position, nodes [i + 1].Position, pos); float d = Vector3.Distance(pos, closesetTMP); if (d < dist) { closestPoint = closesetTMP; dist = d; closestNodeIndex = i; } } dist = 0; for (int i = 0; i < closestNodeIndex; i++) { dist += Vector3.Distance(nodes [i].Position, nodes [i + 1].Position); } dist += Vector3.Distance(nodes [closestNodeIndex].Position, closestPoint); return(dist); }
List <Vector3> shortenIntersectingLineSegments(List <Vector3> points) { List <Vector3> result = new List <Vector3> (); if (points.Count >= 4) { for (int i = 0; i < points.Count - 3; i += 2) { Vector3 p0 = points [i]; Vector3 p1 = points [i + 1]; Vector3 p2 = points [i + 2]; Vector3 p3 = points [i + 3]; Vector3 intersectionPoint = Vector3.zero; if (StageUtilities.GetLineSegmentIntersectionPoint(p0, p1, p2, p3, out intersectionPoint)) { result.Add(p0); result.Add(intersectionPoint); points [i + 1] = intersectionPoint; points [i + 2] = intersectionPoint; } else { result.Add(p0); result.Add(p1); } } result.Add(points [points.Count - 2]); result.Add(points [points.Count - 1]); } else { points.ForEach(p => result.Add(p)); } return(result); }
/// <summary> /// Returned int is an index of start point of a found line segment (-1 if nothing is found). /// </summary> /// <param name="pos"></param> /// <param name="distance"></param> /// <returns></returns> int findClosestLineSegment(Vector3 pos, out float distance) { distance = float.MaxValue; int result = GlobalConst.INVALID_ID; if (flags.Count >= 2) { for (int i = 1; i < flags.Count; i++) { Vector3 p1 = flags [i - 1].transform.localPosition; Vector3 p2 = flags [i].transform.localPosition; float d = StageUtilities.DistToLineSegment(p1, p2, pos); if (d < distance) { distance = d; result = i; } } } return(result); }
public void RefreshPointsRightAndLeft() { PointsRight.Clear(); PointsLeft.Clear(); List <Vector3> pointsRightTmp = new List <Vector3> (); List <Vector3> pointsLeftTmp = new List <Vector3> (); Vector3 direction; Vector3 perpenRightDirection; Vector3 perpenLeftDirection; for (int i = 0; i < Nodes.Count - 1; i += 1) { direction = Nodes [i].Position - Nodes [i + 1].Position; direction.Normalize(); perpenRightDirection = StageUtilities.PerpendicularCounterClockwise(Nodes [i].Position, Nodes [i + 1].Position); perpenLeftDirection = StageUtilities.PerpendicularClockwise(Nodes [i].Position, Nodes [i + 1].Position); perpenRightDirection.Normalize(); perpenLeftDirection.Normalize(); pointsRightTmp.Add(Nodes [i].Position + perpenRightDirection * Nodes [i].Width); pointsLeftTmp.Add(Nodes [i].Position + perpenLeftDirection * Nodes [i].Width); pointsRightTmp.Add(Nodes [i + 1].Position + perpenRightDirection * Nodes [i + 1].Width); pointsLeftTmp.Add(Nodes [i + 1].Position + perpenLeftDirection * Nodes [i + 1].Width); } pointsRightTmp = shortenIntersectingLineSegments(pointsRightTmp); pointsLeftTmp = shortenIntersectingLineSegments(pointsLeftTmp); pointsRightTmp = interpolate(pointsRightTmp, BezierCurveFactor); pointsLeftTmp = interpolate(pointsLeftTmp, BezierCurveFactor); PointsRight = pointsRightTmp; PointsLeft = pointsLeftTmp; }