Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Divide path in equal lenght segments

Discussion in 'Scripting' started by VacaMarron, May 15, 2020.

  1. VacaMarron

    VacaMarron

    Joined:
    Apr 14, 2017
    Posts:
    5
    Hi there everyone!

    I'm hitting myself against a wall right now, i'm trying to create an algorythm that takes an array of points and divide the resulting path in segments of equal lenght (getting the cutting point as a result). For now i have a the vector, the total lenght measured, and the numer of points i get if i want the lenght of each segment to be X.

    Obviously the problem here is how to measure in situations like this.
    upload_2020-5-15_21-40-26.png
    I thinks this is more like a math problem, but i'm not finding a lot of help searching the internet about any system to actually achieve this.

    I do not want a 100% perfect division, i dont care if the segments deviate a little bit as long as it is not much.

    Thank you for your help and attetion people!!

    PS: If you feel brave, the B part of this problem is to do this with two separated paths and treat them like one, like use the total lenght of both of them even if they do not connect.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    So, let's look at red dot D. When you go to find point E, your first step should be checking the distance from point D to the next corner. If this is <1m, you know you'll have to round the corner. More importantly, by calculating that distance, you know exactly how far you have to round that corner.

    If x = dist(D, corner), then dist(corner, E) = 1 - x. You can enforce the latter with Vector3.MoveTowards, with 1 - x as the distance parameter. From there you can continue to point F as normal.

    That's about as detailed an answer as I can give without seeing the code for your existing attempt.
     
  3. VacaMarron

    VacaMarron

    Joined:
    Apr 14, 2017
    Posts:
    5
    Okay, that was my original idea, but i wasn't sure if it was a GOOD idea
    I'll give it a try and tell/show afterwards, thanks :D
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    It being a good idea is relative to what you need.

    If you need to conserve the information about the corner, it's a bad idea.

    But you already said that you don't care if it deveats from the path a little bit... so it's fits your needs. Making it a good idea.

    Of course the size of the segment (1m in this case) will effect this though. And you can always do the division, run it on several paths, and make sure they look like what you consider acceptable.
     
  5. JavaMcGee

    JavaMcGee

    Joined:
    Nov 8, 2014
    Posts:
    1
    A few weeks late, but I just happen to have the same problem a couple months ago and made a PathToSteps conversion function that might do what you want. I use it place one meter spaced footprints and action-point markers along the path created from a NavAgent.

    Code (CSharp):
    1.  
    2. public static List<Vector3> PathAsSteps(Vector3[] path, float stepLength) {
    3.             List<Vector3> steps = new List<Vector3>();
    4.             if (stepLength <= 0) {
    5.                 throw new ArgumentException("stepLength must be > 0");
    6.             }
    7.             Vector3 p0 = path[0];
    8.             steps.Add(p0);
    9.             int s = 1;
    10.             while (s < path.Length) {
    11.                 // work on path segment of p0 to p1
    12.                 Vector3 p1 = path[s];
    13.                 float segmentLength = Vector3.Magnitude(p1 - p0);
    14.                 // bite off a stepSize (eg 1 meter) piece of the segment to get the next step
    15.                 if (segmentLength >= stepLength) {
    16.                     p0 = Vector3.MoveTowards(p0, p1, stepLength);
    17.                     steps.Add(p0);
    18.                 } else {
    19.                     float carryover = stepLength - segmentLength;
    20.                     p0 = p1;
    21.                     s++;
    22.                     if (s < path.Length) {
    23.                         p1 = path[s];
    24.                         p0 = Vector3.MoveTowards(p0, p1, carryover);
    25.                         steps.Add(p0);
    26.                     }
    27.                 }
    28.             }
    29.             return steps;
    30.         }
    31.