我在这个库上工作了很短一段时间,但有一个问题困扰着我。
我有 2 个立方体,一个带有 phisy.js,另一个带有 Three.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 立方体上工作正常,但在 phy.js 立方体上我不起作用。
我为此创建了一个演示(我无法在 jsfiddle 上创建它,因为网络工作人员)
http://demo.cristobaldiaz.cl/test/
我还将源代码留在了 zip 文件中 ---> http://demo.cristobaldiaz.cl/test/issue.zip
您可以在http://demo.cristobaldiaz.cl/test/src/app.js查看移动功能第95行
无论如何,如果你用鼠标旋转相机,你可以检查一下,当你将立方体旋转到红色网格的方向时,就会出现问题。
最佳答案
您可以从动画循环中删除旋转更新并将其移动到物理循环:
scene.addEventListener('update', function(){
actor.onRender();
})
这样它就会始终保持同步。
查看此页https://github.com/chandlerprall/Physijs/wiki/Callbacks-&-Events了解更多详情。
关于javascript - Phyjs 中的旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653572/