Sign in to follow this  

world <-> screen convert

This topic is 671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

XMMATRIX ViewProj = _view() * _projection();
XMVECTOR pointInWorld  = XMVector3Transform(worldPoint, ViewProj);

	float x = ((XMVectorGetX(pointInWorld) + 1.0f) / 2.0f) *_width;
	float y = ((-XMVectorGetY(pointInWorld) + 1.0f) / 2.0f) *_height;

XMVECTOR pointInScreen = DirectX::XMVectorSet(x, y, 0.0f, 0.0f); // works fine

	float xx = (((2.0f * x) / _width) - 1.0f);
	float yy = -(((2.0f * y) / _height) - 1.0f);
	float zz = DirectX::XMVectorGetZ(pointInWorld);

	XMMATRIX invView = XMMatrixInverse(NULL, ViewProj);
	XMVECTOR mousePos = XMVectorSet(xx, yy, zz, 1.0f);
	mousePos = XMVector3Transform(mousePosition, invView); // incorrect

Hi, i am trying to figure out why i cannot accurately convert between world and screen coordinates. The code above takes a point in world space and converts it into a screen position (). This seems to work fine, but when i do the opposite to get the original world coordinate, the result is wrong. The x coordinate is usually close but the y and z are tottaly off. Anyone care to explain this to me? 

Share this post


Link to post
Share on other sites

This topic is 671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this