gpt4 book ai didi

javascript - 在 Three.js 中旋转相机无法正常工作

转载 作者:行者123 更新时间:2023-12-03 06:15:35 24 4
gpt4 key购买 nike

我有以下代码来在 Three.js 场景中围绕 x 轴旋转相机:

var cameraOrginX = 0, cameraOrginY = 0, cameraOrginZ = 0;
var cameraEndX = 0, cameraEndY = 0, cameraEndZ = 1000;
var angle = 0;

function init(){
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(0,0,1000);

//ROTATE THE CAMERA
var radians = angle * Math.PI / 180.0;

cameraEndY = Math.cos(radians) * (cameraEndY-cameraOrginY) - Math.sin(radians) * (cameraEndZ-cameraOrginZ) + cameraOrginY;
cameraEndZ = Math.sin(radians) * (cameraEndY-cameraOrginY) + Math.cos(radians) * (cameraEndZ-cameraOrginZ) + cameraOrginZ;
//

//CAMERA NEW POS
camera.position.x = cameraOrginX + cameraEndX;
camera.position.y = cameraOrginY + cameraEndY;
camera.position.z = cameraOrginZ + cameraEndZ;
console.log(camera.position.y + " " + camera.position.z);
}

当我将 Angular 变量设置为 0 时,我的相机位置为 x=0, y=0, z=1000;这是我们所期望的,也是好的。

但是当我将 Angular 更改为 90 度时,例如我的位置更改为 x=0 y= -1000 z=-999,99999;而您期望位置为 x =0、y=-1000、z =0;

为什么会发生这种情况?有人可以向我解释我做错了什么吗:)

除了 0 之外的所有 Angular 都给了我奇怪的位置:(

如果需要更多代码或 jsfidle,请询问我:)

最佳答案

cameraEndY = Math.cos(radians) * (cameraEndY-cameraOrginY) - Math.sin(radians) * (cameraEndZ-cameraOrginZ) + cameraOrginY;
cameraEndZ = Math.sin(radians) * (cameraEndY-cameraOrginY) + Math.cos(radians) * (cameraEndZ-cameraOrginZ) + cameraOrginZ;

您无法在 cameraEndZ 计算中使用 cameraEndY,因为它已经更新。您应该在此旋转计算中使用旧的yz坐标,而不是更新的坐标。请使用以下内容 -

var cosTheta = Math.cos(radians);
var sinTheta = Math.sin(radians);

var a = cosTheta * (cameraEndY-cameraOrginY);
var b = sinTheta * (cameraEndZ-cameraOrginZ);
var c = sinTheta * (cameraEndY-cameraOrginY);
var d = cosTheta * (cameraEndZ-cameraOrginZ);


cameraEndY = a - b + cameraOrginY;
cameraEndZ = c + d + cameraOrginZ;

关于javascript - 在 Three.js 中旋转相机无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com