我尝试实现FP相机背后的想法来移动玩家对象,所以我有4个向量位置,向右,向上和向前,我使用以下方法更新向量并填充矩阵以发送到着色器:< / p>
void Mesh::updateVectors() {
glm::vec3 f;
f.x = cos(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
f.y = sin(glm::radians(this->pitch));
f.z = sin(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front = glm::normalize(f);
this->right = glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
matrix = glm::lookAt(this->position, this->position + this->front, this->up);
glm::vec3 s(scale);
matrix = glm::scale(matrix, s);
for (GLuint i = 0; i < this->m_Entries.size(); i++) {
this->m_Entries[i].setModelMatrix(matrix);
}
}
以及接收位置和旋转的这些方法:
void Mesh::ProcessKeyboard(Move_Directions direction, GLfloat deltaTime) {
std::cout << this->front.x << " / " << this->front.z << std::endl;
GLfloat velocity = this->movementSpeed * deltaTime;
if (direction == FORWARD)
this->position += this->front * velocity;
if (direction == BACKWARD)
this->position -= this->front * velocity;
updateVectors();
}
void Mesh::Turn(GLfloat y) {
this->yaw += y;
this->updateVectors();
}
对象可以正确旋转但始终沿着一个轴(1,0,0)移动,而不是沿前面(方向)向量移动。 这种方法可以在相机上成功运行,可以向我指向的任何方向移动。
答案 0 :(得分:0)
glm::vec3 front;
position.x = cos(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
position.y = sin(glm::radians(this->pitch));
position.z = sin(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front = glm::normalize(front);
您正在规范默认构造的glm::vec3 front
。这是未定义的行为。除了上面的代码,我没有看到你修改Mesh::front
的其他地方。您没有在Mesh::turn()
中触及它,更新Mesh::yaw
似乎不会以任何方式影响Mesh::front
。