简体   繁体   中英

DirectX 11 Translation/Rotation Issue

I can translate my 2d image to 0, 0 using the below code.

D3DXMATRIX worldMatrix, viewMatrix, orthoMatrix, rotation, movement;

// Get the world, view, and ortho matrices from the camera.
m_camera.GetViewMatrix(viewMatrix);
m_camera.GetWorldMatrix(worldMatrix);
m_camera.GetOrthoMatrix(orthoMatrix);

// Move the texture to the new position
D3DXMatrixTranslation(&movement, ((m_VerticeProperties->screenWidth / 2) * -1) + m_posX, 
    (m_VerticeProperties->screenHeight / 2) - m_posY, 0.0f);

worldMatrix = movement;

//float m_rotationZ = -90 * 0.0174532925f;
//D3DXMatrixRotationYawPitchRoll(&rotation, 0, 0, m_rotationZ);
//worldMatrix = rotation;

// Give the bitmap class what it needs to make source rect
m_bitmap->SetVerticeProperties(m_VerticeProperties->screenWidth, m_VerticeProperties->screenHeight, 
    m_VerticeProperties->frameWidth, m_VerticeProperties->frameHeight, m_VerticeProperties->U, m_VerticeProperties->V);

//Render the model (the vertices)
m_bitmap->Render(m_d3dManager.GetDeviceContext(), flipped);

//Render the shader
m_shader->Render(m_d3dManager.GetDeviceContext(), m_bitmap->GetIndexCount(), worldMatrix, viewMatrix, 
    orthoMatrix, m_bitmap->GetTexture(), m_textureTranslationU, m_VerticeProperties->translationPercentageV);

The result:

在此处输入图片说明

I can also rotate the image with this code:

D3DXMATRIX worldMatrix, viewMatrix, orthoMatrix, rotation, movement;

// Get the world, view, and ortho matrices from the camera.
m_camera.GetViewMatrix(viewMatrix);
m_camera.GetWorldMatrix(worldMatrix);
m_camera.GetOrthoMatrix(orthoMatrix);

//// Move the texture to the new position
//D3DXMatrixTranslation(&movement, ((m_VerticeProperties->screenWidth / 2) * -1) + m_posX, 
//  (m_VerticeProperties->screenHeight / 2) - m_posY, 0.0f);

//worldMatrix = movement;

float m_rotationZ = 90 * 0.0174532925f;
D3DXMatrixRotationYawPitchRoll(&rotation, 0, 0, m_rotationZ);
worldMatrix = rotation;

// Give the bitmap class what it needs to make source rect
m_bitmap->SetVerticeProperties(m_VerticeProperties->screenWidth, m_VerticeProperties->screenHeight, 
    m_VerticeProperties->frameWidth, m_VerticeProperties->frameHeight, m_VerticeProperties->U, m_VerticeProperties->V);

//Render the model (the vertices)
m_bitmap->Render(m_d3dManager.GetDeviceContext(), flipped);

//Render the shader
m_shader->Render(m_d3dManager.GetDeviceContext(), m_bitmap->GetIndexCount(), worldMatrix, viewMatrix, 
    orthoMatrix, m_bitmap->GetTexture(), m_textureTranslationU, m_VerticeProperties->translationPercentageV);

The result:

在此处输入图片说明

I thought multiplying the translation and rotation matrices and setting them = to the world matrix would allow me to see both effects at once.

D3DXMatrixTranslation(&movement, ((m_VerticeProperties->screenWidth / 2) * -1) + m_posX, 
    (m_VerticeProperties->screenHeight / 2) - m_posY, 0.0f);

float m_rotationZ = 90 * 0.0174532925f;
D3DXMatrixRotationYawPitchRoll(&rotation, 0, 0, m_rotationZ);
worldMatrix = rotation * movement;

It doesn't. The image no longer appears on the screen.

Can anyone tell me what im doing wrong? Thanks.

just do world * -translate * rotation * translate it will make you rotate local here my code for example

void Ojbect::RotZ(float angle, Vec3 origin)
{
    Mat4 w, rz, t;
    rz.RotZ(angle);
    t.Translation(-origin.x, -origin.y, 0);
    w = t * rz * -1 * t;
    Vec4 newPos;
    for (int i = 0; i < countV; i++)
    {
        Vec3 pos(vertex[i].x, vertex[i].y, 1);
        newPos.Transform(pos, w);
        vertex[i].x = newPos.x;
        vertex[i].y = newPos.y;
    }
    UpdateVertex(countV);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM