# Quaternion rotate only in x and y

Hi,

i want to rotate only in x and y direction, but it does not work that good.

``````float pitch, yaw, roll;
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
pitch = (diff.y() * 0.5 + pitch);
yaw = (diff.x() * 0.5 + yaw);
std::cout << pitch << " - " << yaw << std::endl;
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
``````

yaw works fine, but pitch does not. it is limited to [-90,90]. But if i move the mouse not only up/down, but a little sideway, it moves over that limit but does crazy things like jumping somewhere. And most times i get nan from pitch and yaw and the model is not displayed anymore.

Any ideas why this happens, or how to rotate only in x/y direction using Quaternion??

sure, use glm
rotating in the xy-level means the rotation axis = vec3(0, 0, 1)

a quaternion stores a rotation, if you want to “add” another rotation:

``````glm::quat m_rotation(1, 0, 0, 0); // no rotation
float angle = 3.14 / 4; // 45°
glm::vec3 axis(0, 0, 1); // rotation axis
m_rotation = glm::rotate(m_rotation, angle, axis); // rotate the quaternion 45° around the z-axis
``````

later, if you want to create a rotation matrix out of it:

``````glm::mat4 matrix = glm::toMat4(m_rotation);
``````

so in Qt:

``````
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(0.0, 1.0, 0.0).normalized(), diff.x() * 0.5) * m_rotation;
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0).normalized(), diff.y() * 0.5) * m_rotation;

``````

But this does not work. i wan to rotate in x and y direction from the beginning. That way you move it first maybe 90° left (in x direction) and then 90° up. But this up moving is from the new rotation view. So it’s the same as 90° y up and 90° z