Archived

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

knine

2D Transformations in 3D

Recommended Posts

knine    122
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

Share this post


Link to post
Share on other sites
CrazedGenius    156
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.

Share this post


Link to post
Share on other sites
knine    122
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

Share this post


Link to post
Share on other sites
Mark Duffill    156
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.

Share this post


Link to post
Share on other sites
knine    122
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

Share this post


Link to post
Share on other sites