2

I have 4 vectors pointing to 4 corners of a square. I'd like to detect if they are in a certain proximity to another vector pointing somewhere else and if so, act accordingly. I found an algorithm said to be measuring closest distance between two line segments, but it's not working properly I think:

public class Test : MonoBehaviour {
    public void FixedUpdate() {
        var source = new Vector3(0, 0, 0);
        var target = new Vector3(0, 0, 4);
        var otherSource = new Vector3(1.6f, 0, -0.5f);

        foreach (var dir in new[] {
            new Vector3(source.x + 1, source.y, source.z + 1),
            new Vector3(source.x - 1, source.y, source.z + 1),
            new Vector3(source.x + 1, source.y, source.z - 1),
            new Vector3(source.x - 1, source.y, source.z - 1)
        }) {
            var A = source;
            var B = otherSource;
            var a = dir - A;
            var b = target - B;

            var n = Vector3.Cross(a, b);
            var u = Vector3.Cross(n, A - B) / Vector3.Dot(n, n);

            var AA = A - a * Vector3.Dot(b, u);
            var BB = B - b * Vector3.Dot(a, u);

            Debug.DrawRay(A, a, Color.blue);
            Debug.DrawRay(B, b, Color.red);
            Debug.DrawLine(AA, BB, Color.green);
        }
    }
}

Now, if you run it, you'll see something like this:

enter image description here

I was hoping to see four green lines, but there are none. If I hovewer move the otherSource vector a bit up, then I see this:

enter image description here

So a bit better, but still not what I'm looking for. Any help adjusting this algorithm?

Marek M.
  • 3,799
  • 9
  • 43
  • 93
  • 2
    `Vector3.Distance` is used to find distance between two vectors – Programmer Sep 19 '17 at 21:37
  • You can also subtract two vectors and get the magnitude squared which would be fine for comparisons. – George Sep 19 '17 at 22:37
  • 1
    What is *another vector pointing somewhere else*? And *measuring closest distance between two vectors*, do you mean *distance between two points* or *two segments/lines defined by two points*? – zwcloud Sep 20 '17 at 04:02
  • If you you are trying to get the distance between two 3D lines, see [this](https://stackoverflow.com/questions/38637542/finding-the-shortest-distance-between-two-3d-line-segments) and [this](https://math.stackexchange.com/questions/13734/how-to-find-shortest-distance-between-two-skew-lines-in-3d). – zwcloud Sep 20 '17 at 04:08
  • @Programmer, I expressed myself incorrectly - I meant line segments. In this case if I use `Vector3.Distance` I'm getting values: `2.384186E-07` and `6.743496E-07` which are incorrect, because they are just too small, and not varying enough (why 2 distinct values instead of 4?). – Marek M. Sep 20 '17 at 06:47
  • @zwcloud - implementation looks mighty complicated at first sight, but I'll try it later. Thanks! – Marek M. Sep 20 '17 at 06:52

1 Answers1

0

So it seems I misunderstood what actually had to be done. To get the info I needed (the distance between a point and a ray) I used this code:

public static float PointToRayDistance(Vector3 point, Vector3 origin, Vector3 target) {
    var ray = new Ray(origin, target - origin);
    var cross = Vector3.Cross(ray.direction, point - ray.origin);

    return cross.magnitude;
}

Hopefully it will help someone.

I'm leaving the original question's title, because I guess a lot of newbies may look for the same stuff I did, using the same words :)

Marek M.
  • 3,799
  • 9
  • 43
  • 93