Archived

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

Trouble using D3DXVec3Project()

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

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 on other sites
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 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 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 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 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 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 on other sites
What kind of processor does your system have? Intel? AMD?

xyzzy

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 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.

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

Thanks all again!

Share on other sites
No luck

I'm using this code now:
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, 200, 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);						D3DXVECTOR4 out1, out2;		D3DXVec3Transform( &out1, &pos2, &view );		D3DXVec4Transform( &out2, &out1, &proj );		int screenX = (int)out2.x + m_width/2;		int screenY = (int)out2.y + m_height/2;		m_smallFont.draw( mp_graphics->get_sprite(), screenX, screenY, false, "%s", m_players[i].getName().c_str() );	}}

Was that how you meant? No matter what I put into pos2 the screen coords always end up in the middle of the screen.

[edited by - BauerGL on March 10, 2004 9:41:26 AM]

[edited by - BauerGL on March 10, 2004 9:42:11 AM]

Share on other sites
Dang!

I cant spot anything wrong with that. Run in the debugger then break just before the vector transforms, and check both matricies, and vectors AS they get transforms, ie run the whole thing line by line. Thats all I can suggest, sorry!

Good Luck

Yratelev