three.js:2 x方向向量到3 x旋转或矩阵

时间:2017-09-07 18:55:58

标签: javascript three.js quaternions

我有一个由2个单位向量编码的坐标系统(我称之为'Ox'和'Oy'),1个插入点和近似是'Oz'向量,似乎总是{0 0 1}。

例如(2次轮换后):

Ox:{x: 0.956304755963036, y: -0.292371704722735, z: 0}
Oy:{x: -0.250611464938861, y: -0.819713166317425, z: 0.51503807491006}
move:{x: 889.08282028218, y: -845.071708420642, z: -396.787982804802}

问题是:如何在my.js中将my_mesh与此系统对齐?

我无法弄清楚它是如何投影的,但感谢this answer我来到了这里:

if( 'Ox' in align && 'Oy' in align ){
    var mx = new THREE.Matrix4().lookAt( align.Ox , new THREE.Vector3(0,0,0), new THREE.Vector3(0,1,0) );
    var my = new THREE.Matrix4().lookAt( align.Oy , new THREE.Vector3(0,0,0), new THREE.Vector3(1,0,0) );
    mx.multiply(my);
    var qt = new THREE.Quaternion().setFromRotationMatrix(mx);

    my_mesh.matrix.makeRotationFromQuaternion(qt);
}
// move
mesh.matrix.setPosition( align.move );
mesh.matrixAutoUpdate = false;

似乎接近解决方案了?

修改

在West回答之后,我意识到我不应该称这个'坐标系',因为向量不是正交的。

以下是旋转后的一些记录('z30'表示围绕Oz旋转30°)..我称之为Oz的总是设置为{x: 0, y: 0, z: 1}

// base
Ox: {x: 1, y: 0, z: 0}
Oy: {x: 0, y: 1, z: 0}
move: {x: 6.42995404746696, y: -500, z: -268.028464077285}

// base x45
Ox: {x: 1, y: 0, z: 0}
Oy: {x: 0, y: -0.707106781186548, z: 0.707106781186547}
move: {x: 6.42995404746696, y: -73.2233047033605, z: -444.805159373921}

// base y60
Ox: {x: 0.500000000000001, y: 0, z: 0.866025403784438}
Oy: {x: 0, y: 1, z: 0}
move: {x: 6.42995404746766, y: -500, z: -268.028464077285}

// base z30
Ox: {x: 0.866025403784439, y: 0.5, z: 0}
Oy: {x: 0.5, y: -0.866025403784439, z: 0}
move: {x: -118.570045952533, y: -33.4936490538881, z: -268.028464077284}

// base z30 x45
Ox: {x: 0.866025403784439, y: 0.353553390593274, z: -0.353553390593273}
Oy: {x: 0.5, y: -0.612372435695795, z: 0.612372435695794}
move: {x: -118.570045952533, y: -96.9068910760492, z: -421.121573001233}

// base z30 x45 y60
Ox: {x: 0.739198919740117, y: 0.353553390593274, z: 0.573223304703363}
Oy: {x: 0.28033008588991, y: 0.612372435695795, z: -0.739198919740117}
move: {x: -63.6525674250102, y: -403.093108923947, z: -83.228734142255}

1 个答案:

答案 0 :(得分:0)

您希望将对象的方向设置为与由三个正交单位长度向量oxoyoz指定的旋转坐标系对齐。

如果您不了解oz,则可以进行计算:

var oz = new THREE.Vector3(); // create once and reuse

oz.crossVectors( ox, oy ).normalize();

然后,由于您似乎想直接设置mesh.matrix,请执行以下操作:

mesh.matrix.makeBasis( ox, oy, oz ).setPosition( new_position );

mesh.matrixAutoUpdate = false; // prevent matrix from being overwritten

three.js r.87