摄像头旋转对准任意平面 [英] Camera rotation aligned to arbitrary plane

查看:271
本文介绍了摄像头旋转对准任意平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么,我认为是一个摄像头一个相当简单的场景,但我的穴居人的大脑拒绝去思考什么比两个维度以上。



我的3D摄像头的需求对象后面要对齐。这个对象能够坚持任何表面,从而对什么是达是可以是任意载体相机的想法。这是非常相似的这个问题,但由于我不是直接控制'选手'对象和我,而不是试图用CreateLookAt,我有解决方案的麻烦决策意识:的如何使用四元数相机时计算旋转?

$ ; b
$ b

有关简化测试起见,我们假设该对象是一个球体



我目前有:




  • 在三维空间中的目标对象的绝对位置。

  • 代表表面法线的单位矢量对象是目前上。在这种情况下,正常的球面三角形。

  • X和Y的角度描述如何相机应轨道的对象。



  • 我相信这是所有的需要,但我不知道如何把它一起在工作的方式。我知道我需要的对象的norMy最好的尝试是转变angle'd相机如下:

     矩阵方向=矩阵.CreateRotationY(OrbitYaw)* Matrix.CreateRotationX(OrbitPitch); 

    orientation.Up = CurrentNormal;
    orientation.Right = Vector3.Cross(orientation.Forward,orientation.Up);
    orientation.Right = Vector3.Normalize(orientation.Right);
    orientation.Forward = Vector3.Cross(orientation.Up,orientation.Right);
    orientation.Forward = Vector3.Normalize(orientation.Forward);

    的Vector3目标= ObjectPosition;

    的Vector3位置= Vector3.Transform(新的Vector3(0,50,0),方向);

    矩阵视图= Matrix.CreateLookAt(位置,目标,CurrentNormal);



    定位代码似乎是正确的,但它很难说,因为第二个问题组成:相机出现滚的位置的变化。这是有道理的 - 所有的相机都知道是它的位置,在哪里看,我不会明确告诉它如何本身应该旋转。这是我需要做的,使相机保持恒定的调整(除了什么是必要的,以保持它在相同的相对位置),而跟随的对象。


    解决方案

    拓展了一点什么添■在他的评论中说,你可以借用相机下面让相机的方向去的对象的方位的某些方面。

      cameraPosition = objectPosition +(objectOrientation.Backward * 50); 
    cameraTarget = objectPosition;

    矩阵cameraArc = Matrix.CreateFromAxisAngle(objectOrientation.Up,orbitYaw)* Matrix.CreateFromAxisAngle(objectOrientation.Right,orbitPitch); //假设objectOriention是邻正常的矩阵

    cameraPosition = Vector3.Transform(cameraPosition - cameraTarget,cameraArc)+ cameraTarget;

    视图= Matrix.CreateLookAt(cameraPosition,cameraTarget,Vector3.Up);



    尽管堵漏 Vector3.Up 进入最后行(你应该在你的情况一样),视图矩阵的向上向量将与对象的向上向量。


    对齐

    I have what I think is a fairly simple scenario for a camera, but my caveman brain refuses to think in anything more than two dimensions.

    My 3D camera needs to be aligned behind an object. This object can stick to any surface, and thus the camera's idea of what "up" is can be any arbitrary vector. It's very similar to this question, but since I'm not directly controlling the 'player' object and am instead trying to use CreateLookAt, I'm having trouble making sense of the solution: How can I calculate the rotation when using a quaternion camera?

    For the sake of simplified testing, let's assume that the object is on a sphere.

    I currently have:

    • The target object's absolute position in 3D space.
    • A unit vector representing the normal of the surface the object is presently on. In this case, the normal of the triangle on the sphere.
    • The X and Y angles describing how the camera should be orbiting the object.

    I believe that this is all that's required, but I'm not sure how to put it all together in a way that works. I know that I need to transform the angle'd camera by the object's norMy best attempt is as follows:

    Matrix orientation = Matrix.CreateRotationY(OrbitYaw) * Matrix.CreateRotationX(OrbitPitch);
    
    orientation.Up = CurrentNormal;
    orientation.Right = Vector3.Cross(orientation.Forward, orientation.Up);
    orientation.Right = Vector3.Normalize(orientation.Right);
    orientation.Forward = Vector3.Cross(orientation.Up, orientation.Right);
    orientation.Forward = Vector3.Normalize(orientation.Forward);
    
    Vector3 Target = ObjectPosition;
    
    Vector3 Position = Vector3.Transform(new Vector3(0,50,0), orientation);
    
    Matrix View = Matrix.CreateLookAt(Position, Target, CurrentNormal);
    

    The positioning code seems to be correct, but it's very difficult to tell because of the second problem: the camera appears to "roll" as the position changes. This makes sense - all the camera knows is its position and where to look, and I'm not explicitly telling it how it itself should be rotated. This is what I need to do, so that the camera maintains a constant alignment (aside from what's necessary to keep it in the same relative position) while following the object.

    解决方案

    Expanding a little on what Tim S said in his comment, you can borrow some aspects of the orientation from the object the camera is following to get the camera's orientation going.

    cameraPosition = objectPosition + (objectOrientation.Backward * 50);
    cameraTarget = objectPosition;
    
    Matrix cameraArc = Matrix.CreateFromAxisAngle(objectOrientation.Up, orbitYaw) * Matrix.CreateFromAxisAngle(objectOrientation.Right, orbitPitch);//assumes objectOriention is a ortho-normal matrix
    
    cameraPosition = Vector3.Transform(cameraPosition - cameraTarget, cameraArc) + cameraTarget;
    
    view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
    

    Despite plugging Vector3.Up into the last line (which you should do in your case), the view matrix's up vector will be in alignment with the object's up vector.

    这篇关于摄像头旋转对准任意平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆