【问题标题】:Three js move object using vector with applied angle三个 js 使用具有应用角度的矢量移动对象
【发布时间】:2021-01-04 22:38:26
【问题描述】:

在键盘按钮上,我正在调用以下函数。我只想在 Z 轴上应用运动。效果很好。

// declared global Object3D object
function moveRight() {
    v = new THREE.Vector3(0, 0, 0.01)
    object.position.add(v)
}

但是,有时模型已经旋转。我将此旋转存储在变量“totalrotation”(以度为单位)中。在这种情况下,我想在正确的方向上移动我的对象(不仅在 z 轴上)。我试过的是:

// declared global Object3D object
// declared global float totalrotation, lets say it is 90 deg
function moveRight() {
    v = new THREE.Vector3(0, 0, 0.01)
    rads = THREE.Math.degToRad(totalrotation)
    v.applyAxisAngle( new THREE.Vector3(0, 1, 0), rads);
    object.position.add(v)
}

但它不起作用。然后我进一步尝试,只是尝试将 rads 除以数字 4:

// declared global Object3D object
// declared global float totalrotation, lets say it is 90 deg
function moveRight() {
    v = new THREE.Vector3(0, 0, 0.01)
    rads = THREE.Math.degToRad(totalrotation) / 4
    v.applyAxisAngle( new THREE.Vector3(0, 1, 0), rads);
    object.position.add(v)
}

它开始按预期工作。 谁能解释一下,为什么第二个代码示例不起作用?在第二个示例中,我向对象添加了正确旋转的矢量,但我不知道为什么必须将角度除以数字 4。

EDIT1:这个问题无法解决,因为我在另一种方法中犯了错误,所以总旋转应该大4倍。没有“/ 4”划分的代码段正在工作。答案中有更多信息。

【问题讨论】:

    标签: javascript vector three.js rotation


    【解决方案1】:

    好的,我想通了。基本上,我在另一种方法中意外地增加了总旋转次数太多。按下按钮后,我想添加 1 度角,相反,我为每个对象出现添加 1 度角。四乘以 1 度是 4,这就是总角度必须除以 4 的原因。 很抱歉造成混淆,我想没有人知道,因为我没有提供这个功能。这个方法:

    function rotateLeft() { // this method gets invoked by keyboard press
        for (var i = 0; i < objects.length; i++) {
            if (objects[i][1] > 0) {
                totalrotation += 1
                rotateAboutPoint(objects[i][0], new THREE.Vector3(globalXpos, globalYpos, craneZ-1.06), new THREE.Vector3(0, 1, 0), THREE.Math.degToRad(1))
            }
        }
    }
    

    必须改为:

    function rotateLeft() { // this method gets invoked by keyboard press
        totalrotation += 1
        for (var i = 0; i < objects.length; i++) {
            if (objects[i][1] > 0) {
                rotateAboutPoint(objects[i][0], new THREE.Vector3(globalXpos, globalYpos, craneZ-1.06), new THREE.Vector3(0, 1, 0), THREE.Math.degToRad(1))
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多