【问题标题】:Three.js rotating an object smoothly inside a functionThree.js 在函数内平滑地旋转对象
【发布时间】:2020-05-28 01:26:09
【问题描述】:

我已经使用 Three.js 构建了一个魔方,并且一切正常,但我想为魔方的转动制作动画。现在,当我转向一侧时,它就会卡入新位置。怎么让它慢慢转呢?

我现在使用的代码:

function turnOrangeSide(inverse) {
    let x = 0;
    let y = 1;
    let z = 1;
    orangeGroup = new THREE.Object3D();
    scene.add(orangeGroup);

    //This puts all the parts of the Cube that have to be turned in the group.
    orangeGroup.attach(getIntersecting(rotationPointO, x, y, z + 1));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y, z - 1));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y + 1, z));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y - 1, z));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y + 1, z + 1));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y - 1, z + 1));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y + 1, z - 1));
    orangeGroup.attach(getIntersecting(rotationPointO, x, y - 1, z - 1));

    let rotation = Math.PI / 2
    if (inverse) rotation = -Math.PI / 2
    orangeGroup.rotation.x += rotation;
}

https://rekhyt2901.github.io/AlexGames/RubiksCube/RubiksCube.html 上的实时示例。

【问题讨论】:

    标签: javascript animation three.js rotation


    【解决方案1】:

    您实际上可以做的是使用参数方程来逐步围绕轴旋转立方体。
    那会给出类似的东西:

    let fps = 60;           // fps/seconds
    let tau = 2;            // 2 seconds
    const step = 1 / (tau * fps);  // step per frame
    const finalAngle = Math.PI/2;
    const angleStep = finalAngle * step;
    let t = 0;
    
    function animateGroup(t){
       if (t >= 1) return; // Motion ended
       t += step;  // Increment time
       orangeGroup.rotation.x += angleStep; // Increment rotation
       requestAnimationFrame(() => animateGroup(t));
    }
    
    animateGroup(t);
    

    【讨论】:

    • 谢谢!这真的很好。我不得不稍微调整一下,但它工作正常,实际上让我的代码更干净,谢谢!
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2013-08-06
    相关资源
    最近更新 更多