0
votes

I try to implement the idea behind the FP camera to move the player object so I have 4 vectors position, right, up and front, I use the following method to update vectors and populate the matrix to send to the shader:

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

and these methods to receive position and rotation:

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

the object can rotate correctly but always move along one axis (1,0,0) not at the front (Direction) vector. this method runs successfully with camera which can move in any direction I point to.

1

1 Answers

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

You are normalising a default constructed glm::vec3 front. That's undefined behaviour. Apart from the code above, I don't see where else you modify Mesh::front. You are not touching it in Mesh::turn(), and updating Mesh::yaw doesn't seem to affect Mesh::front in any way.