sofakng

Two questions...

1) How can I create resolution inpendant graphics For example, if I draw a box that takes up half of the screen in 640, 480 it should look the EXACT same for somebody using 1024, 768 resolution.

2) How can I create a 2D camera I get the term "viewport" and "camera" confused so let me clarify:

Let's say I have a side-scrolling game (or better yet, a Worms / Scorched Earth clone) and the user can only see certain parts of the "world" at once. As you user moves the mouse I'd like the change the player's view (eg. the camera).

Thanks for your help!



Re: XNA Game Studio Express How can I have resolution independant graphics? ...and how can I create a 2D camera?

Bill Reiss

First on question 1...I assume you're talking about 2D graphics using SpriteBatch here, since the 3D camera and view settings will take care of this automatically. In 2D, my first idea was to override the SpriteBatch methods for Draw and scale in there, but I'm happier now with using the standard SpriteBatch.Draw() methods that include a scale parameter, and calculating my position and scale based on a "base" resolution and the current resolution.

So basically I assume that I'm writing a game for 1024x768, and do all of my positioning and scaling assuming that resolution. Then in before calling Draw, I calculate my target position and scale and draw based on that. I also calculate an xpadding and ypadding so that I can handle windows with a different aspect ratio, and figure that into the calculations, giving a letterbox effect.

On question 2, you could just figure your world offset into the above drawing routines to handle that. You wouldn't really have to deal with the camera. To speed things up, you can figure out what is actually visible before drawing.






Re: XNA Game Studio Express How can I have resolution independant graphics? ...and how can I create a 2D camera?

Pocketmnky

Both of those problems just require a little bit of extra data saved in a creative location.

1) Build a ScreenShape class that exposes a Vector2 Size field. That Size field will basically act as the Scale factor for your drawable shape so you should set your sizes as a portion of the screen; say (0.5f, 0.5f) would be half of the viewable area, etc.

When you go to render each of your screen shapes, you can just be sure to set the renderable size (either in your SpriteBatch.Draw() call directly or Effect.World matrix for 3D objects or textured quads or whatever) as Vector2(GraphicsDevice.Viewport.X, GraphicsDevice.ViewPort.Y) * Size.

You can do the exact same thing with postion as well, it's just a little different.

2) A 2D camera is really just a 3D camera that only ever points along the Z-axis. You can always just set your effect.view matrix to something like: view = Matrix.CreateLookAt(new Vector3(camera.Position, 1000f), new Vector3(camera.Position, 0f), Vector3.UnitY); Assuming that you have a camera object that exposes a Vector2 Position. Mind you this would fix your camera's distance at 1000f so you could also make that into a variable if your game requires it.






Re: XNA Game Studio Express How can I have resolution independant graphics? ...and how can I create a 2D camera?

sofakng

Thanks for the replies!

I'm a bit confused on the ScreenShape class exposing a size field, but I do understand Bill Reiss's idea of simply multiplying everything by a scale factor. Are you basically saying the same thing

Also, is it better to keep adjusting the view matrix every frame or to adjust the rendering positions based on the camera position

It would be nice to simply draw everything at a consistent position (eg. my sprite is at 50, 50 which is on-screen is my camera is at 0, 0 but it will be automatically drawn offscreen if my camera [eg. view matrix] is moved further away, etc...)

If I choose to not manipulate the matrixes, I can just have a function that figures out the rendering position of any object:

[ Pseudo-code ]

public vector2 GetRenderPosition(vector2 OriginalPosition)
{
vector2 result;

result.x = (OriginalPosition.x + Camera.X) * ScreenWidthRatio;
result.y = (OriginalPosition.y + Camera.Y) * ScreenHeightRatio;

return result;
}





Re: XNA Game Studio Express How can I have resolution independant graphics? ...and how can I create a 2D camera?

Pocketmnky

There's probably quite a few ways to pull it off. Basically what you need is:

1) a variable that describes the size of the object on the screen

and

2) a variable that describes the size of the screen.

since you want the size of the object to be a fraction of the size of the screen (such as 1/2 the screen's width and 1/4 of the screen's height) the easiest way to do that is to store it as a Vector2(0.5f, 0.25f) and then in your Draw(gameTime) call you could say:

Vector2 ScreenRelativePosition = ScreenSize * ObjectPosition;
Vector2 ScreenRelativeSize = ScreenSize * ObjectSize;
SpriteBatch.Draw(Texture, ScreenRelativePosition, ScreenRelativeSize)

something like that.






Re: XNA Game Studio Express How can I have resolution independant graphics? ...and how can I create a 2D camera?

Bill Reiss

It may be best to just start and get it working at a certain resolution, and worry about scaling later. It's not something that you should have a much more difficult time adding later, as long as you wrap the spriteBatch.Draw logic in a method so that you don't have spriteBatch.Draw calls all over the place.

Bill