Jump to content

  • Log In with Google      Sign In   
  • Create Account

world <-> screen convert


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


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

#1 qwertyPrince   Members   -  Reputation: 116

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? 



#2 qwertyPrince   Members   -  Reputation: 116

Like
0Likes
Like

Posted 11 February 2016 - 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: 864

Like
1Likes
Like

Posted 12 February 2016 - 10:49 AM

I think you had to divide xyz by w :)


.:vinterberg:.

 


#4 qwertyPrince   Members   -  Reputation: 116

Like
0Likes
Like

Posted 15 February 2016 - 02:14 AM

I think you had to divide xyz by w smile.png

 

yea, thats what was missing! thanks






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS