【问题标题】:Rotation to direction vector旋转到方向向量
【发布时间】:2019-11-05 18:19:04
【问题描述】:

好的,所以我得到了一个 Three.js 对象。我可以读取对象的旋转,但我想要一个指向对象旋转方向的 vec3,我该如何获得?

【问题讨论】:

  • Vector3.applyQuaternion(Object3D.quaternion) 不成功?
  • 您在寻找.getWorldDirection吗?
  • 两者都不起作用。 Vector3.applyQuaternion(Object3D.quaternion) 只留下向量 0/0/0。 getWorldDirection() 给出了奇怪的值。
  • 你能用一个活生生的例子来证明这个问题吗?正如您所看到的,Vector3.getWorldDirection() 按预期工作。 jsfiddle.net/91whg4u8
  • 好的,哇。 Vector3.getWorldDirection() 正在工作,对不起,我的错。并感谢您的帮助。

标签: javascript three.js 3d


【解决方案1】:

THREEJS 使用四元数而不是以某种顺序围绕 X、Y、Z 旋转。 您可以提取四元数轴:

export function deconstructQuaternion(q: THREE.Quaternion) {    
if (q.w > 1) {
    q.normalize(); // if w > 1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
}

const angle = 2 * Math.acos(q.w);
const s = Math.sqrt(1 - q.w * q.w); // assuming quaternion normalized then w is less than 1, so term always positive.
const axis = new THREE.Vector3();
if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
    // if s close to zero then direction of axis not important
    axis.set(q.x, q.y, q.z); // if it is important that axis is normalized then replace with x=1; y=z=0;
} else {
    // normalize axis
    axis.set(q.x / s, q.y / s, q.z / s)
}

return {
    axis,
    angle
};
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多