Sign in to follow this  

Moving the camera in my 2D scene...

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

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]

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 3735 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this