Skip to main content
edited title
Link
Tetrad
  • 30.1k
  • 12
  • 95
  • 143

Direct3D - Zooming into Mouse Position

deleted 2595 characters in body
Source Link
roohan
  • 196
  • 4

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.

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.

Tweeted twitter.com/#!/StackGameDev/status/272550640155230210
added 2595 characters in body
Source Link
roohan
  • 196
  • 4

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.

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.

deleted 4 characters in body
Source Link
roohan
  • 196
  • 4
Loading
Source Link
roohan
  • 196
  • 4
Loading