Skip to main content
gamedev.net
🔒 Locked

Stupid Question

Started by Muzlack Apr 15, 2002 at 10:51 PM 5 replies 600+ views
Original Post
Muzlack
Muzlack
Ok, I have a really stupid question. Is there a faster way to see what "tile" the character is on judged by the character''s x and y on the screen rather than a for loop? because It would take a lot longer to figure it out rather if the character where in the bottom right corner than in the top right.
--Muzlack
Puzzler183
Puzzler183
Why don''t you just keep track of where they are in the character struct??
Muzlack
Muzlack
Because, the game I am making has nothing to do with tiles(for the most part). I only use them for pathfinding. And, I just want to be able to figure out what tile the character is in when the player clicks somewhere the character is to move.
--Muzlack
Oluseyi
Oluseyi
You should have a function to convert character/map coordinates to world coordinates, and be able to determine which tile from that. If your world was a 200x200 grid, for example, and the character was at (12,12) on the screen, which is at offset (129,15) in the world, then the character is at (141,27) in the world, which should give you a good idea what tile it underfoot.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
Kylotan
Kylotan
Personally, I have no idea how you even got this far without knowing exactly the ratio of tile coordinates to player coordinates. Odd. Anyway, my systems tend to use tiles that are 32x32 so to find a tile''s top left corner in player coordinates, you just multiply by 32. And to translate a player''s top left point to a tile, you divide by 32. You''re just multiplying or dividing by the tile size. If you need the centre of the player, then just add on half the player''s width and/or height first. I don''t store screen position: screen position is calculated on the fly from world position minus the camera position.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
Fundy Glostna
Fundy Glostna
Simple:
For a 32x32 tilemap, to find the tile column where the player resides, divide the x-position by 32 and take the floor. To find the tile row where the player resides, divide the y-position by 32 and take the floor. Assuming that your tiles are stored in a multi-dimensional array, the tile of the player''s x-position and y-position can be looked up using the calculated row and column:


  
int row = (int)floor(player.ypos/32);
int col = (int)floor(player.xpos/32);

//TILE is whatever data type is stored in the tiles array

TILE thetile = tilearray[row][col];


If your player spans more than one tile, you might want to use two rows and two columns - one "col" for the left side of the player, another for the right side. One "row" for the bottom of the player, and another for the top.
No, Canadians DON'T all live in igloos. They live in one BIG igloo!

Topic Locked

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

Sign in to reply to this topic.