Jump to content
  • Advertisement
Sign in to follow this  

Game camera zoom for a sprite based 2d game

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

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




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->Draw(spriteData.srv, XMFLOAT2(spriteData.x, spriteData.y), nullptr, Colors::White);


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.



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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!