public List <Vector2> CalculatePath(Vector2 lastDrawnPoint, Vector2 mousePosition) { path.Clear(); //Use start point of previous segment as a reference point of new segment to ensure smooth joint previousSegmentStart = currentSegmentStart; currentSegmentStart = CreateBezierAnchorPoint(previousSegmentStart, lastDrawnPoint); currentSegmentEnd = CreateBezierAnchorPoint(currentSegmentStart, mousePosition); for (float percent = 0f; percent <= 1f; percent += 0.01f) { Vector2 point = GetCubicBezierPoint(currentSegmentStart.position, currentSegmentStart.controlPointAfter, currentSegmentEnd.controlPointBefore, currentSegmentEnd.position, percent); path.Add(point); } return(path); }
private BezierAnchorPoint CreateBezierAnchorPoint(BezierAnchorPoint previousBezierAnchorPoint, Vector2 position) { BezierAnchorPoint bezierPoint = new BezierAnchorPoint(); bezierPoint.position = position; if (previousBezierAnchorPoint == null) { bezierPoint.controlPointBefore = position; bezierPoint.controlPointAfter = position; } else { //setup control points symmetrically to ensure smooth joint bezierPoint.controlPointBefore = Vector2.Lerp(previousBezierAnchorPoint.controlPointAfter, position, 0.5f); bezierPoint.controlPointAfter = position + (position - bezierPoint.controlPointBefore); } return(bezierPoint); }
public void StartNewPath(Vector2 pathStartPosition) { previousSegmentStart = CreateBezierAnchorPoint(null, pathStartPosition); currentSegmentStart = CreateBezierAnchorPoint(null, pathStartPosition); currentSegmentEnd = CreateBezierAnchorPoint(null, pathStartPosition); }