Moving the camera in my 2D scene...

Started by
8 comments, last by Gary the Llama 16 years, 7 months ago
I've got my 2D scene setup using Direct3D 9. I'm using an orthographic view. I've got my sprite displaying on screen. The question is, how do I move the camera in the x and y directions? Here's a snippet from my game loop:

if (d3ddev->BeginScene())
{
	// Black background
	d3ddev->ColorFill(backbuffer, NULL, D3DCOLOR_XRGB(0, 0, 0));

	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	D3DXVECTOR3 pos;
	pos.x = 10.0f;
	pos.y = 20.0f;
	pos.z = 0.0f;

	RECT srcRect;

	srcRect.left = 1;
	srcRect.right = 32;
	srcRect.top = 1;
	srcRect.bottom = 32;

	sprite->Draw(spriteTexture, &srcRect, NULL, &pos, D3DCOLOR_XRGB(255, 255, 255));

	sprite->End();

	d3ddev->EndScene();
}

d3ddev->Present(NULL, NULL, NULL, NULL);

I'm not trying to do anything fancy right now... Just display a sprite and move the camera. I've got no camera movement code in there right now... So how would I have it automatically scroll right? Beforehand, in my Direct3D init code, I'm doing this, if it makes a difference.

D3DXMatrixOrthoLH(&Ortho2D, 800.0, 600.0, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

d3ddev->SetTransform(D3DTS_PROJECTION, &Ortho2D);
d3ddev->SetTransform(D3DTS_WORLD, &Identity);
d3ddev->SetTransform(D3DTS_VIEW, &Identity);

So how can I get that camera moving? :) [Edited by - Gary the Llama on September 18, 2007 8:50:31 PM]
Advertisement
I have no clue how Direct 3D transformations work, but either the vector at the fourth column or fourth row ( therefore the transpose ) of your 4x4 transformation matrix gives the translation...

if M is your matrix then x=M[0][3], y=M[1][3] and z=M[2][3] if D3D Matrices are loaded equally like OGL ones, or x=M[3][0], y=M[3][1], z=M[3][2] if D3D loads it transposed.
Unfortunately, I'm still lost as to how to do this... Any other suggestions?
Direct 3D transformations documentation from MSDN... http://msdn2.microsoft.com/en-us/library/bb206269.aspx

Quote:So how can I get that camera moving? :)

In real time? Simply change the values of the translation during the mainloop ( usually in dependence of time )
Getting closer (I think), but still no camera movement.

if (d3ddev->BeginScene()){	// Black background	d3ddev->ColorFill(backbuffer, NULL, D3DCOLOR_XRGB(0, 0, 0));        // I assume this is where I need to setup a D3DXMATRIX        // and then do a d3ddev->SetTransform(D3DTS_VIEW, &out)        // but I have no idea what vectors to pass in...        // Right now my view is set to identity, my projection        // is set to D3DXMatrixOrthoLH(&Ortho2D, 800.0, 600.0, 0.0f, 1.0f);        // if it makes a difference... 	sprite->Begin(D3DXSPRITE_ALPHABLEND);	D3DXVECTOR2 pos;	pos.x = zamboniSprite.x;	pos.y = zamboniSprite.y;	RECT srcRect;	srcRect.left = 1;	srcRect.right = 32;	srcRect.top = 1;	srcRect.bottom = 32;	D3DXVECTOR2 scaling(1.0f, 1.0f);			D3DXMATRIX spriteMatrix;	D3DXMatrixTransformation2D(&spriteMatrix, 0, 0, &scaling, 0, 0, &pos);	//D3DXMatrixTransformation2D(&spriteMatrix, 0, 0, &scaling, 0, 0, &D3DXVECTOR2(150.0f, 150.0f));	sprite->SetTransform(&spriteMatrix);	sprite->Draw(spriteTexture, &srcRect, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));	sprite->End();	d3ddev->EndScene();


I've included comments where I believe I would do my d3ddev->SetTransform(D3DTS_VIEW, &out) but I'm not 100% for sure on that and I have no idea what to pass into the matrix to get the camera moving.

Sorry for being so dumb, any help would be appreciated.
Anyone? I'd love some help on this...
Mods, if you think it's a good idea, can we move this to the DirectX forum please?
Okay so my FVF has D3DFVF_XYZRHW in it and I think that's causing a problem. Is this what's causing my view matrix not to work?
Quote:Original post by Gary the Llama
Okay so my FVF has D3DFVF_XYZRHW in it and I think that's causing a problem. Is this what's causing my view matrix not to work?


yes, that format assumes that there is no need to translate from model space to world space or any transformation like that, everything is based off of screen coordinates.

Your idea of using a 3D camera in a 2D world in my opinion is a waste of time.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Quote:Original post by freeworld
Quote:Original post by Gary the Llama
Okay so my FVF has D3DFVF_XYZRHW in it and I think that's causing a problem. Is this what's causing my view matrix not to work?


yes, that format assumes that there is no need to translate from model space to world space or any transformation like that, everything is based off of screen coordinates.

Your idea of using a 3D camera in a 2D world in my opinion is a waste of time.


Care to elaborate why you think it's a waste of time?

How would you do it?

This topic is closed to new replies.

Advertisement