本文介绍了旋转平面使其平行于另一个平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个平面,第一个平面由世界空间坐标中的 3 个点给出,它是使用自定义 Geometry() 和一个面(换句话说:它只是一个三角形)构建的在 3D 空间中).第二个是由 PlaneBufferGeometry() 构造的,它与 xy 平面平行.如何旋转红色使其与第一个平行???

I've got 2 planes, the first one is given by 3 points in world space coordinates, and it's constructed using custom Geometry() and one face (in other words: it's just a triangle in 3D space).The second one is constructed from PlaneBufferGeometry() which is parallel to the xy plane.How to rotate the red one to make it parallel to the first one ???

插图:

我的算法如下:

  1. 通过平面的法向量获取旋转轴
  2. 计算它们之间的角度
  3. 使用轴和角度创建并应用到红色平面旋转矩阵

在代码中:

var v1, v2, v3, geom, mat, plane3mesh, redPlaneMesh;
geom = new THREE.Geometry();
geom.vertices.push(v1, v2, v3);
var face = new THREE.Face3(0, 1, 2);
geom.faces.push(face);
geom.computeFaceNormals();
geom.computeVertexNormals();
plane3mesh = //...create mesh

geom = new THREE.PlaneBufferGeometry(700, 700);
redPlaneMesh = //...create red plane mesh
var redPlaneNormal = new THREE.Vector3(0, 0, 1);
var axis = face.normal.clone().cross(redPlaneNormal);
var angle = Math.acos(face.normal.dot(redPlaneNormal) ); // lengths are unit so no need for division

var rotationWorldMatrix = new THREE.Matrix4();
rotationWorldMatrix.makeRotationAxis(axis.normalize(), angle);
redPlaneMesh.matrix.multiply(rotationWorldMatrix);
redPlaneMesh.rotation.setFromRotationMatrix(redPlaneMesh.matrix);

但它给出了一个错误的结果,这是这样的:

But it gives a wrong result, which is this:

我的算法有什么问题,有哪些不同的解决方案?

What is wrong with my algorithm and what are the different solutions?

推荐答案

acos 函数给出绝对角度作为结果.因此,例如,结果 Pi/4 可能意味着以 Pi/4 旋转 A"和以 Pi/4 旋转 B".

acos function gives absolute angle as result. So, for example, result Pi/4 may mean both "rotate A by Pi/4" and "rotate B by Pi/4".

您的轴是通过第一个和第二个法线计算的,因此 N1、N2 和 Ax 向量形成右手"三元组.似乎在这种情况下,您总是必须否定角度符号才能将所需的法线转换为其他法线.

Your axis is calculated through the first and the second normals, so N1, N2 and Ax vectors form "right-handed" triplet. It seems that in this case you always have to negate angle sign to transform needed normal into other one.

这篇关于旋转平面使其平行于另一个平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 04:24