我制作3D游戏已经有很长一段时间了,我停止了处理控件的复杂问题。我使用自定义的pointerlock控件,我希望旋转的行为像three.jsTrackballControls
。
我的代码:
function animate() {
requestAnimationFrame(animate);
camera.position.x = (Math.sin(euler.y) - Math.sin(euler.x)) * zoomValue + lastPlayerX;
camera.position.z = (Math.cos(euler.y) - Math.sin(euler.x)) * zoomValue + lastPlayerZ;
camera.position.y = -Math.sin(euler.x) * zoomValue + lastPlayerY;
composer.render();
}
animate();
lastPlayerX
、lastPlayerY
和lastPlayerZ
是球员位置轴(这是相机必须"环绕"的目标(,euler
是代表相机旋转的THREE.Euler
,zoomValue
是相机必须缩小的距离。
主要的谜题是计算方法,那么我该怎么做才能让相机围绕玩家运行呢?(仅围绕X和Y轴(。
我刚刚想明白了:
正确的公式是
function animate() {
requestAnimationFrame(animate);
camera.position.x = Math.sin(euler.y) * Math.sin(euler.x + Math.PI / 2) * zoomValue + lastPlayerX;
camera.position.z = Math.cos(euler.y) * Math.sin(euler.x + Math.PI / 2) * zoomValue + lastPlayerZ;
camera.position.y = -Math.sin(euler.x) * zoomValue + lastPlayerY;
composer.render();
}
animate();
我希望这能帮助到任何人!