代码之家  ›  专栏  ›  技术社区  ›  Max Mister

使用鼠标事件计算相机的位置和旋转

  •  4
  • Max Mister  · 技术社区  · 7 年前

    我的计划:

    1、计算鼠标方向【x,y】【成功】

    I我的鼠标移动事件:

    int directionX = lastPosition.x - position.x;
    int directionY = lastPosition.y - position.y;
    

    2、计算角度[θ,φ][成功]

    float theta = fmod(lastTheta + sensibility * directionY, M_PI);
    float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);
    

    编辑{

    错误修复:

    float theta = lastTheta + sensibility * directionY * -1;
    if (theta < M_PI / -2)theta = M_PI / -2;
    else if (theta > M_PI / 2)theta = M_PI / 2;
    
    float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);
    

    }

    现在我给了 θ ,则, phi公司 这个 中点 以及 半径 我想计算 位置 以及 旋转 [让摄像头看到中心点]

    3、计算位置坐标【X、Y、Z】【失败】

    float newX = radius * sin(phi) * cos(theta);
    float newY = radius * sin(phi) * sin(theta);
    float newZ = radius * cos(phi);
    

    解决方案[由meowgoesthedog提供]:

    float newX = radius * cos(theta) * cos(phi);
    float newY = radius * sin(theta);
    float newZ = radius * cos(theta) * sin(phi);
    

    4、计算旋转[失败]

    float pitch = ?;
    float yaw = ?;
    

    解决方案[由meowgoesthedog提供]:

    float pitch = -theta;
    float yaw = -phi;
    

    感谢您的解决方案!

    1 回复  |  直到 7 年前
        1
  •  2
  •   meowgoesthedog    7 年前

    您的尝试几乎(有点)正确:

    • 如图所示,在OpenGL中,“垂直”方向通常为 Y ,而您的公式假定 Z

    • phi theta 顺序不对

    • 非常简单的转换: yaw = -phi ,则, pitch = -theta (从摄像机的角度)

    固定公式:

    float position_X = radius * cos(theta) * cos(phi);
    float position_Y = radius * sin(theta);
    float position_Z = radius * cos(theta) * sin(phi);
    

    (鼠标增量也可能存在一些符号问题,但应该很容易解决。)