找到旋转点的角度,使其面向3d空间中的另一个点

Dar*_*man 5 javascript 3d rotation three.js

说我有两个向量:

V1 = {x:3.296372727813439,y:-14.497928014719344,z:12.004105246875968}

V2 = {x:2.3652551657790695,y:-16.732085083053185,z:8.945905454164146}

如何确定v1需要旋转的角度才能直接观察v2?

说英语:说我确切地知道我在太空中的确切位置,以及另一个人在太空其他地方的确切位置......数学上,我怎么能弄清楚用手指指向它们的角度?

这是我的轴的样子

我当前(不正确)的公式

v2.x -= v1.x; // move v2 (vector 2) relative to the new origin
v2.y -= v1.y;
v2.z -= v1.z;

v1.x = 0; // set v1 (vector 1) as the origin
v1.y = 0;
v1.z = 0;

var r = Math.sqrt(Math.pow(v2.x,2) + Math.pow(v2.y,2) + Math.pow(v2.z,2));
var ? = Math.acos((Math.pow(v2.x,2) + Math.pow(v2.z,2))/(r*Math.sqrt(Math.pow(v2.x,2) + Math.pow(v2.z,2))));
var ? = Math.acos(v2.x/Math.sqrt(Math.pow(v2.x,2) + Math.pow(v2.z,2)));
Run Code Online (Sandbox Code Playgroud)

然后我用theta和phi旋转v1.

v1.rotation.y = ?;
v2.rotation.x = ?;
Run Code Online (Sandbox Code Playgroud)

但这给了我错误的轮换.

θ= 0.6099683401012933

φ= 1.8663452274936656

但是,如果我使用THREE.js并使用lookAt函数,它会吐出这些旋转,这样可以很好地工作:

y/θ:-0.24106818240525682

x/φ:2.5106584861123644

感谢您提前获得所有帮助!我不能在我的最终结果中使用THREE.js,我正在尝试使用纯粹的vanilla JS,因此我可以将其移植到另一种语言中.

Ima*_*man 2

正确的方程是:

 var dx = v2.x-v1.x; //-0.93
 var dy = v2.y-v1.y; //-31.22
 var dz = v2.z-v1.z;
 var rxy = Math.sqrt( Math.pow(dx,2) + Math.pow(dy,2) );
 var lambda = Math.atan(dy/dx);
 var phi = Math.atan(dz/rxy)
Run Code Online (Sandbox Code Playgroud)

上面的公式philambda需要根据你的向量所在的四分之一进行调整。我让你很容易:

 //if you do the calculations in degrees, you need to add 180 instead of PI
 if (dx < 0) phi = phi + Math.PI;
 if (dz < 0) lambda = -1 * lambda;
Run Code Online (Sandbox Code Playgroud)