翻译-旋转-使用XMMATRIX向后翻译 [英] Translate - Rotate - Translate Back using XMMATRIX

查看:172
本文介绍了翻译-旋转-使用XMMATRIX向后翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只知道一个对象的世界矩阵(因为不跟踪其x/y/z位置,这样会更容易),那么如何围绕它的中心旋转呢?

If all I know is an object's World matrix (because its x/y/z position is not tracked, which would be easier), how do I go about rotating it around it's center?

如果我知道该位置,它将像这样简单:

If I knew the location, it'd be about as simple as something like this:

        XMMATRIX world = pMissile->GetWorldMatrix(); 

        XMMATRIX matrixTranslation = XMMatrixTranslationFromVector(pMissile->GetPosition());
        XMMATRIX matrixInvTranslations = XMMatrixInverse(nullptr, matrixTranslation);

        float rotationAmount = (60 * XMConvertToRadians((float)fElapsedTime / 2.0f));

        XMMATRIX missileWorld = world * 
                    matrixInvTranslations 
                    * XMMatrixRotationX(rotationAmount) 
                    * XMMatrixRotationY(rotationAmount)
                    * XMMatrixRotationZ(rotationAmount)
                    * matrixTranslation;        
        pMissile->SetWorldMatrix(missileWorld);

不幸的是,由于我不知道该职位,所以我不确定该怎么做.基本上,我需要能够仅从世界矩阵中获取转换回原点".在我开始将元素从矩阵中拉出之前,必须有DirectX或DirectXTK函数来执行此操作,不是吗?

Unfortunately, since I don't know the position, I'm not sure what to do. Basically I need to be able to get the "Translate back to the origin" from just the world matrix. Before I start pulling elements out of the matrix, there must be a DirectX or DirectXTK function to do this, no?

目前,我正在分解矩阵以获取它:

Currently I'm decomposing the matrix to get it:

        XMVECTOR vectorTranslation, vectorScale, rotationQuat;
        XMMatrixDecompose(&vectorScale, &rotationQuat, &vectorTranslation, world)

如果这是正确/最好的方法,请告诉我!

If that's the right/best way, let me know!

有些切线,如您所见,在我将其转换为原点进行旋转之前,我使用了转换的逆向将其移回"它原来的位置.许多示例都跳过了这一点-我是否缺少某些东西,因为您不需要-最后再翻译回去?

Somewhat tangentially, as you can see I use an inverse of the translation to "move it back" to where it was originally before I translated it to the origin for rotation. A lot of samples skip this - is there something I'm missing in that you don't -need- to translate back at the end?

推荐答案

XMMatrixDecompose是获取任意变换矩阵元素的正确,完全通用的方法.计算是昂贵的,因此大多数人都对矩阵中的内容进行了假设-因为他们在所有点上都对其进行控制.例如,避免非均匀缩放实际上可以简化事情.

XMMatrixDecompose is the correct, fully general way to get the elements of an arbitrary transformation matrix. The computation is expensive, so most folks make assumptions about what's in the matrix--because they control it at all points. For example, avoiding non-uniform scaling can really simplify things.

许多游戏专有地使用旋转和平移,并且避免缩放或至少避免非均匀缩放.您只需换位上面的3x3元素,然后取反最后一行的x,y和z元素,就可以从这些矩阵快速计算出逆.

Many games exclusively use rotation and translation, and avoid scaling or at least avoid non-uniform scaling. You can quickly compute the inverse from such matrices by just transposing the upper 3x3 elements and then negating the x, y, and z elements of the last row.

如果您知道矩阵仅包含旋转和平移,而从未包含比例,则旋转矩阵只是上方的3x3元素.只要您的矩阵是同质的(即最后一列是[0 0 0 1]),您就可以从最后一行中读出译文:world.r[3]应该是(x, y, z, 1).

If you know your matrix only contains a rotation and translation, and never contains scale, then the rotation matrix is just the upper 3x3 elements. As long as your matrix is homogenous (i.e. the last column is [0 0 0 1]), you can just read out the translation from the last row: world.r[3] should be (x, y, z, 1).

如果您不熟悉DirectXMath,则应考虑在 DirectX工具包.它更自动地处理对齐复杂性,并包括方便的辅助程序(如Matrix::Translation),该辅助程序仅提取等效的world.r[3] x,y和z.

If you are new to DirectXMath, you should consider using the SimpleMath wrapper in the DirectX Tool Kit. It handles the alignment complexities a bit more automatically, and includes handy helpers like Matrix::Translation which just extracts the equivalent world.r[3] x, y, and z.

这篇关于翻译-旋转-使用XMMATRIX向后翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆