Jump to content

  • Log In with Google      Sign In   
  • Create Account

world <-> screen convert

  • You cannot reply to this topic
2 replies to this topic

#1 qwertyPrince   Members   -  Reputation: 102

Like
0Likes
Like

Posted 10 February 2016 - 09:58 PM

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? 



Sponsor:

#2 qwertyPrince   Members   -  Reputation: 102

Like
0Likes
Like

Posted Yesterday, 01:26 AM

i could not figure out where i was going wrong and decided to use the XNA project and unproject which work a charmwink.png



#3 vinterberg   Members   -  Reputation: 476

Like
0Likes
Like

Posted Today, 10:49 AM

I think you had to divide xyz by w :)


.:vinterberg:.

 






PARTNERS