【问题标题】:rotation issue on physijs关于physijs的轮换问题
【发布时间】:2014-12-26 06:25:01
【问题描述】:

我正在使用这个库很短的时间,但我遇到了一个让我很头疼的问题。

我有 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);

在three.js 立方体上工作正常,但在physi.js 立方体上我不工作。

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

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

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

你可以在http://demo.cristobaldiaz.cl/test/src/app.js line 95查看移动功能

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

【问题讨论】:

  • 我对 cannon.js(另一个物理引擎)也有类似的问题,结果我必须设置四元数,而不是旋转。
  • 感谢凯文,我尝试使用四元数,但仍然失败。
  • 我也尝试使用actor.physiMesh.matrix.makeRotationFromQuaternion((new THREE.Quaternion()).setFromEuler(new THREE.Euler(0, rot, 0, 'XYZ'))); 进行矩阵旋转,但没有:(
  • 嗯,physi.js 是一个物理引擎,通过对对象施加力而不是旋转或四元数来移动对象是有意义的。
  • @KevinKuyl 是的,我知道,但是,我不知道旋转的力量是什么。

标签: javascript three.js physijs


【解决方案1】:

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

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

这样,它将始终保持同步。

查看此页面https://github.com/chandlerprall/Physijs/wiki/Callbacks-&-Events了解更多详情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2015-01-27
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多