Skip to main content
GameDev.net gamedev.net
🔒 Locked

recalculating map coordinates??

Started by Llama Feb 18, 2003 at 12:42 PM 12 replies 600+ views
Original Post
Llama
Llama
Hello! My question is simple: Is there a mathematical way to redo a map-to-screen-calculation? i mean screen-xy (235,34) back to map-coordinates(eg 3,3) thx!!!
evillive2
evillive2
you probably have some kind of world coordinates for your object. Use that to get the map position:

map_x = obj_world_x/tile_width;
map_y = obj_world_y/tile_height;

assuming you have a camera object with a screen position of 0,0 and some kind of world position:

screen_x = obj_world_x - camera_world_x;
screen_y = obj_world_y - camera_world_y;

hope that makes sense...
Evillive2
Mav
Mav
i didn''t really get what evillive ment with that:

map_x = obj_world_x/tile_width;
map_y = obj_world_y/tile_height;

but let''s assume you have such a camera-object evillive talked of. this camera has a certain offset in x- and in y-direction. you get the screen-coordinates by doing this:

screenX = mapX - camOffsetX;
screenY = mapY - camOffsetY;

to get the mapcoordinates from the screencoordinates again simply do this:

mapX = screenX + camOffsetX;
mapY = screenY + camOffsetY;

that''s it.

evillive2
evillive2
quote:

i didn''t really get what evillive ment with that:

map_x = obj_world_x/tile_width;
map_y = obj_world_y/tile_height;



sorry, let me clarify what I meant even though we kind of said the same thing

map_x and map_y are the tilemap position map[map_x][map_y] or simply just the tile they are on.

obj_world_x and obj_world_y are the pixel they are on on the map.

camera_world_x and camera_world_y is the top left pixel the camera would be positioned at on the map.

screen_x and screen_y are the screen values for obj_world_x/y respectively.

hope that cleared it up a little.
Evillive2
Mav
Mav
oh, that was what you ment. when I talk of map-coordinates I mean that what you call worldcoordinates.
Llama
Llama
quote:
Original post by evillive2
you probably have some kind of world coordinates for your object. Use that to get the map position:

map_x = obj_world_x/tile_width;
map_y = obj_world_y/tile_height;



That would be a possible solution for a tile without rotation, but my tiles are 45° rotated.

to make it more clear: i''m using the following calculation to get screen_xy outta map_xy:

x = (MapSizeX*(TilesizeX/2))+(TilePosX*(TilesizeX/2))-(TilePosY*(TileSizeX/2))-(TileSizeX/2);

y = (TilePosX*(TilesizeX/2))*(TilePosY*(TilesizeY/2))-(TileSizeY/2);

hope this makes a sense for you, cause it might not be the best way to do this...
BodySplash
BodySplash
i hope you''re using rectangular tiles, because with isometrical tiles i think this equations won''t work..
isometric rules
Llama
Llama
no i'm using isometric tiles and the calculations work pretty good, but i need a calculation that does exactly the opposite of them. (screen coordinates -> map coordinates)

[edited by - Llama on February 22, 2003 10:05:14 AM]
cairnswm
cairnswm
I read a number of articles on this but couldn''t find one that worked well. I created my own using the following logic.

1. Each point on the screen can be in either of two rectangles.
2. For each rectangle calculate if it is in the corner section
3. The one that isn''t in a corner is the tile clicked on.

hope this helps.
darookie
darookie
Most primitive way to do this:


BYTE gMask[TILE_Y][TILE_X] = {
{1, 1, 1, 0, 0, 4, 4, 4 },
{1, 1, 0, 0, 0, 0, 4, 4 },
{1, 0, 0, 0, 0, 0, 0, 4 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{2, 0, 0, 0, 0, 0, 0, 3 },
{2, 2, 0, 0, 0, 0, 3, 3 },
{2, 2, 2, 0, 0, 0, 3, 3 },
{2, 2, 2, 2, 0, 3, 3, 3 },
};


Now you can figure the possible tile by dividing screen x and y
positions by TILE_X and TILE_Y and look up the x%TILE_X and y%TILE_Y in gMask.
If the value is zero, the tile is MAP[y/TILE_Y][x/TILE_X], else
you check the number in gMap and adjust it according to the value
(e.g. ''1'' means the tile north-west, ''3'' is south-east etc.)
Air
Air
Ok, first off you said your tiles are rotated at a 45° angle. Standard isometric view is a 30°/60° due to the fact that it gives a much better sense of depth perception and the illusion of perspective. This equation will work for any angle, however, so long as you know how to calculate the width and height of the isometric tile size.

Here is my equation, which I''ve been using for some time now with a lot of success to translate a mouse click into a map-based coordinate. scrny and scrnx are the coordinates of the mouse at the time the user clicked. ISO_TILE_WIDTH and ISO_TILE_HEIGHT are a bit tricky. These are the width and height of the tile after translation . ie, the width and height of a tile at it''s widest point after it has been rotated along the 30° angle-- for example, a 32x32 tile becomes a 56x28 isometric tile.

x = (scrny / ISO_TILE_WIDTH) + (scrnx / ISO_TILE_WIDTH) + CamOffsetX;
y = (scrny / ISO_TILE_HEIGHT) - (scrnx / ISO_TILE_HEIGHT) + CamOffsetY;

- Air
cairnswm
cairnswm
I''ve seen this or similar calculations before. What I dont understand is how this calculation takes the offset between tiles into account. Each second row of tiles is off set by TILE_WIDTH / 2. This is not in the equation therefore the equations only work for square tiles and not isometric (diamond shaped) tiles.
ShadowChaser
ShadowChaser
Heres some code that not only supports tile coordinates *half way between tiles* or at any arbritrary location, it also supports height (Z), making it "3D"! :-)

void IsoRegion::TileToDisplayCoords(long RegionOffsetX, long RegionOffsetY, const Point3D & Position, long &ScreenX, long &ScreenY)
{
float HalfTileSizeY = (float)g_GameSystem.HalfTileSizeY; // Declared as a float here to avoid type conversion later
float xSizeComp = (float)g_GameSystem.HalfTileSizeX+1; // Declared as a float here to avoid type conversion later
long xTileBM = (long)floor(((Position.GetX()-1) * xSizeComp) - (Position.GetY() * xSizeComp));
long yTileBM = (long)floor((Position.GetX() * HalfTileSizeY) + (Position.GetY() * HalfTileSizeY));

// Calculate display coordinates with offsets
ScreenX = xTileBM - RegionOffsetX;
// HACK: Fix Z
ScreenY = yTileBM - RegionOffsetY - (long)(Position.GetZ() * g_GameSystem.IsoTileLength);
}
cairnswm
cairnswm
I have ScreenX and ScreenY - how do I find out which tile they fall on?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.