Three.js - How can I calculate the distance between two 3D positions?

user3490600 picture user3490600 · Apr 3, 2014 · Viewed 23k times · Source

I've already tried searching several different things on Google. Doesn't seem like I'm able to find anything. Thought I might as well upload a question to Stack Overflow.

Thanks!

Answer

gaitat picture gaitat · Apr 3, 2014

Where v1 and v2 are of type THREE.Vector3:

function distanceVector( v1, v2 )
{
    var dx = v1.x - v2.x;
    var dy = v1.y - v2.y;
    var dz = v1.z - v2.z;

    return Math.sqrt( dx * dx + dy * dy + dz * dz );
}

Update: In the r74 release of three.js the method .distanceTo( v ) can be used.