Search Unity

Coordinate / Distance Math Question

Discussion in 'Scripting' started by dacoursey, Mar 30, 2017.

  1. dacoursey

    dacoursey

    Joined:
    Dec 17, 2016
    Posts:
    40
    HI all, I don't know exactly how to explain what I want to do so I made a picture.

    I have points A and B and I need to create an array of semi-random points between the two, with loose tolerance. Simply put, I want a squiggly line between A and B. I have no problem making the line, but I was hoping to find an easier way to determine if points are between the existing points, or some magic method that already does this. :) Points A and B can be anywhere on the screen.

    Coords.png

    Thanks for looking!

    David
     
  2. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    My first thought is that perhaps you could get the distance from the point to both A and B and then add those together and compare it to the distance between A and B themselves. The larger the difference between those 2 results the further the point is from being in a direct line between A and B. You could set some arbitrary limit such as the Sum of the distances from A and B must be < 120% of the distance between A and B themselves and tweak the tolerance by just changing the %.

    The method would allow the point to be a little behind A or B instead of between the 2 but you could prevent that by also checking that it's distance to either A or B individually is not greater than the distance between A and B.
     
    Last edited: Mar 30, 2017
  3. Grizmu

    Grizmu

    Joined:
    Aug 27, 2013
    Posts:
    131
    I enjoyed the problem and decided to give it a shoot myself.

    The uploaded script allows customization of the randomization shape and its start/end points. A word of warning though: I haven't tested it much, so please let me know if there are any problems with it.

    #Edit
    Reuploaded with new line generation method.



     

    Attached Files:

    Last edited: Mar 31, 2017
  4. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    If you're explicitly trying to make an arbitrary squiggly line between two points, I'd recommend the midpoint displacement method. Only adjustment you'd need to make from typical versions presented on the internet would be to figure out the vector that is perpendicular to the one between the two lines, and use that as the displacement vector, rather than just altering the y-value (since that assumes a horizontal line). Fortunately, finding a perpendicular vector in 2D is as easy as swapping the x/y values and negating one of them. In 3D, it requires picking a vector that is perpendicular to the surface you're working in (the normal of a plane, often just Vector3.forward or Vector3.back), and then taking the cross product of the plane's normal vector with the input line to get an output line that is perpendicular to the input vector.
     
  5. dacoursey

    dacoursey

    Joined:
    Dec 17, 2016
    Posts:
    40
    Griz - That's pretty intense, and impressive. I'll give it a shot and let you know.

    Andy - No swirly picture?? :D I'm going to try that out as well.

    Thanks to both of you, I will report back as I make some progress.

    david