C ++根据四元数旋转矩阵以一定角度移动对象

时间:2018-12-02 05:54:00

标签: c++ opengl quaternions glm-math rotational-matrices

我已经尝试了一段时间了。我正在用6自由度运动建造一艘太空船。我已经实现了俯仰和偏航,但是我不知道如何以当前角度向前移动船。例如。如果用户以25度向右偏航,然后以10度向上倾斜,则我需要飞船以这个新角度前进。

注意:我在类构造函数中将偏航和俯仰默认设置为0.0度。

作为一个例子,我有以下代码将船右转偏航:

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

我有三个夸脱(yawQuat,pitchQuat和rollQuat)。该当前方法更新了偏航角。我将quat相乘,对其进行归一化,然后将其转换为矩阵。尽管上面的代码可以很好地用于偏航,但是下面的accelerate代码却不能:

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

我正在计算x,y和z以获得前向量。然后,我用前向量乘以速度(在这种情况下为0.0f)来更新模型的位置。这导致物体以错误的角度移动。有人可以告诉我我在做什么错吗?谢谢。

0 个答案:

没有答案