Obtaining momentum quaternion from two quaternions and timestep - rotation

I have a quaternion which holds the rotation of an object. During the frame I modify it and obtain a new quaternion. I can calculate a quaternion that rotates from 'previous frame' to 'current frame'.
I cannot figure out, however, how to 'divide by t' this quaternion to get the rotation-per-second that I need.
I.e, based on the timestep, I need to know what the quaternion would look like had it been applied to itself an X amount of times (meaning, 28.5 times at 28.5 fps, etcetera).
Would anybody know how to do this? Or would you advise me to do something akin converting to Euler, multiplying, and then converting back?

Since combining rotations is equivalent to quaternion multiplication, repeating a rotation X times is equivalent to exponentiation: pow(q,X)=pow(q,1/t), or exp(ln(q)*X)=exp(ln(q)/t). See how to calculate these here.

Related

WebGL Combined Camera Rotations

Why is it that when I multiply a rotation matrix with the camera matrix -
let rotation = m.mult4(m.rotateX(xRads)),m.rotateY(yRads)));
camera = m.mult4(rotation,camera);
I get incorrect rotation. But when I multiply that same rotation matrix with just the translation portion of the camera matrix I get the correct rotation?
let rotation = m.mult4(m.rotateX(xRads)),m.rotateY(yRads)));
camera = m.mult4(rotation, m.identity([ camera[12], camera[13], camera[14] ]))
Is there some kind of unintended accumulation that's throwing out the result? Is there a way to successfully multiply with the entire camera matrix so I don't have to track rotations separately?
You're doing two different things here: rotate a (presumably) already rotated coordinate system vs. setting the translation component on a newly created rotation matrix.
Is there some kind of unintended accumulation that's throwing out the result?
Unintended by you, yes. Unintended by linear algebra, no.
Is there a way to successfully multiply with the entire camera matrix so I don't have to track rotations separately?
Not really, as even if you were to decompose the matrix you'd have the problem that there's more than one valid result.

Accurately converting IMU angular velocities into a quaternion

Given three angular velocities vx, vy, vz about the x, y and z axes, measured in radians per second, as derived from an IMU's rate gyro, how do I produce an equivalent quaternion for the entire rotation between one sample and the next, i.e. the integral of rotation over time dt between the current sample and the previous sample?
The primary issue is that these three angular velocities are measured independently of each other, and yet rotations are not commutative. This means the order in which the angular velocities are applied during the integration would affect the computed quaternion, just as converting Euler angles to a quaternion produces a different quaternion depending on the order in which the Euler rotations are applied (e.g. x, then y, then z, vs. some other order).
I think the right thing to do is to split the timestep dt into a number of shorter time period samples, e.g. say N=10, then divide each velocity by that number, giving vx' = vx/N, vy' = vy/N, vz' = vz/N, and then applying the rotations N times in round robin fashion, in largest to smallest order, calculating the actual rotation over the interval dt/N in each case, and accumulating this into the final rotation quaternion.
I see a lot of references to quaternion derivatives when related questions are asked though, and I wonder if it might be possible to convert the angular velocities (which are derivatives of Euler angles) directly to a quaternion derivative (again though probably suffering from axis ordering sensitivity), then somehow integrate the quaternion derivative to convert back to a quaternion spanning time dt.
Seems like there should be a "right" way to do this, since every IMU that uses a rate gyro has to solve this problem. Any insights into this would be greatly appreciated!
I found the answer in this excellent post by Ashwin Narayan.
Update (1): the rowan library implements the necessary quaternion exponentiation in Python.
Update (2): User harold pointed to this answer, which shows the same quaternion exponentiation in C++ code, which is more legible than the NumPy code in rowan.

How to calculate an angle from a rotation matrix

I have a given 3x3 rotation matrix and I want to calculate the rotation angle around z axis. How do I get there?
For example, in this case below, how did they calculated the "-30deg rotation around the x axis"? Or how did they get to the "-74deg" value around that axis?
This is my original matrix:
Thank you!
It is simple if the rotation matrix is just a rotation matrix and there is no scaling. Here is a site that explains in more pretty terms then I am willing to diagram here. Basically the rotation matrix is composed of sinf(x) and cosf(x) of euler angles (well you can think of it like that at least). You can therefore use values within it to back calculate the euler angles.
http://nghiaho.com/?page_id=846
If you have scaling involved you will need to normalize each row of the matrix first. Then apply the above method.

How to get angle rotated of a 3D object from its transform martrix

I have a 3d object which is free to rotate along x,y and z axis and it is then saved as a transform matrix. In a case where the sequence of rotation is not known and the object is rotated for more than 3 times (eg :-if i rotate the object x-60degress, y-30 degrees, z-45 degrees then again x->30 degrees), is it possible to extract the angles rotated from the transform matrix?.I know that it is possible to get angles if the sequence of rotation is known, but if I have only the final transform matrix with me and nothing else, is it possible to get the angles rotated(x,y,and z) from the transform matrix ?
Euler angle conversion is a pretty well known topic. Just normalize the matrix orientation vectors and then use something like this c source code.
The matrix is the current state of things it has no knowledge of what the transformation has been in the past. It does not know how the matrix was built. You can just take the matrix into and decompose it into any pieces you like, as long as:
The data do not overlap. For example:Two X turns after each other is indistinguishable form each other (no way to know if its 1 2 or three different rotations summed).
The sequence order is known
A decomposition can be built out of the data (for example scale can be measured)

Quaternions and Transform Matrices

Tell me if I am wrong.
I'm starting using quaternions. Using a rotation matrix 4 x 4 (as used in OpenGL), I can compute model view matrix multiplying the current model view with a rotation matrix. The rotation matrix is derived from the quaternion.
The quaternion is a direction vector (even not normalized) and a rotation angle. Resulted rotation is dependent on the direction vector module and the w quaternion component.
But why I should use quaternions instead of Euler axis/angle notation? The latter is simpler to visualize and to manage...
All information that I found could be synthetized with this beatifull article:
http://en.wikipedia.org/wiki/Rotation_representation
Why it is better to use quaternions is explained in the article.
More compact than the DCM representation and less susceptible to round-off errors
The quaternion elements vary continuously over the unit sphere in R4, (denoted by S3) as the orientation changes, avoiding discontinuous jumps (inherent to three-dimensional parameterizations), this is often referred to as gimbal lock.
Expression of the DCM in terms of quaternion parameters involves no trigonometric functions
It is simple to combine two individual rotations represented as quaternions using a quaternion product
Unlike Euler angles, quaternions don't suffer from gimbal lock.
I disagree that quaternions are easier to visualize, but the main reason for using them is that it's easy to concatenate rotations without "matrix creep".
Quaternions are generally used for calculative simplicity - it's a lot easier (and faster) to do things like composing transformations when using quaternions. To quote the Wikipedia page you linked,
Combining two successive rotations,
each represented by an Euler axis and
angle, is not straightforward, and in
fact does not satisfy the law of
vector addition, which shows that
finite rotations are not really
vectors at all. It is best to employ
the direction cosine matrix (DCM), or
tensor, or quaternion notation,
calculate the product, and then
convert back to Euler axis and angle.
They also do not suffer from a problem common to axis/angle form, gimbal lock.
Quaternions are easier to visualize, manage and create in scenarios where you want to rotate about a particular axis that can be easily calculated. Determining a single rotation angle is much easier than decomposing a rotation into multiple angles.
Corrections to the OP: the vector represents the axis of rotation, not a direction, and the rotation component is the cosine of the half-angle, not the angle itself.
As mentioned, quaternions don't suffer from gimble lock.
For a given rotation, there is exactly one normalized quaternion representation.
There can be several seemingly unrelated axis/angle values that result in the same rotation.
Quaternion rotations can be easily combined.
It is extraordinarily complex to calculate an axis/angle notation that is the cumulation of two other axis/angle rotations.
Floating point numbers have a higher degree of accuracy when representing values between 0.0 and 1.0.
The short answer is that axis/angle notation can initially seem like the most reasonable representation, but in practice quaternions alleviate many problems that axis/angle notation presents.

Resources