OpenGL ES3,C ++ 11,Android,我有一个物体和一个实现第三人称相机的相机,所以当我触摸屏幕时,我想让物体看向相机,所以我有一个物体和视图的变换矩阵相机的矩阵,相机围绕物体旋转,当我触摸屏幕时,我希望物体朝向相机旋转,所以我尝试了以下内容:
glm::vec3 up(0, 1, 0);
glm::vec3 camFor(glm::cross(camera.getRight(), up));
GLfloat theta = glm::angle(glm::vec2(camFor.x, camFor.z), glm::vec2(getDirection().x, getDirection().z));
LOGI("game theta %f", theta);
matrix = glm::rotate(matrix, theta, glm::vec3(0.0, 1.0, 0.0));
我得到了物体前向矢量和相机前向矢量之间的水平角度glm::vec3(matrix[2][0], matrix[2][1], matrix[2][2])
,第一次当相机正好在物体后面我得到θ等于pi时那就是我触摸了屏幕触发对象旋转的事件并按预期看向摄像机,θ变为零,如果我围绕对象旋转摄像机并再次触发事件,它会为θ提供意外值。
相机的旋转代码是:
void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, double& delta, GLboolean constrainpitch) {
xoffset *= (delta * this->rotSpeed);
yoffset *= (delta * this->rotSpeed);
pitch += yoffset;
yaw += xoffset;
if (constrainpitch) {
if (pitch >= maxPitch) {
pitch = maxPitch;
yoffset = 0;
}
if (pitch <= minPitch) {
pitch = minPitch;
yoffset = 0;
}
}
glm::quat Qx(glm::angleAxis(glm::radians(yoffset), glm::vec3(1.0f, 0.0f, 0.0f)));
glm::quat Qy(glm::angleAxis(glm::radians(xoffset), glm::vec3(0.0f, 1.0f, 0.0f)));
glm::mat4 rotX = glm::mat4_cast(Qx);
glm::mat4 rotY = glm::mat4_cast(Qy);
view = glm::translate(view, c);
view = rotX * view;
view = view * rotY;
view = glm::translate(view, -c);
}
解决方案:
错误在于获取相机矩阵的右矢量这是正确的
glm::vec3& Camera::getRight() {
right = glm::vec3(view[0][0], view[1][0], view[2][0]);
return right;
}
答案 0 :(得分:2)
错误在于获取相机矩阵的右矢量这是正确的
glm::vec3& Camera::getRight() {
right = glm::vec3(view[0][0], view[1][0], view[2][0]);
return right;
}