I have been trying to figure this out for a while now. i am building a space ship with 6DOF movement. I have implement pitch and yaw, but I can't figure out how to move the ship forward at its current angle. E.g. If the user yaws to right at 25 degrees, then pitchs up at 10 degrees, I would need the ship to then move forward at this new angle.
Note: I default the yaw and pitch at 0.0 degrees in the class constructor.
As an example I have the following code to move the ship right on yaw:
void Player::yawRight(float deltaTime)
{
this->yaw -= this->rotationAngle * deltaTime;
if (this->yaw < 0.0f) {
this->yaw = 360.0f;
}
cout << "yawRight: " + to_string(this->yaw) << endl;
this->yawQuat = glm::angleAxis(glm::radians(this->yaw), glm::vec3(0, 1, 0));
this->pitchQuat = glm::angleAxis(glm::radians(this->pitch), glm::vec3(0, 0, 1));
this->rollQuat = glm::angleAxis(glm::radians(this->roll), glm::vec3(1, 0, 0));
glm::mat4 rotationMatrix = glm::toMat4(glm::normalize(this->yawQuat * this->pitchQuat * this->rollQuat));
this->model = glm::mat4(1.0f);
this->model = glm::translate(this->model, this->position) * rotationMatrix;
}
I have three quats (yawQuat, pitchQuat and rollQuat). This current method updates the yaw angle. I multiply the quats, normalize them and convert them to matrix. While the above code works fine for yaw, the following code for accelerate
just doesn't:
void Player::accelerate(float deltaTime)
{
float x = cos(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
float y = sin(glm::radians(this->pitch));
float z = sin(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
glm::vec3 front = glm::vec3(x,y,z);
front = glm::normalize(front);
glm::mat4 rotationMatrix = glm::toMat4(glm::normalize(this->yawQuat * this->pitchQuat * this->rollQuat));
this->position += front * 0.01f;
this->model = glm::mat4(1.0f);
this->model = glm::translate(this->model, this->position) * rotationMatrix;
}
I am calculating the x,y and z to get the front vector. I then update the position of the model with the front vector multiplied by speed (0.0f in this case). This results in the object moving at the wrong angle. Can someone tell what I am doing wrong here? Thank you.