簡體   English   中英

DirectX 11基於旋轉移動對象

[英]directx 11 move object based on rotation

嗨,我是DirectX 11的新手,我只是想讓我的游戲對象根據它的旋轉向前移動。 這就是我所擁有的,但是無論我是否轉動它,它只會沿一條直線移動。

void Object::MoveObject(float x, float y, float z)
{

    Position.x += x;
    Position.y += y;
    Position.z += z;

    _translate = XMMatrixIdentity();
    _translate *= XMMatrixTranslation(Position.x, Position.y, Position.z);

    XMVECTOR forward = { Position.x,Position.y,Position.z };
    XMFLOAT4X4 currrentWorld = GetWorld();
    XMMATRIX currentWorldM = XMLoadFloat4x4(&currrentWorld);

    XMVECTOR scale = currentWorldM.r[0];
    XMVECTOR rotation = currentWorldM.r[1];
    XMVECTOR translation = currentWorldM.r[2];
    forward *= currentWorldM.r[3];

    forward = XMVector3Normalize(forward);

    translation = XMVectorAdd(translation, forward);

    XMStoreFloat4x4(&_world, XMMATRIX(scale, rotation, translation, forward));

    UpdateWorld();
}

矩陣的第一行不包含比例,第二行不包含旋轉,第三行不包含平移。 它們比這更復雜。

要分解矩陣,可以使用XMMatrixDecompose。 要重新組合,可以使用XMMatrixTranslationFromVector,XMMatrixRotationQuaternion,XMMatrixScalingFromVector,然后將結果相乘。

幸運的是,無需移動矩陣就可以在對象的局部坐標中移動對象(如果這就是您想要的)。 您需要使用XMMatrixTranslationFromVector構造一個localTranslation矩陣,並將您的世界矩陣更新為XMMatrixMultiply( localTranslation, _world )

PS我推薦DirectX Toolkit庫用於這類事情。 特別是如果您不熟悉DirectX。 SimpleMath.h標頭包含這些向量和矩陣周圍易於使用的包裝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM