2D Transformations in 3D

Started by
5 comments, last by knine 22 years, 2 months ago
I want to write a 2D game in Direct3D 8. I''m having a problem understanding how to make the transformations in 3D. I know that I setup my perspective as Orthogonal to do 2D games in 3D. But when I transformed with DirectDraw it was like: if(object.x < 640) { object.x+=vel * fTimeDelta; } if(object.y < 480) { object.y+=vely * fTimeDelta; } How do I do the same types of transformations using a D3DXMATRIX with DirectGraphics 8? Thanks! knine
Advertisement
Look at the docs for D3DXMatrixTranslation

Build a translation matrix, pass it to the device, and render.

Also, check out the "Tutorial" samples in the SDK. There is one that handles VERy basic matrix stuff.
Author, "Real Time Rendering Tricks and Techniques in DirectX", "Focus on Curves and Surfaces", A third book on advanced lighting and materials
Okay. I''m setting things up like so:

D3DXMATRIX matOrtho2D;D3DXMATRIX matIdentity;D3DXMatrixOrthoLH(&matOrtho2D, 640.0f, 480.0f, 0.0, 1.0);D3DXMatrixIdentity(&matIdentity);m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matOrtho2D);m_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity);m_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity);


and when I do my transformation to move the sprite I do so like this:

D3DXMATRIX matPosition;	D3DXMatrixTranslation(&matPosition, m_vPos.x, m_vPos.y, 0.0f);m_pd3dDevice->SetTransform(D3DTS_WORLD, &matPosition);


does that look right? It seems to be running okay, except for the fact that I can''t see my sprite.

Thanks in advance for your help!
knine
You could also use pre-transformed and lit vertices,

  /////////////////////////////////////////// sprite vert structure#define D3DFVF_SPRITEVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)struct SPRITE_VERTEX{    f32 x, y, z, rhw;	// The transformed position for the vertex.    u32 ARGB;		    // The vertex color.	f32 tu, tv;			// tex coords};/////////////////////////////////////////  


Then just define the x,y in screen space (0,0 is top left), set z to 1, and rhw to 1. (between 0 and 1 for depth if you are going to use a zbuffer )

Of course you''ll have to manage the sprite and modify its x,y''s when it moves.
Thanks. Things are working now. Now my problem is that (0, 0) is at the center of the screen. How do I get it at the top left corner of the screen?

Thanks!
knine
maybe with the view matrix?
hmmmmmmmmmmmm...
--- foobarWe push more polygons before breakfast than most people do in a day
Deerrr!! Thanks foobar. I can''t believe I overlooked that!

knine

This topic is closed to new replies.

Advertisement