Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 243
user3490600 Avatar asked Apr 03 '14 18:04

user3490600


People also ask

How do you calculate Euclidean distance in 3d?

The distance formula states that the distance between two points in xyz-space is the square root of the sum of the squares of the differences between corresponding coordinates. That is, given P1 = (x1,y1,z1) and P2 = (x2,y2,z2), the distance between P1 and P2 is given by d(P1,P2) = (x2 x1)2 + (y2 y1)2 + (z2 z1)2.


1 Answers

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.

like image 189
gaitat Avatar answered Oct 10 '22 03:10

gaitat