I have a 3D cube that can be rotated with the mouse to show any side of the sphere.I want to be able to click a point on the cube and draw a circle around that point at a fixed height(so i dont need depth information), however i run into a problem where i cant translate the screen coridiantes to the worldspace coordinates. What i have attempted:

  • get mouse click position(x,y)

  • normalise the screen position(-1 to 1)

  • create a vector with the z coordinate as 1

  • multiply the vector by (inverseProjection x inverseView)

However, this does not give me the world coordinates as i would have expected. Where am i going wrong?

The depth information is not needed form this, i only need to map the mouse click point (x,y) to the world point (x,y,z) where z is fixed.

DirectX::XMMATRIX projection = projectionmatrix();
DirectX::XMMATRIX view = viewmatrix();

DirectX::XMMATRIX invProjectionView = DirectX::XMMatrixInverse(&DirectX::XMMatrixDeterminant(view *  projection), (view *  projection));
//invViewProjection = invView * invProjection;

float x = (((2.0f * mouseX) / _viewport.Width) - 1);
float y = -(((2.0f * mouseY) / _viewport.Height) - 1);

DirectX::XMVECTOR mousePosition = DirectX::XMVectorSet(x, y, 1.0f, 0.0f);

mouseInWorldSpace = DirectX::XMVector3Transform(mousePosition, invProjectionView);

Edit: The fixed axis is the y-axis, the z and x axis are the changing axis

Edit: I have revised the question after realising my mistakes in the initial question.

enter image description here

The x, y, and z are positioned as shown above. I want to be able to click the cube surface and drwaw the shape at a point with a fixed y-offset from the click point.

I am not able to draw a plane on the surface of the cube due to the nature in which the cube is generated hence the need for the screen to world coords.

  • 2
    \$\begingroup\$ Possible duplicate of Problems with converting from Screen Space to World Space \$\endgroup\$
    – Vaillancourt
    Feb 5 '16 at 2:21
  • \$\begingroup\$ The question linked relates to depth information. i dont need the depth \$\endgroup\$ Feb 5 '16 at 2:30
  • \$\begingroup\$ Yes, and it seems your arbitrary Z = 1 turns out to be on the far clipping plane side of the frustum, which is exactly what the answer is explaining. \$\endgroup\$
    – Vaillancourt
    Feb 5 '16 at 2:41
  • \$\begingroup\$ Thanks for the answer, i have tried to understand but still dont get whats going on. \$\endgroup\$ Feb 5 '16 at 7:29
  • \$\begingroup\$ Have you tried using XMVector3Unproject? \$\endgroup\$ Feb 8 '16 at 20:35

Not 100% sure of what you are trying to draw where, but neither Z=0 nor Z=1 correspond to the location of the cube in world space. Both points, however, do define the ray shown below in red. You can run an intersection test between that ray and the cube's BoundingBox to determine the exact world coordinate of collision. "Depth", which you claim to not need, is actually the calculated length of the collision vector. The Z of your cursor position comes directly from it.

CollisionPoint = RayStart + (RayDirection * CollisionDistance);

If you project CollisionPoint to screen-space, the resulting Z value will approximately match the value that is written to the depth buffer, if one is in use, and enabled.

If my assumptions are correct, you would draw your "3D circle" centered at
World(24.15, FixedHeight, 50).

enter image description here


Ok so this is my solution:

since the troube was broght about by the missing z coordinate, it check for intersection with the plane and store the z-coordinate. i then use this same coordinate when unprojecting using the code above and it seems to work fine. Thanks for the help


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.