物理上的轮换问题

rotation issue on physijs

本文关键字:问题 物理上      更新时间:2023-09-26

我正在使用这个库很短的时间,我有一个杀死我的问题。

我有 2 个立方体,一个带有 phisy.js另一个带有三个.js,我有一个功能,当您根据相机的旋转按 A、S、D 或 W 键时旋转它们,我的代码是这样的:

    var v = new THREE.Vector3(0, 0, 0);
    if (keys.forward === 1)
        v.x = -1;
    if (keys.right === 1)
        v.z = 1;
    if (keys.backward === 1)
        v.x = 1;
    if (keys.left === 1)
        v.z = -1;
    //get the searched angle
    var angle = camera.rotation.y + Math.atan2(v.x, v.z);
    var rot = normalMesh.rotation.y;
    var diff = angle - rot;
    if (Math.abs(diff) > Math.PI) {
        //find the shortest way to rotate
        if (diff > 0)
            rot += 2 * Math.PI;
        else
            rot -= 2 * Math.PI;
        diff = angle - rot;
    }
    //get the /2 for a smooth rotation, i really need this
    if (diff !== 0)
        rot += diff / 2;
    normalMesh.rotation.set(0, rot, 0);

在三个.js立方体上工作正常,但在物理.js立方体上我不起作用。

为此创建了一个演示(我无法在 jsfiddle 上创建它,因为网络工作者)

http://demo.cristobaldiaz.cl/test/

我也把源代码留在一个zip文件上---> http://demo.cristobaldiaz.cl/test/issue.zip

您可以在第 95 行 http://demo.cristobaldiaz.cl/test/src/app.js 检查移动功能

无论如何,如果使用鼠标旋转相机,则可以检查将立方体旋转到红色网格体方向时是否出现问题。

您可以从动画循环中删除旋转更新并将其移动到物理循环中:

scene.addEventListener('update', function(){
    actor.onRender();
})

这样它将始终保持同步。

有关更多详细信息,请参阅此页面 https://github.com/chandlerprall/Physijs/wiki/Callbacks-&-Events。