【问题标题】:Why does my rotation go awry when I change the scale of a Three.js mesh?当我更改 Three.js 网格的比例时,为什么我的旋转会出错?
【发布时间】:2012-07-23 10:47:26
【问题描述】:

我想围绕世界 x 和 y 轴旋转一个球体。我用这段代码成功地做到了:

// Update ball rotation.
var tempMat = new THREE.Matrix4();
tempMat.makeRotationAxis(new THREE.Vector3(0,1,0), stepX/ballRadius);
tempMat.multiplySelf(ballMesh.matrix);
ballMesh.matrix = tempMat;
tempMat = new THREE.Matrix4();
tempMat.makeRotationAxis(new THREE.Vector3(1,0,0), -stepY/ballRadius);
tempMat.multiplySelf(ballMesh.matrix);
ballMesh.matrix = tempMat;
ballMesh.rotation.getRotationFromMatrix(ballMesh.matrix);

但是,当我以任何方式远离 (1,1,1) 缩放 ballMesh 时,旋转会以一种难以描述的方式出错。我在这里提出了一个 jsfiddle 示例:

http://jsfiddle.net/pxTTv/26/(使用方向键旋转)

如果将比例(在 jsfiddle 代码中表示)改回 (1,1,1),它会按我的预期工作。

这是什么原因造成的,我该如何解决?

【问题讨论】:

    标签: javascript matrix webgl three.js rotational-matrices


    【解决方案1】:

    您将 scale 参数留在了对 vector.getRotationFromMatrix( matrix, scale ) 的调用之外。

    另外,最好不要弄乱对象矩阵——除非你真的知道自己在做什么。相反,只需设置对象的旋转、位置和缩放,然后让库更新矩阵。

    在您的情况下,您正在覆盖对象的矩阵,但幸运的是,它在渲染调用中被重新计算。

    这是一个更正的小提琴:http://jsfiddle.net/pxTTv/27/

    【讨论】:

    • 太好了,谢谢。另外,我真的想听从你的建议,不要弄乱对象矩阵——这会让我的事情变得简单得多!不幸的是,我无法以这种方式完成我想要的旋转。有什么提示吗?
    • 我在上面发布的小提琴向您展示了如何在无需直接设置对象矩阵的情况下完成您想要的旋转。
    • 我现在明白了。再次感谢;这很有启发性。
    猜你喜欢
    • 2015-05-09
    • 2019-05-22
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2021-04-28
    • 1970-01-01
    • 2014-12-06
    相关资源
    最近更新 更多