Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算任意轴上对象的世界上方向向量(ThreeJS)_Javascript_Three.js_Calculus - Fatal编程技术网

Javascript 计算任意轴上对象的世界上方向向量(ThreeJS)

Javascript 计算任意轴上对象的世界上方向向量(ThreeJS),javascript,three.js,calculus,Javascript,Three.js,Calculus,我很难找到物体在任意轴上的绝对旋转 到目前为止,我已经能够得到我的物体的相对旋转。为了获得相对旋转,我在面向选定轴的对象后面放置了一个平面。通过在这个平面上进行光线投射,我可以通过使用atan2检索3D坐标来计算角度 获取对象相对旋转的代码: let planeCrossObjectDir = planeNormal.clone().cross(this.objectUpDirection).normalize(); let projectedPoint = new THREE.Vector3(

我很难找到物体在任意轴上的绝对旋转

到目前为止,我已经能够得到我的物体的相对旋转。为了获得相对旋转,我在面向选定轴的对象后面放置了一个平面。通过在这个平面上进行光线投射,我可以通过使用
atan2
检索3D坐标来计算角度

获取对象相对旋转的代码:

let planeCrossObjectDir = planeNormal.clone().cross(this.objectUpDirection).normalize();
let projectedPoint = new THREE.Vector3();
rayPlane.projectPoint(intersection.point, projectedPoint);
let centerProjected = new THREE.Vector3();
rayPlane.projectPoint(this.raycastPlane.position, centerProjected);

let u1 = this.objectUpDirection.dot(projectedPoint);
let v1 = planeCrossObjectDir.dot(projectedPoint);
let u2 =  this.objectUpDirection.dot(centerProjected);
let dotv2 = planeCrossObjectDir.dot(centerProjected);

let uvCoord = new THREE.Vector2(u1, v1).sub(new THREE.Vector2(u2, dotv2));

var theta = Math.atan2(uvCoord.y, uvCoord.x); // range [-PI, PI]
theta *= 180 / Math.PI; // range [-180, 180]
if (theta < 0) theta = 360 + theta; // range [0, 360]
let objectsRotation = Math.round(360 - theta); // invert rotation
让planeCrossObjectDir=planeNormal.clone().cross(this.objectUpDirection).normalize();
设projectedPoint=new THREE.Vector3();
光线平面投影点(交点、投影点);
设centerProjected=new THREE.Vector3();
光线平面.projectPoint(this.raycastPlane.position,centerProjected);
设u1=this.objectUpDirection.dot(projectedPoint);
设v1=planeCrossObjectDir.dot(投影点);
设u2=this.objectUpDirection.dot(中心投影);
设dotv2=planeCrossObjectDir.dot(中心投影);
设uvCoord=new THREE.Vector2(u1,v1).sub(new THREE.Vector2(u2,dotv2));
var theta=Math.atan2(uvCoord.y,uvCoord.x);//范围[-PI,PI]
θ*=180/Math.PI;//范围[-180,180]
如果(θ<0)θ=360+θ;//范围[0360]
让objectsRotation=Math.round(360θ);//反向旋转
我想通过更改
planeCrossObjectDir
并使用平面的世界上方向向量而不是
this.objectUpDirection
来计算绝对旋转的相同原理

然而问题是:我不知道如何计算这个

我画了一张图来简化事情/澄清:
我认为你走了很长的路。对象的
matrixWorld
属性应包含最终确定的世界值,包括位置、旋转和缩放。可以通过两种不同的方式获取旋转组件:

作为矩阵:

var rotationMatrix = new THREE.Matrix4().extractRotation(yourObject.matrixWorld);
// rotationMatrix will now contains the rotation component of your object's world matrix
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3();
yourObject.matrixWorld.decompose(position, quaternion, scale);
// quaternion now contains your object's world rotation as a quaternion
作为四元数:

var rotationMatrix = new THREE.Matrix4().extractRotation(yourObject.matrixWorld);
// rotationMatrix will now contains the rotation component of your object's world matrix
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3();
yourObject.matrixWorld.decompose(position, quaternion, scale);
// quaternion now contains your object's world rotation as a quaternion
应用于上方向向量:

var rotationMatrix = new THREE.Matrix4().extractRotation(yourObject.matrixWorld);
// rotationMatrix will now contains the rotation component of your object's world matrix
var position = new THREE.Vector3();
var quaternion = new THREE.Quaternion();
var scale = new THREE.Vector3();
yourObject.matrixWorld.decompose(position, quaternion, scale);
// quaternion now contains your object's world rotation as a quaternion
然后,可以根据使用的方法更新上方向向量:

var up1 = new THREE.Vector3(0, 1, 0).applyMatrix4(rotationMatrix).normalize();

如果您尝试这两种路径,您将看到它们得到相同的结果(在JavaScript的范围内)


如果您有问题,或者我误解了您的问题,请留下评论。

我认为您误解了我的问题(可能是我的错,因为很难解释;p)。你的答案给了我物体的上方向向量。开始时,这似乎是正确的,但如果我向下旋转对象(Y:180度),上方向向量也将指向下方。你可以说答案只是简单地取世界上的向量(
THREE.Vector3(0,1,0)
),但我希望X&Z也被应用(Y朝向世界上的向量)。这当然取决于旋转的轴。