Sign in to follow this  
SteveHatcher

Game camera zoom for a sprite based 2d game

Recommended Posts

Hi All,

 

I am confused about the game 'camera' for a 2d sprite based game

 

I have some code that initializes a win32 window and then a directX device. The code to initialize the directX window consists of the following major DirectX11 calls (the arrows just indicate the order of these calls):

 

D3D11CreateDevice -> CreateSwapChain -> CreateRenderTargetView -> OMSetRenderTargets -> RSSetViewports

 

I then load the textures I want to use using DirectxTK's CreateDDSTextureFromFile()

 

To draw my .dds texture I call

 

graphics->drawAsset(spriteData);

 

where spriteData is a structure that contains my textures shader resource view.

 

The drawSprite function uses the DirectXTK to draw my sprite as per their documentation:"

 

void Graphics::drawAsset(const SpriteData &spriteData)
{
    g_Sprites->Begin(SpriteSortMode_Deferred);
    g_Sprites->Draw(spriteData.srv, XMFLOAT2(spriteData.x, spriteData.y), nullptr, Colors::White);
    g_Sprites->End();
}

 

Somehow in all of this my sprite appears on screen with its top left pixel location at (spriteData.x, spriteData.y).

 

What I want to do now is zoom the camera out quite a bit, to enter kind of a 'debug' mode, where I can see the location of sprites and objects off screen.

 

If I was dealing with 3d objects I have a feeling I could do this quite easily by modifying the view matrix. The directX tool kit has a nice example "SimpleSample_Desktop_2012_Win8" where they draw some models and a sprite. Even in their example though, when I modify the view matrix only the 3d objects move, the 2d sprite stays exactly where it is e.g. changing -6.0f to -20.0f in the line...

 

XMVECTOR Eye = XMVectorSet( 0.0f, 3.0f, -6.0f, 0.0f );

 

So I am a bit confused in the view matrix's role when dealing just with 2d sprites like I am,

 

And also how to include an easy zoom function similar to the way the view matrix works in 3d.

 

Thanks

Edited by SteveHatcher

Share this post


Link to post
Share on other sites

SpriteBatch rendering is in screen space, unrelated to a "viewpoint," camera or eye position. Your view matrix isn't used by the shader that renders the sprites. Because sprites are intended to be in 2D screen space, and not part of a 3D world, it's not clear what you mean by "zooming" or changing the "view." Those terms apply to objects which you can move/rotate, or view from different angles, somehow related to camera parameters.

 

In any case, take a look at the docs for SpriteBatch. In particular the section Custom Transform - "SpriteBatch::Begin also has a transformMatrix parameter, which can be used for global transforms such as scaling or translation of an entire scene. It otherwise defaults to matrix identity."

 

If you want something to happen to your sprite as a result of zooming, you'll have to choose an appropriate scale factor to zoom in or out. In addition, scaling will still be relative to the origin (N.B., screen) position of the sprite. You'll also have to decide how you want to move the sprite origin in a way that appeals to you.

Share this post


Link to post
Share on other sites

You might want to move the SpriteBatch begin and end calls outside your drawasset method.

 

The begin and end calls batch the sprites together for many reasons one of them being more optimal in operation with the graphics driver and hardware by making a smaller number of system calls.

 

Just my 2p worth...

Share this post


Link to post
Share on other sites

@Buckeye Thanks, that worked perfectly. I used a scaling matrix to scale the whole scene down, and the draw command allows changing of the origin so I can place the scene right in the middle of the screen and see all around the outside.

 

@braindigitalis Thanks for picking up on that. I moved the begin and end calls to my render function, where all the draw() calls are made.

Share this post


Link to post
Share on other sites

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