Bhupi Singh
Bhupi Singh

Reputation: 1

How to calculate distance between the two mouse click in three.js?

I can use this code for getting the 3d position of a mouse click but how to get the distance between two mouse clicks. Please can anyone help me.

var vector = new THREE.Vector3();

vector.set(
    ( event.clientX / window.innerWidth ) * 2 - 1,
    - ( event.clientY / window.innerHeight ) * 2 + 1,
    0.5 );

vector.unproject( camera );

var dir = vector.sub( camera.position ).normalize();

var distance = - camera.position.z / dir.z;

var pos = camera.position.clone().add( dir.multiplyScalar( distance ) );

Upvotes: 0

Views: 720

Answers (1)

Hasan
Hasan

Reputation: 2552

You cannot.

You cannot simply unproject a screen coordinate (2D) to world (3D) coordinate using some magic formula.

if you really need 3D coordinates of mouse clicks you have to make sure those clicks hit at least one geometry in the world and you do a raytest to find out exactly at which point the clicks hit your geometries.

Upvotes: 2

Related Questions