# Moving an object at an angle based on quaternion rotation matrix (c++)

I have been trying to figure this out for a while now, and can’t seem to get a working method. I am building a space ship movement with the following movement keys:

P = accelerate (move forward)

A = yaw left

D = yaw right

W = pitch down

S = pitch up

The following the code I have to perform the movements for keys A,D,W and S. They all work as expected. I just can’t figure out how to move forward at the angle the space ship is.

``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(0, 0, 1));      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; }  void Player::yawLeft(float deltaTime) {     this->yaw += this->rotationAngle * deltaTime;      if (this->yaw > 360.0f) {         this->yaw = 0.0f;     }      cout << "yawLeft: " + 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(0, 0, 1));      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; }  void Player::pitchDown(float deltaTime) {     this->pitch += this->rotationAngle * deltaTime;      if (this->pitch > 360.0f) {         this->pitch = 0.0f;     }      cout << "pitchDown: " + to_string(this->pitch) << 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(0, 0, 1));      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; }  void Player::pitchUp(float deltaTime) {     this->pitch -= this->rotationAngle * deltaTime;      if (this->pitch < 0.0f) {         this->pitch = 360.0f;     }      cout << "pitchDown: " + to_string(this->pitch) << 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(0, 0, 1));      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; }   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::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0, 1, 0)));      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; }  void Player::accelerate(float deltaTime) {     // get the forward vector     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));      // update the position     // this is incorrect     this->position += front * 0.01f;      this->model = glm::mat4(1.0f);     this->model = glm::translate(this->model, this->position) * rotationMatrix; } ``

Note: the actual code is crappy as it has a lot of reptition, I will fix that.

For the `accelerate()` method I get the forwad vector from the yaw and pitch, but it doesn’t move in the right direction. Can someone provide me a guide or an explanation as to how I can make the ship move forward in the orientation it current is in? Thanks!