Reputation: 265
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!
Upvotes: 19
Views: 33383
Reputation: 12632
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.
Upvotes: 65
Reputation: 104763
In three.js, to calculate the distance between two 3D positions, use the Vector3.distanceTo()
method:
const distance = vec1.distanceTo( vec2 );
three.js r.74
Upvotes: 32
Reputation: 49
In Javascript:
function dist(x0,y0,z0,x1,y1,z1){
deltaX = x1 - x0;
deltaY = y1 - y0;
deltaZ = z1 - z0;
distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
return distance;
}
Upvotes: 3