   Convert Screen Tiles to Map Tiles
  I'm using a diamond map. I have a function that converts Map tiles to Screen pixels.I have a function that converts Screen pixels to Map tiles.How do I convert Screen tiles to Map tiles?[edited by - spyder on February 10, 2004 8:57:45 AM]
  Since no one else has asked, I will:What the heck do you mean? ;  Sorry for being unclear. Let see. Lets say the top left corner on the Screen is tile (-10,-10). I want to find out what tile that is on the Map. So I want a function where I send in screenTileX and screenTileY and it will give me mapTileX and mapTileY.Make any sense? ;  He wants to know which tile is on for example pixel(0,0).   --You''re Welcome,Rick Wong- sitting in his chair doing the most time-consuming thing.. ;  No not really... I have a mouseToMap function already. ;  I think I''m beginning to understand what the question is, but you said that you have a function that "converts screen pixels to map tiles", which is where my confusion comes from... apparently we aren''t speaking the same language.So, to promote better communication, here are the standard terms used with the various coordinate spaces that deal with orthotile calculations.MapSpace: ranges from (0,0) to (MapWidth-1,MapHeight-1), each coordinate represents a single tile location.WorldSpace: if the entire map were rendered onto an infinite screen, it would take a particular size of rectangle to contain all of the pixels, which is WorldSpace. The size and ranges depend on the graphics used as well as the coordinate system for MapSpace.ViewSpace: we don''t happen to have an infinite screen handy, and so have to limit what we can look at. ViewSpace ranges from (0,0) to (ViewWidth-1,ViewHeight-1).  Measured in pixels.ScreenSpace: The rectangular section of the screen allocated for use in rendering.  Often the same as ViewSpace, but may not be. Measured in pixels.There are, in addition, two spaces that I use, depending on the nature of the view.  These are: AnchorSpace, which tie ViewSpace(0,0) with a WorldSpace coordinate; PlotSpace, which is slightly larger than ViewSpace and used so that nothing is "missed" when the contents of tiles stick out of the top of the tile itself).When rendering a scene, you go from MapSpace->WorldSpace->ViewSpace->ScreenSpace, and when determining which tile a mouse is over, you go the reverse direction.Typically, MapSpace->WorldSpace is handled by one component(TilePlotter), WorldSpace->ViewSpace->ScreenSpace and ScreenSpace->ViewSpace->WorldSpace is handled by another component(Scroller), and the WorldSpace->MapSpace is handled by yet another (TileMapper/MouseMap).So, which of the four transformations are you having trouble with?;  Here's my problem.My map is rectangular in MapSpace. When drawn it is diamond in WorldSpace. I want to know the underlying map locations that constitute a rectangle in my diamond/world space. This would allow me to redraw a specific rectangle in my diamond/world space.   [edited by - spyder on February 11, 2004 3:26:19 AM];  aha!  think i got it now, but just to double check:either, based on a map coordinate, you want to know what rectangular part of the screen you need to update?or, you want to know which tiles you need to draw to completely cover up that rectangle? ;quote: you want to know which tiles you need to draw to completely cover up that rectangle? Thats right! So I was thinking that if my rectangle is (5, 5, 10, 10) I would have a function telling me which map coordinates are needed to update that rectangle.What I think is tricky about this is that some of you think my rectangle is composed of pixels. It's not! Every location should reference a map tile.When I say redraw a rectangle (10 wide, 10 high) I want 100 tiles on the map.   [edited by - spyder on February 11, 2004 10:57:39 AM];  First, you need to keep image extent rectangles.  An image extent is a rectangle that, were you to plot the image at (0,0) would describe a rectangle that encompassed the entire image.Next, you need to merge all image extent rectangles together into a tile extent rectangle.  If using WIN32 and the RECT structure, this is easily done with UnionRect.The two things above you will only need to do one time, unless you are dynamically loading and unloading your images.When you have a map tile that you need to update, you do the following:First, plot the tile into screenspace.  Take a copy of the tile extent rectangle, and offset it by the plotted tile position.  Set the clipping rect to this rectangle (called the screen update rectangle).Next, transform the screen update rectangle into world space (making it the world update rectangle).  Subtract the tile extent rects right and bottom from the world update rect's left and top, and subtract the tile extent rect's left and top from the world update rect's right and bottom.[edit:corrected world update rect calculation.]Map the upper left corner of the worldspace update rect into map coordinates.  hold on to this value, as you will need it later.Now you loop through the all of the tiles in the world update rect.The first check is to plot the tile into world space, and check to see that it is in the world update rect.  if it is not, then check it for being south of the world update rect.  if to the south, you are done, so quit looping.  if you are to the left, simply move one tile to the east until you are inside the update rect.  if you are to the right, you are done with a row, and need to go back to the stored value, and move it one tile to the southwest.if inside the update rectangle, render the tile, then move one tile to the east.