0

我在相机容器中有一个移动相机,它像飞机一样在场景中飞来飞去;所以它可以移动到任何位置 x,y,z 正负。相机容器正在使用样条曲线查看他自己的未来路径。

现在我想使用鼠标方向旋转相机,但在向前移动物体的同时仍然保持一般的位置。你可以说,我想把头转向我的身体:在移动身体的同时,大致看方向,我正在把头转过来,让我说上下 220 度。所以我不能回头看我的身体。

在我的代码中,cameraContainer 负责在一条直线上移动并查看移动方向。相机作为子项添加到负责使用鼠标进行旋转的 cameraContainer 中。

我无法正常工作的是相机的旋转。我想这是一个非常普遍的问题。假设仅在 x 轴上移动的相机不是直线移动的,它像曲线一样移动。特别是在不同的相机位置,旋转看起来非常不同。我试图使用 cameraContainer 来避免这个问题,但这个问题似乎与世界坐标无关。

这是我所拥有的:

// camera is in a container
cameraContainer = new THREEJS.Object3D();
cameraContainer.add(camera);
camera.lookAt(0,0,1);
cameraContainer.lookAt(nextPositionOnSplineCurve);


// Here goes the rotation depending on mouse
// Vertical
var mouseVerti = 1;     // 1 = top, -1 = bottom
if(window.App4D.mouse.y <= instance.domCenterPos.y)             // mouse is bottom?
    mouseVerti = -1;

// how far is the mouse away from center: 1 most, 0 near
var yMousePerc = Math.abs(Math.ceil((instance.domCenterPos.y - window.App4D.mouse.y) / (instance.domCenterPos.y - instance.domBoundingBox.bottom) * 100) / 100);
var yAngleDiffSide = (instance.config.scene.camera.maxAngle - instance.config.scene.camera.angle) / 2;
var yRotateRan = mouseVerti * yAngleDiffSide * yMousePerc * Math.PI / 180;
instance.camera.rotation.x += yRotateRan;       // rotation x = vertical


// Horizontal
var mouseHori = 1;      // 1 = top, -1 = bottom
if(window.App4D.mouse.x <= instance.domCenterPos.x)             // mouse is left?
    mouseHori = -1;

// how far is the mouse away from center: 1 most, 0 near
var xMousePerc = Math.abs(Math.ceil((instance.domCenterPos.x - window.App4D.mouse.x) / (instance.domCenterPos.x - instance.domBoundingBox.right) * 100) / 100);
var xAngleDiffSide = (instance.config.scene.camera.maxAngle - instance.config.scene.camera.angle) / 2;
var xRotateRan = mouseHori * xAngleDiffSide * xMousePerc * Math.PI / 180;
instance.camera.rotation.y += xRotateRan;       // rotation y = horizontal

如果有人能给我提示,将非常感激!

4

1 回答 1

1

经过更多的尝试和错误,我得到了答案。解决方案是简单地考虑 y 的初始旋转。

将相机容器和真实相机设置为容器的子对象时,我必须将相机指向相机容器对象的正面,以便让相机朝正确的方向看。这导致 0, 3.14, 0 (x,y,z) 的初始旋转。解决方案是每次我分配(如 WestLangley 所述)鼠标旋转时,将 3.14 添加到 y 旋转。

cameraReal.lookAt(new THREE.Vector3(0,0,1));
cameraReal.rotation.y = xRotateRan + 3.14;
于 2013-05-14T21:32:51.077 回答