Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BauerGL

Trouble using D3DXVec3Project()

This topic is 5271 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

Hi, I'm creating a 3D airplane/dogfight kind of game and I would like to draw the players' names above their models. So I need to convert my players' world coordinates into screen coordinates, and I plan on using D3DXVec3Project() to achieve that. This is how I'm doing it now:
// Draw player names at their position

D3DVIEWPORT9 viewport;
mp_graphics->get_device()->GetViewport( &viewport );
D3DXMATRIX proj, view;
mp_graphics->get_device()->GetTransform( D3DTS_PROJECTION, &proj );
mp_graphics->get_device()->GetTransform( D3DTS_VIEW, &view );
m_smallFont.set_color( D3DCOLOR_XRGB( 200, 0, 0 ) );
for( int i = 0; i < MAX_PLAYERS; ++i )
{
	if( m_players[i].getConnected() )
	{
		// Get the position of the player

		BVector3 pos1 = m_players[i].getPos();
		D3DXVECTOR3 pos2( pos1.x, pos1.y, pos1.z );

		// Calculate the same world matrix used to position the player

		D3DXMATRIX trans, rot, orient, result;
		D3DXMatrixTranslation( &trans, pos1.x, pos1.y, pos1.z );
		D3DXMatrixRotationY( &rot, D3DXToRadian( 180.0f ) );
		D3DXMatrixRotationQuaternion( &orient, &m_players[i].getOrientation() );
		result = rot * orient * trans;
				
		D3DXVECTOR3 transpos;
		D3DXVec3Project( &transpos, &pos2, &viewport, &proj, &view, &result);

		m_smallFont.draw( mp_graphics->get_sprite(), (int)transpos.x, (int)transpos.y, false, "%s", m_players[i].getName().c_str() );
	}
}
The text doesn't appear at the player's position though, it just moves around in a strange way (the player is always in the center of the screen). I'm a bit unsure about the matrix stuff here, which matrix should view be? The same one that is used to position my "camera"? Should the world matrix be the same one used to position the player? Should the input vector be the player's position? I'm using the default viewport automagically created by D3D, should I define my own? Does creating my window "centered" on the screen have any impact on the viewport (instead of letting it be created at 0,0)? Am I extracting the screen x,y correctly? I'm not using a pure device and I have tested that GetViewport() and GetTransform() return ok. Have you got any solution/tips? [edited by - BauerGL on March 6, 2004 8:59:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by BauerGL
result = rot * orient * trans;

Can you do this? I thought you always had to use D3DXMatrixMultiply to multiply matrices together. Also, since you only actually need the object''s position, do you need to add in the rotation and stuff?


Dustin Franklin
Mircrosoft DirectX MVP

Share this post


Link to post
Share on other sites
Hi circlesoft

Yea, D3DXMATRIX comes with overloaded operators so result = rot * orient * trans does work. Though if you use D3DMATRIX (notice no ''X'') you have to use D3DXMatrixMultiply() etc. I don''t think I have to use the rotation and orientation matrices but it doesn''t work any better without them.

Share this post


Link to post
Share on other sites
Does anyone know which view and world matrices I am supposed to use in D3DXVec3Project()? I.e should it be the view matrix that I use to position my camera and should it be my world matrix used to position my object?

Share this post


Link to post
Share on other sites
Yes, the world matrix you use is the same one used to position your object. The view matrix used is the one used to position your camera.

The idea of D3DXVec3Project is that you''re trying to make a vertex go through all the transformations that the hardware would normally apply to it when rendering your geometry. That is why you must also apply the projection matrix as well as the viewport, because these are used to calculate the final screen-space location of the vertex.

neneboricua

Share this post


Link to post
Share on other sites
Hmm still won''t work. The thing is that when I''m using the world matrix used to position my player and the view matrix used to position my camera, the text ends up at the world coordinates but on screen. So if my player is at world coords 500,400,300 the text ends up on screen at 500,400. Got any other ideas?

Share this post


Link to post
Share on other sites
Are you sure you''re using the right matrices? What about the viewport? I believe that you can call GetViewport even on a PURE device and it will work.

neneboricua

Share this post


Link to post
Share on other sites
A small checklist, as im unsure what uve done, but i hope this helps:

1: Get a Vector that represents the planes position
2: Transform this by the viewmatrix
3: Transform that by the projection matrix = Vector V
4: Get your viewpoints rect W = width, H = height

screenX = V.x + W/2;
screenY = V.y + H/2;

5: Add a small amount to the screenY to put the text above the plane on the screen (if thats where u want it)

6: horay ur done, i hope this helps

Yratelev

Share this post


Link to post
Share on other sites
neneboricua19: Yes I''m sure. I''m calculating my world matrix in the exact same way as I''m calculating my plane''s position. I tried saving my view matrix right after positioning my camera, same result though. I also tried setting the projection matrix and viewport manually just before calling D3DXVec3Project(), but no luck.

xyzzy00: Intel P4 Hyperthreading etc.

Yratelev: Thanks for the tips, gonna try it out as soon as possible.

Thanks all again!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!