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

标签 c++ opengl math 3d

我的计划:

<强>1。计算鼠标方向[x,y][成功]

我的鼠标移动事件:

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

<强>2。计算角度[theta, phi] [成功]

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);

}

现在我已经给出了 thetaphicenterpointradius 我想计算位置旋转[相机看中心点]

<强>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;

感谢您的解决方案!

最佳答案

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

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

  • phitheta 顺序错误

  • 非常简单的转换: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);

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

关于c++ - 使用鼠标事件计算相机的位置和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277093/

相关文章:

c++ - C++ 的最佳(速度)任意精度库是什么?

c++ - 进行依赖注入(inject)时公开私有(private)方法

java - 如何实现 pow(double x, double y)?

android - 将 LatLngBounds 缩小 5%

c++ - CUDA:避免在分支发散时串行执行

c++ - 文字的使用,在 C++ 中是/否

python - Phonon 的 VideoWidget 在 QGLWidget(Qt,Python)上显示错误的颜色

javascript - 在 Javascript 中对范围内的每个元素求和

python - 使用 CUDA 在 python 中展开一个简单的可并行化 for 循环

SDL 程序中的颜色已关闭