• Advertisement
Sign in to follow this  

Setting up Direct3D Camera for 2D game

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

My first post here, i was going to search the forums for similar topics but I could not find the search tool for the forums.

 

so here is my camera setup:

        HRESULT result;
	result = Direct3D_Object->CreateDevice(GameSettings.GraphicAdapter, GameSettings.DeviceType, 
                                               GameSettings.WindowHandle, GameSettings.HardwareAccelaration,
                                               &D3DPParameters, &D3D_Device);

	if (result == D3D_OK)
	{
		D3DXMATRIX V;
		D3DXVECTOR3 pos(0.0f, 0.0f, -400.0f);
		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
		D3DXMatrixLookAtLH(&V, &pos, &target, &up);
		D3D_Device->SetTransform(D3DTS_VIEW, &V);

		D3DXMATRIX P;
		float width = 800;
		float height = 600;
		D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.5f,
		width/height, -400.0f, 0.0f);
		D3D_Device->SetTransform(D3DTS_PROJECTION, &P);

so I used D3DX_PI*0.5f for the angel so that I can easly set the camera distance away from the origin (0, 0, 0), just by setting the z for the camera = - half the screen width...

 

6LuMk.png

 

this way my camera wont show anything that is behind the z = 0!!

 

 

and here the code to draw the spirit:

LPD3DXSPRITE SpriteTest;
LPDIRECT3DTEXTURE9 TestTexture;
LPCTSTR ImageName = L"test.jpg";

D3DXMATRIX TestMat;
D3DXVECTOR2 ScaleVector;
D3DXVECTOR3 PositionVector;

PositionVector.x = 0;
PositionVector.y = 0;
PositionVector.z = 0;

ScaleVector.x = (float) 400/566;
ScaleVector.y = (float) 400/566;

D3DXCreateTextureFromFileEx(D3D_Device, ImageName, 566, 800, D3DX_DEFAULT, 0, 
                            GameSettings.BackBufferFormat, D3DPOOL_MANAGED, 
                            D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &TestTexture);

D3DXMatrixTransformation2D(&TestMat, NULL, NULL, &ScaleVector, NULL, NULL, NULL);

SpriteTest->SetTransform(&TestMat);
SpriteTest->Draw(TestTexture, NULL, NULL, &PositionVector, D3DCOLOR_XRGB(255, 255, 255));

however this comes up when I run the game?

 

 

 

6LtsR.jpg

 

please if you need any more information let me know

 

thank you for your time.

 

Also if anyone can tell me how to search the forums that also would help.

 

Edit: I forgot to mention client dimentions are 800*600 I confirmed them using GetClientRect

thanx again for you help.

 

Edit: Iam using Directx9 (Directx SDK June 2010)

 

Edit: test.jpg size is 566 x 800 pixel

Edited by CppVoidppC

Share this post


Link to post
Share on other sites
Advertisement

For 2D game you should use 

D3DXMatrixOrthoLH(&matOrtho,640, 480, -1000, 1000);

You dont have to worry about camera or things... Any thing u draw will be exactly its size !

 

because it's more simple. 

Edited by greenpig83

Share this post


Link to post
Share on other sites

thank you... really... thanx for taking the time to answer my question.

hat off for you.cool.png

 

is there anything i should do to mark your answer as the perfect answer or the one that i accept as the solution to my problem other than the one up thing?

wish you good luck....

 

For 2D game you should use 

D3DXMatrixOrthoLH(&matOrtho,640, 480, -1000, 1000);

You dont have to worry about camera or things... Any thing u draw will be exactly its size !

 

because it's more simple. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement