Krzysztof Jurkiewicz - 7 months ago 62

C++ Question

I'm trying to use quaternions in order to rotate objects in my program.

But the problem with them is applying... a rotation.

I'm trying to rotate the matrix in this way:

`ModelMatrix = ModelMatrix * glm::toMat4(glm::quat(RotationW, RotationX, RotationY, RotationZ));`

with random numbers, and the result is an enormous scaling

Additionally lets say we've got:

`quat3 = quat2 * quat1`

Model1 = Model * glm::toMat4(glm::quat( quat3.w , quat3.x, quat3.y, quat3.z);

Model2 = Model * glm::toMat4(glm::quat( quat1.w , quat1.x, quat1.y, quat1.z);

Model2 = Model2 * glm::toMat4(glm::quat( quat2.w , quat2.x, quat2.y, quat2.z);

do Model2 and Model1 have the same rotation?

I know about glm::rotate(Model,RotationAngle, RotationAxis) , but I'd rather use quaternions

Answer

In order for a quaternion to represent a pure rotation, it has to be a unit quaternion. Random numbers usually do not have this property. You may want to normalize them before generating the quaternion:

```
l = sqrt(RotationW * RotationW + RotationX * RotationX
+ RotationY * RotationY + RotationZ * RotationZ);
RotationW /= l;
RotationX /= l;
...
```

Of course, it does not make any sense to use quaternions only to convert them to a matrix. Directly calculating the matrix will be more efficient.

For the quaternion product, you have it in the wrong order:

```
q3 = q2 * q1
=> M * mat(q3) = M * mat(q2) * mat(q1)
```