【问题标题】:Why does this cube not rotate around its own axes?为什么这个立方体不绕自己的轴旋转?
【发布时间】:2014-05-20 10:31:02
【问题描述】:

我有一个立方体,我想通过键盘输入在 3D 空间中围绕它自己的轴旋转。立方体仍在围绕世界轴旋转。

这是我的代码:

var rotation_matrix_y, rotation_matrix_x, rotation_matrix_z;

var geometry = new THREE.CubeGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial({color: 0x00ff00, wireframe: true});
var cube = new THREE.Mesh(geometry, material);
var axisHelper = new THREE.AxisHelper( 5 );

cube.add( axisHelper );
scene.add(cube);
cube.rotation.set(0, 0, 0);
cube.matrix.makeRotationFromEuler(cube.rotation);

var render = function () {
document.addEventListener("keydown", onKeyDown, false);

    function onKeyDown(e) {                 
        // W - up
    if (e.keyCode == 87) {
        rotation_matrix_x = new THREE.Matrix4().makeRotationX(.0001);
    cube.applyMatrix(rotation_matrix_x);
        }               
    // S - down
    if (e.keyCode == 83) {
    rotation_matrix_x = new THREE.Matrix4().makeRotationX(-0.0001);
    cube.applyMatrix(rotation_matrix_x);
    }
        // D - right
    if (e.keyCode == 68) {
    rotation_matrix_y = new THREE.Matrix4().makeRotationY(-0.0001);
    cube.applyMatrix(rotation_matrix_y);
        }
        // A - left
    if (e.keyCode == 65) {
    rotation_matrix_y = new THREE.Matrix4().makeRotationY(0.0001);
    cube.applyMatrix(rotation_matrix_y);
        }

        requestAnimationFrame(render);

        stats.update();
    renderer.render(scene, camera); 
};

有几个问题与此类似,但它们似乎都已过时,并且某些方法已被弃用。

【问题讨论】:

  • 试试cube.rotateX( 0.01 );cube.rotateY( -0.01 );
  • 这些方法在矩阵上可用,但也已被弃用并删除。
  • 你试过我的建议了吗?你在用three.js r.66吗?
  • 抱歉,当我将它们应用到多维数据集本身时,什么也没发生。当我在矩阵上调用它时,我收到了弃用警告。我正在使用 r.66。我应该调用其他方法来应用 rotateX/Y 吗?
  • 我在测试时添加了以下行,这导致您的建议中断:cube.rotation.setFromRotationMatrix(cube.matrix);请添加您最初的建议作为答案,我会将其标记为已解决。谢谢!

标签: matrix rotation three.js


【解决方案1】:

在自己的 x 轴上旋转对象的简单方法如下:

object.rotateX( radians );

还有object.rotateY( radians );object.rotateZ( radians );

three.js r.66

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 2017-10-13
    • 2023-03-07
    • 2023-02-23
    • 2017-02-21
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多