oglglslnoob
oglglslnoob

Reputation: 9

GLM OpenGL Rotation only correct around X axis

I'm using this very simple code to rotate objects around their own origin

void Entity::rotate(unsigned short axis, float speed, float delta)
{
    m_Orientation[axis] += delta * speed;
    glm::quat quaternion = glm::quat(glm::vec3(m_Orientation[0], m_Orientation[1], m_Orientation[2]));

    m_RotationMatrix = glm::mat4_cast(quaternion);
}

The issue I'm facing is that the rotation is only relative to object's direction on the X axis. Meaning that no matter which way the object is facing (what it's orientation is), if I rotate it around X it will always rotate around it's own X axis. That is what I would like to do, but on the other 2 axis as well.

But Y and Z always rotate around world X and Y axis, disregarding which way my object is facing.

My code is so small and simple I don't really see the problem? Is the X rotation being always correct the accident, or is it that the other two are wrong?

Upvotes: 1

Views: 1605

Answers (1)

Talesseed
Talesseed

Reputation: 191

I suggest that you try this:

#include <glm/gtx/euler_angles.hpp>
/**
If you change speed to a vec3,
it do all the calculations at once
(you won't have to do it for each angle)
**/

void Entity::rotate(glm::vec3 speed, float delta) 
{
   rotation = speed * data
   m_modelMatrix *= glm::eulerAngleXYZ(rotation.x, rotation.y, rotation.z);
}

The glm::eulerAngleXYZ function will take the angles and create a rotation matrix from it.

But be aware of gimbal lock.

If you do your multiplication of the matrix from right to left, it will rotate around the global axis. If you do it the other way, it will rotate around the local axis.

Upvotes: 0

Related Questions