Allegro.cc Forums » Programming Questions » Point in tile isometric diamond map

Credits go to HoHo and Sirocco for helping out!
 Point in tile isometric diamond map
 Rick Member #3,572 June 2003 Is it possible to find what tile the mouse is in on an isometric diamond shaped map with a calculation instead of having to loop through each tile and figuring it out? Much like a square tile map you can do:```row = (int)mouse_x/TILE_H; col = (int)mouse_y/TILE_W; ``` [EDIT]I guess if things are easier in a staggered isometric map I'd be open to that also. I have just never worked on those before. ========================================================Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.
 HoHo Member #4,534 April 2004 use a mouse lookup maphttp://www.gamedev.net/reference/articles/article2026.aspI've done it in an Xcom-like engine and it worked rather fine. I'd give you a link to the sources I once attached to a post but it seems like the links are broken and I can't find that post. I can upload it later today if you want to.Actually I meant this article but the first one can be useful too. This one also has formula for finding the tile coordinates from screen coordinates __________In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de SnepscheutMMORPG's...Many Men Online Role Playing Girls - Radagar"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski
 Sirocco Member #88 April 2000 You've got a couple of options depending on the complexity of the map. You can create a lookup table using various methods. Some are more memory efficient than others. There are some techniques you could investigate using rays to trace the route to the 'monitor', which I've used with varying degrees of success.At the end of the day, a lookup table would probably be your best bet. -->Graphic file formats used to fascinate me, but now I find them rather satanic.
 Rick Member #3,572 June 2003 I've looked at the second article (Out With the Old, In With the New) which shows how to find the tile the mouse is in via a simple calculation, but I'm not sure about the drawing of the diamond tilemap that matches that code. You seem to have to draw the first tile 1/2 way into the top left screen. ========================================================Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.
 HoHo Member #4,534 April 2004 Quote: I'm not sure about the drawing of the diamond tilemap that matches that code. You seem to have to draw the first tile 1/2 way into the top left screen. I don't understand. The ideas from the second article should work with pretty much anything. I copied them almost directly to my engine that supported diamond shape maps, multiple levels and per-pixel scrolling. Result was something like this: {"name":"dagame.PNG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62883d2af523cf4b80b7faefda8e980b.png","w":664,"h":515,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/62883d2af523cf4b80b7faefda8e980b"} __________In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de SnepscheutMMORPG's...Many Men Online Role Playing Girls - Radagar"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski
Rick
Member #3,572
June 2003

Here is the complete code and it doesn't seem to work. I just copied the code on that page and plugged it in. When I clicked the top tile I get mapx = 34, and mapy = 2, which isn't right. I'm expecting 0,0. I must be doing something wrong.

[EDIT]
Revised code as he had some bugs in it. Now mapx is 0 which is right but mapy is -1 which isn't right.

[EDIT2]
I think I got it. I had to put my xadjust to 32 (1/2 tile width) so it would make the first tile being drawn half off the screen.

Bascially changed:
pointer_get_map_coords(mouse_x, mouse_y, 0, 0, 0, 0, &mapx, &mapy);
to
pointer_get_map_coords(mouse_x, mouse_y, 32, 0, 0, 0, &mapx, &mapy);

and it works now. Thanks all.

 1 #include "allegro.h" 2 3 #define TILE_W 64 4 #define TILE_H 32 5 #define MAP_ROW 10 6 #define MAP_COL 10 7 8 BITMAP *buffer, *tile; 9 10 11 void drawMap() 12 { 13 int row, col, x, y; 14 15 for(row=0;row

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

 Wilson Saunders Member #5,872 May 2005 When I made my Isometric tileing engine I had two grids of tiles the first with no offset and the other offset by 1/2 the width and 1/2 the height. The tiles were bitmaps with magenta 0xff00ff as the non diamond section. So individually the grids would draw a checkered pattern but toggether they draw the full ground.Any to get to your question. You find the which node in each grid the mouse is closest to like: ```int grid1indexX = mouse_x/TILE_WIDTH; int grid1indexY = mouse_y/TILE_HEIGHT; int grid2indexX = (mouse_x + TILE_WIDTH/2)TILE_WIDTH; int grid2indexY = (mouse_y + TILE_HEIGHT/2)TILE_HEIGHT; ``` Then you decide which grid tile you are going to use like this ```if( grid1[grid1indexX][grid1indexY]->DistanceTo(mouse_x, mouse_y) < grid2[grid2indexX][grid2indexY]->DistanceTo(mouse_x, mouse_y) ) { // use grid 1 tile } else { // use grid 2 tile } ``` Hope this helps ________________________________________________Play my games at http://monkeydev.com
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads