node.js - 如何在Three.js中从欧拉得到原始旋转四元数

标签 node.js math three.js quaternions euler-angles

我有一个根据旋转四元数计算的欧拉。我想存储欧拉,然后将其加载到其他地方来计算原始四元数。我怎样才能实现这一目标?

const rot = new THREE.Quaternion(-0.00122, 0.98602, -0.15773, -0.05371);
const {x, y, z} = new THREE.Euler().setFromQuaternion(
        rot,
        'YXZ',
      );
 // How to make rot1 euqal to rot (as close as possible)?
 const rot1 = new THREE.Quaternion().setFromEuler(new THREE.Euler(x, y, z));
 console.log(rot); // Quaternion {_x: -0.00122, _y: 0.98602, _z: -0.15773, _w: -0.05371}
 console.log(rot1); // Quaternion {_x: 0.0012199961779640698, _y: -0.9860197701687391, _z: -0.15688998318156033, _w: 0.056116464812051764}
 console.log(rot1.inverse()); // Quaternion {_x: -0.0012199961779640698, _y: 0.9860197701687391, _z: 0.15688998318156033, _w: 0.056116464812051764}

方向错误,而且误差似乎有点大。 这是fiddlejs

最佳答案

您的脚本存在两个问题。

1) 从欧拉角创建四元数时,欧拉旋转轴顺序“YXZ”丢失:

const rot1 = new THREE.Quaternion().setFromEuler(new THREE.Euler(x, y, z, 'YXZ'));

2) 四元数和欧拉角的精度不同。使用函数将欧拉角转换为四元数时获得较小的精度:

 // Update decimal precision
 const decimalPrecision = 100000;
 function updatePrecision(input) {
    return Math.round(input*decimalPrecision) / decimalPrecision;
 }

 const rot1SmallerPrecition = {
    x: -updatePrecision(rot1.x),
    y: -updatePrecision(rot1.y),
    z: -updatePrecision(rot1.z),
 }

这里是fiddle有更正

关于node.js - 如何在Three.js中从欧拉得到原始旋转四元数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60472521/

相关文章:

math - 霍夫曼代码中字符的单位代码的条件?

r - 日期时间 - 确定多个(n)日期时间范围是否在 R 中相互重叠

windows - node.js http 服务器作为 Windows 服务

java - 四舍五入,同时保持尾随零

javascript - 设置 three.js 对象的 matrixWorld 属性

javascript - ThreeJS 垃圾收集问题

three.js - Threejs 和 3D Tiles(来自 Cesium)

node.js - Passport Github 策略不起作用并抛出错误

node.js - console.timeLog() 在 Jest 测试中不起作用

node.js - 在 Node js : Zapier 中解析 XML