Distance bugs with Vector3.Distance

So basically, I found a bug in Vector3, and I know how to reproduce it. If I’m wrong, you can say “I told you so”. Eric seems to be certain there couldn’t possibly be a bug. I will fight you on this Eric :rage::wink:

Alright lets begin the steps to reproduce…

Step 1: make an object.
Step 2: Add a collider, rigidbody, and this script to the object:

using UnityEngine;
using System.Collections;

public class reproductionsteps : MonoBehaviour
{
    private Vector3[] vectors;

    void Start()
    {
        vectors = gameObject.GetComponent<MeshFilter>().mesh.vertices;
    }

    void OnCollisionEnter(Collision collision)
    {
        for (int x = 0; x < vectors.Length; x++)
        {
            Debug.Log(Vector3.Distance(vectors[x], gameObject.transform.InverseTransformPoint(collision.contacts[0])));
        }
    }
}

Step 3: make sure the object has a decent sized mesh with many vertices.
Step 4: cause a collision, and look at the strange debug log results!

If I’m right, Eric owes me a cookie! :wink:

For starters this is not an issue with Vector3.Distance, your title implies that it is.

And the reason why the contactPoint and the vertice seem so far apart is because the contactPoint is in world space, and the vertices is in local space relative to the Mesh. It does not take into account any scaling, rotation, or translation of the mesh at all.

You need to transform those points into world space before comparing.

If you take the Transform of the GameObject that the Mesh is attached to, and you can call ‘Transform.TransformPoint’ on the vertices to get this world coordinate:

1 Like

There are no bugs with Vector3.Distance (or any other math functions); this is literally the code used by Unity:

public static float Distance (Vector3 a, Vector3 b)
{
    Vector3 vector = new Vector3 (a.x - b.x, a.y - b.y, a.z - b.z);
    return Mathf.Sqrt (vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
}

–Eric

Is the source code of mathf, vector etc avalible somewhere?

You can inspect the UnityEngine assembly and anything that’s not native code is readable, same as all other .NET/Mono DLLs.

–Eric

You need balls to suspect a 3D Engine creator company to make a mistake on a simple Math function such as Vector3.Distance. :smile:

2 Likes

Eh If you read my post I said that

So its not the Vector3 thats the issue, its the contact point. But I figured it out, it was because it had to be in local coordinates and it was in global. Thanks though.

1 Like

Perhaps he was referring to the subject, and…

Eh sorry for the confusion. I figured it out though, lets let this thread die.

Bump because I found a way to prove Vector3 is bugged. Try it for yourself :smile:

First post. Look at it!

It’s not bugged. (I assume you mean Vector3.Distance, though the Vector3 class in general has no bugs either.) Did you even look at the code I posted? The code extracted from UnityEngine.dll that Unity is literally running? Nah, that was a rhetorical question, you obviously didn’t look at it. :wink: Here it is again:

public static float Distance (Vector3 a, Vector3 b)
{
    Vector3 vector = new Vector3 (a.x - b.x, a.y - b.y, a.z - b.z);
    return Mathf.Sqrt (vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
}

Kindly demonstrate any bugs in that code. You’re really going to claim that somehow Unity can’t manage the most basic possible math? You don’t want to “fight me” on this. :wink: (It’s not that Unity has no bugs, far from it, but they’re all edge cases of some kind. 100% of these “basic Unity math/logic bug!!!” topics have turned out to be user error, without fail, and this one is no different. If it was actually true, no Unity games would ever be able to run. Your example doesn’t show any bugs, but rather a lack of understanding of what you’re doing. Print out actual values for all steps, don’t make vague claims like “look at strange debug log results”.)

Also, please revert your edit and post a new message instead, since you’ve now made a big mess of this topic. Once you’ve done that, I’ll delete this message and re-post it as a proper response.

–Eric

1 Like

Sigh, it turns out it was my model. It was scaled up by 100, which was why the vertices were within a few thousanths of each other. Case closed.