如何在 DirectX11 中围绕其局部轴之一旋转对象

How to rotate an object around one of its local axis in DirectX11

我想围绕其局部轴之一旋转对象。在我的例子中是 Y 轴。 该对象有自己的世界矩阵,初始化如下:

XMFLOAT4X4 newInstance;

// Construct matrix
XMStoreFloat4x4( &newInstance.worldMatrix, XMMatrixScaling( scale.x, scale.y, scale.z ) *
                 XMMatrixRotationRollPitchYaw( XMConvertToRadians( rotation.x ),
                                               XMConvertToRadians( rotation.y ),
                                               XMConvertToRadians( rotation.z ) ) *
                    XMMatrixTranslation( translation.x, translation.y, translation.z ) ) ;

// Add new instance to collection
mInstanceData.push_back( newInstance );

我的最终目标是根据游戏手柄的输入旋转我的对象。

没有 "math-heavy" 背景,所以请多多包涵。 我熟悉矩阵相乘时顺序的重要性(缩放 * 旋转 * 平移)。我尝试了几种方法,但都对我失败了。

第一种方法

// Create rotation matrix
XMMATRIX rotationMatrix = XMMatrixRotationY( XMConvertToRadians( angle ) );

// Multiply with old matrix AND store back to the old matrix
XMStoreFloat4x4( &newInstance.worldMatrix, XMLoadFloat4X4( &newInstance.worldMatrix ) * rotationMatrix );

这导致对象围绕世界 Y 轴旋转,而不是 其本地轴。

第二种方法

我认为我需要找到我的本地 Y 轴。也许从我现有的世界矩阵中提取它?我发现 this post 有人解释说 您可以确实从对象的世界矩阵中提取对象的所有局部轴。

A | 1 0 0 0 |
B | 0 1 0 0 |
C | 0 0 1 0 |
D | 0 0 0 1 |
----x y z w

Here, the first three values in row A are your local X-axis = (1,0,0)
first three values in row B are your local Y-axis = (0,1,0)
and first three values in row C are your local z-axis = (0,0,1).

所以我着手从对象世界矩阵中提取 Y 轴。

// Construct axis
XMVECTOR axis =  XMVectorSet( mInstanceData[0].worldMatrix._21, mInstanceData[0].worldMatrix._22, mInstanceData[0].worldMatrix._23, 0 );

// Create rotation matrix based on axis
XMMATRIX rotationMatrix = XMMatrixRotationAxis( axis ,XMConvertToRadians( angle) );

// Multiply with old matrix AND store back to the old matrix
XMStoreFloat4x4( &mInstanceData[0].worldMatrix, XMLoadFloat4x4( &mInstanceData[0].worldMatrix ) * rotationMatrix );

这个方法也失败了。使用XMMatrixRotationAxis的结果是物体从世界原点到指定向量的轴旋转。

第三种方法

然后我想我也许可以使用与初始化矩阵相同的技术来执行旋转!这意味着我仍然需要从对象世界矩阵中提取一些当前数据。我找到了这个:

所以我尝试从当前世界矩阵中提取任何相关数据并构造新的缩放、旋转和平移矩阵,将它们相乘并存储到对象世界矩阵中。

// Extract scale and translation and construct new matrix
XMStoreFloat4x4( &mInstanceData[0].worldMatrix,  XMMatrixScaling( mInstanceData[0].worldMatrix._11, mInstanceData[0].worldMatrix._22, mInstanceData[0].worldMatrix._33 ) *
        XMMatrixRotationRollPitchYaw( 0.0f, XMConvertToRadians( angle ), 0.0f ) *
        XMMatrixTranslation( mInstanceData[0].worldMatrix._41, mInstanceData[0].worldMatrix._42, mInstanceData[0].worldMatrix._43 ) );

这导致该对象将自身变成了某种无法形容的东西。

当所有事情都失败时,你会求助于 stackoverlow.. 有没有人可以分享一些见解?谢谢!

AFAIU,第一种方法是正确的,但是,您似乎首先创建了一个世界矩阵(因此您的每个顶点现在都在 world-space 中)然后应用 'around-local-y-axis' 转型。 IMO,您应该首先应用 XMMatrixRotationY( XMConvertToRadians( angle ) ); 到模型中的顶点-space 然后构建世界矩阵。