我的计划:
<强>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);
}
现在我已经给出了 theta、phi、centerpoint 和 radius 我想计算位置和旋转[相机看中心点]
<强>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
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);
(鼠标增量也可能存在一些符号问题,但应该很容易修复。)
关于c++ - 使用鼠标事件计算相机的位置和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277093/