javascript - 旋转到方向向量
问题描述
好的,所以我得到了一个 Three.js 对象。我可以读取对象的旋转,但我想要一个指向对象旋转方向的 vec3,我如何得到它?
解决方案
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
};
}
推荐阅读
- mysql - 在 MySQL 数据库表中使用 While 循环插入
- angular - 角度路由器导航触发 POPSTATE IMPERATIVE 和 HASHCHANGE
- visual-studio-2019 - 如何将 Visual Studio 2019 配置为保持 16.4 服务基准并避免更新到 16.5?
- javascript - Vue无法读取null的属性'id'
- reactjs - React Native:热重载/快速刷新到底发生了什么?
- c# - 在 ASP.Net Core 3.0 中多次读取正文
- typescript - 为什么 array.length + 1 返回 NaN
- python - tensorflow 多维索引
- android - ViewPager2 中的片段未填充高度
- bash - Launchctl 不启动 netcat shell