Create a directional vector with 2 position vectors

Hello,

I was wondering if it exists a function in Babylon to create a new Vector3 with two position vectors as arguments.

For example if I click somewhere in the world I want to get back the position of my click and with the position of my active camera create a new directional Vector3 (from the camera position to the click position, get with pickedPoint) to use with cameraDirection.

Best regards,
Jez

I find the solution :

window.addEventListener(“click”, function () {
// We try to pick an object
var pickResult = scene.pick(scene.pointerX, scene.pointerY);
console.log(pickResult);
//let vec3 = camera.getTarget().subtract(camera.position)
let vec3 = pickResult.pickedPoint.subtract(camera.position).normalize();
camera.cameraDirection = new BABYLON.Vector3(vec3.x, 0, vec3.z); // I want to avoid to move the Y axis

            }); 

So what I needed was the normalize but I don’t understand what it’s doing.

It is creating a unit vector from the vector, ie one in the same direction with magnitude 1.

a.normalize() = a/||a||

a = (2, 3, 6)

||a||2 = 22 + 32 + 62 = 4 + 9 + 36 = 49
||a|| = 7

(2, 3, 6).normalize() = (2/7, 3/7, 6/7)

3 Likes

Hello JohnK,

I got it! thank you very much for your help !!!

So finally it doesn’t work because I just get a vector with magnitude 1 but what I want is just move the camera to the clicked position so the magnitude can change.

Best Regards,
Jez

Yes, if A has position vector a, and B has position vector b then the vector from A to B is b - a and don’t normalize.

okai okai thank you very much for your help :slight_smile:

1 Like