Retrieving X , Y and Z coordinates of model

Hi everyone!
I’ve been trying to gauge the distance between two points. As I googled it first I come up with this link.
[https://threejs.org/docs/#api/en/core/Raycaster]
However, I can only get X and Y coordinates but I also need to get Z since I try to calculate distances between 2 points in 3d model

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

function onMouseMove( event ) {

// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components

mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

}
I tried to create a Vector3D object but still can’t manage to get the Z value since the event object does not have a function for getting the Z

I’d be so grateful if anyone could help me with the issue

2 points in 3D space or on the screen? :thinking:

@ibrahimunal
Hi!
Are you looking for something like this? [SOLVED]Mesure distance between two points on a object by clicking

2 Likes

in 3D space… I can only get X and Y coordinates but not Z

But distance is a number - not a vector - it doesn’t have x, y, or z. :thinking:

See link @prisoner849 shared. To get distance between 2 points in 3D it’s usually enough to subtract these points:

const positionA = obj.position;
const positionB = obj.position;
const difference = new Three.Vector3().subVectors(positionB, positionA);

const distanceTotal = difference.length();

const distanceXAxis = Math.abs(difference.x);
const distanceYAxis = Math.abs(difference.y);
const distanceZAxis = Math.abs(difference.z);
3 Likes

This was exactly what I’ve been looking for . I was also thinkig to add a mesh at the endpoints of the line. This is a brilliant solution. I was thinking of getting to coordinates of 2 points and calculate it by myself but thats much better! Thanks a lot!

1 Like

@prisoner849 codepen solution helped me to get the solutions. Thanks btw

yo anybody got anything on how to make a basic camera movement based on both the “Sprite” mesh item’s movement (which I also need help with making it move with WASD) and the position of the mouse. Just like reply w/ instructions on how to make a locked mouse that can move around the camera. Basically I just need like a first person engine or smthn

There is also a handy method on vector:

somePoint.distanceTo( anotherPoint )

@check
One easy way if you’re using orbitcontrols…

When you move the sprite like..
sprite.position.x += 1;
Also move the camera by the same amount: camera.position.x += 1;

and the controls.target
controls.target.x += 1;

This way the camera will follow the sprite, the camera will stay pointing at the sprite… but you will still be able to rotate the camera.

There is also this example which is more complex but covers simple collision/wall sliding and FPS style movement: