Viewport coordinates/boundaries

Dec 29, 2011 at 2:37 PM
Edited Dec 29, 2011 at 3:36 PM

I can't seem to figure out the translation between map coordinates and screen coordinates.

Under the process of learning, I noticed a few things...


First off:

viewport = new xTile.Dimensions.Rectangle(new Size(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));

doesn't seem to clip the bottom and right sides of my map to match my screen's dimensions. Instead, it continues beyond the display a bit.


Secondly, if I set my viewport to { 0, 0 }, part of the upper-leftmost map doesn't display.

If I change it to:

{ -(map.Layers[0].TileWidth / 2), -(map.Layers[0].TileHeight / 2) }

then it shows 1 pixel of unrendered area along the left edge of the screen, and several more along the top edge. My goal is to restrict the viewport to the boundaries of the map, so that every pixel of the map can be scrolled to and displayed on the screen without displaying any unrendered areas. For now, player sprites are simply centered on screen, as to avoid any future code changes.


Finally, I figured that I could limit viewport scrolling via something like:

viewport.X += (int)((pad.ThumbSticks.Left.X * gameTime.ElapsedGameTime.TotalMilliseconds) * SCALAR_MOVE_SPEED);
viewport.Y -= (int)((pad.ThumbSticks.Left.Y * gameTime.ElapsedGameTime.TotalMilliseconds) * SCALAR_MOVE_SPEED);
if (viewport.X + viewport.Width > map.DisplayWidth) viewport.X = map.DisplayWidth - viewport.Width;
if (viewport.Y + viewport.Height > map.DisplayHeight) viewport.Y = map.DisplayHeight - viewport.Height;

... but this proves false, limiting much more than expected.


How can I fix this?

Jan 2, 2012 at 12:32 PM

Are all the layers in the map greater than your viewport? The parallax calculations can exhibit some strange behaviour otherwise.

Could you post some screenshots and maybe a sample project that I could check out? Also, what platform are you trying this out on?

Jan 9, 2012 at 10:43 AM

on that note, what is the easiest way to remove parallax behaviour completely

Jan 9, 2012 at 9:06 PM

The easiest way to have all layers scroll together without parallax is to have them all of the same size.