Direct3D - Zooming into Mouse Position

Posted by roohan on Game Development See other posts from Game Development or by roohan
Published on 2012-11-24T18:01:49Z Indexed on 2012/11/24 23:21 UTC
Read the original article Hit count: 226

Filed under:
|

I'm trying to implement my camera class for a simulation. But I cant figure out how to zoom into my world based on the mouse position. I mean the object under the mouse cursor should remain at the same screen position. My zooming looks like this:

VOID ZoomIn(D3DXMATRIX& WorldMatrix, FLOAT const& MouseX, FLOAT const& MouseY) {      
    this->Position.z = this->Position.z * 0.9f;
    D3DXMatrixLookAtLH(&this->ViewMatrix, &this->Position, &this->Target, &this->UpDirection);
}

I passed the world matrix to the function because I had the idea to move my drawing origin according to the mouse position. But I cant find out how to calculate the offset in to move my drawing origin. Anyone got an idea how to calculate this?

Thanks in advance.

SOLVED

Ok I solved my problem. Here is the code if anyone is interested:

VOID CAMERA2D::ZoomIn(FLOAT const& MouseX, FLOAT const& MouseY) {
    // Get the setting of the current view port.
    D3DVIEWPORT9 ViewPort;
    this->Direct3DDevice->GetViewport(&ViewPort);

    // Convert the screen coordinates of the mouse to world space coordinates.
    D3DXVECTOR3 VectorOne;
    D3DXVECTOR3 VectorTwo;

    D3DXVec3Unproject(&VectorOne, &D3DXVECTOR3(MouseX, MouseY, 0.0f), &ViewPort, 
        &this->ProjectionMatrix, &this->ViewMatrix, &WorldMatrix);
    D3DXVec3Unproject(&VectorTwo, &D3DXVECTOR3(MouseX, MouseY, 1.0f), &ViewPort, 
        &this->ProjectionMatrix, &this->ViewMatrix, &WorldMatrix);

    // Calculate the resulting vector components.
    float WorldZ = 0.0f;
    float WorldX = ((WorldZ - VectorOne.z) * (VectorTwo.x - VectorOne.x)) /
        (VectorTwo.z - VectorOne.z) + VectorOne.x;
    float WorldY = ((WorldZ - VectorOne.z) * (VectorTwo.y - VectorOne.y)) /
        (VectorTwo.z - VectorOne.z) + VectorOne.y;

    // Move the camera into the screen.
    this->Position.z = this->Position.z * 0.9f;
    D3DXMatrixLookAtLH(&this->ViewMatrix, &this->Position, &this->Target, &this->UpDirection);

    // Calculate the world space vector again based on the new view matrix,
    D3DXVec3Unproject(&VectorOne, &D3DXVECTOR3(MouseX, MouseY, 0.0f), &ViewPort, 
        &this->ProjectionMatrix, &this->ViewMatrix, &WorldMatrix);
    D3DXVec3Unproject(&VectorTwo, &D3DXVECTOR3(MouseX, MouseY, 1.0f), &ViewPort, 
        &this->ProjectionMatrix, &this->ViewMatrix, &WorldMatrix);

    // Calculate the resulting vector components.
    float WorldZ2 = 0.0f;
    float WorldX2 = ((WorldZ2 - VectorOne.z) * (VectorTwo.x - VectorOne.x)) / 
        (VectorTwo.z - VectorOne.z) + VectorOne.x;
    float WorldY2 = ((WorldZ2 - VectorOne.z) * (VectorTwo.y - VectorOne.y)) /
        (VectorTwo.z - VectorOne.z) + VectorOne.y;

    // Create a temporary translation matrix for calculating the origin offset.
    D3DXMATRIX TranslationMatrix;
    D3DXMatrixIdentity(&TranslationMatrix);

    // Calculate the origin offset.
    D3DXMatrixTranslation(&TranslationMatrix, WorldX2 - WorldX, WorldY2 - WorldY, 0.0f);

    // At the offset to the cameras world matrix.
    this->WorldMatrix = this->WorldMatrix * TranslationMatrix;
}

Maybe someone has even a better solution than mine.

© Game Development or respective owner

Related posts about c++

Related posts about directx