How to get a partial transform matrix from an existing one

When rotating or moving an object using transformation matrices, in some cases we need to be able to extract a part of it from the entire rotation/movement. For example, when rotating an object by 180 degrees, we need to rotate it not at once, but in several steps, each time rotating it by 30 degrees. Linear interpolation of matrices can help us with this.

First, let’s create a final transformation matrix, for example, let it be a matrix for rotating an object by 180 degrees.

We can determine the rotation matrix by a given number of degrees using the mathutils library.

The mathutils.Matrix object has a Rotation() function. This function returns the rotation matrix, taking the rotation angle and the axis around which the rotation is performed as parameters.

Here we passed to the function an angle in radians equal to 180 degrees, and specified the axis of rotation – Z. The second parameter of the function is responsible for the size of the resulting matrix, we set it to 4, and received a 4×4 matrix.

For clarity, we can apply this matrix to the current active object and make sure that it rotates 180 degrees around the Z axis.

In our example, we know that our matrix rotates the object by 180 degrees. We can easily calculate that, for example, to rotate the object in three steps, we will need an intermediate matrix that rotates the object by 60 degrees.

But what if we don’t know how many degrees exactly our matrix rotates the object? This can happen, for example, when calculating the rotation from one vector to another. But we still need to rotate the object in three steps.

In this case, we can calculate linear interpolation for our matrix. Linear interpolation is the finding of an intermediate value between two points specified by one function.

Interpolation can be calculated between two matrices.

In our case, we can take the identity rotation matrix as the starting matrix from which we will calculate the interpolation. Such a matrix does not rotate the object, but can serve as the starting point for calculations.

And we can take our 180-degree rotation matrix as the final matrix.

To find the partial matrix that rotates the object by 1/3 relative to the full matrix (180 * 1/3 = 60), we will use the lerp() function of the Matrix object.

Here we took the starting matrix, called its lerp() function, in the parameters of which we specified the final matrix and the required part of the rotation from it.

As a result, we got a matrix of rotation by 60 degrees.

After applying it to the current active object,

we can verify that it has rotated exactly 60 degrees around the Z axis.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments