So I have a system in place for a top down tile map.
The map loads 9 blocks.
Each block contains 20x20 tiles (can be changed later)
So now I think I have the maths to change a players global position to 3 different co-ordinates
If its necessary definitions:
r_width = 50 – the width the tile should be rendered
r_height = 50 – the height the tile should be rendered
tiles_per_block_x = 20 – the number of tiles in a block in the X direction
tiles_per_block_y = 20 – the number of tiles in a block in the Y direction
For getting the block co-ordinates the player is in (integer)
block_x = Player.x / (r_width * tiles_per_block_x)
block_y = Player.y / (r_height * tiles_per_block_y)
This is used chiefly for loading blocks into memory and keeping track of where blocks are saved
For local block co-ordinates I have (double values)
local_x = Player.X - (block_xr_widthtiles_per_block_x)
local_y = Player.Y - (block_yr_heighttiles_per_block_y)
This is used mainly for shifting the image on the screen to the proper position
Finally the tile numbers (integer value)
tile_x = local_x / r_width
tile_y = local_y / r_height
This is used to determine which tiles to render as it will always be (x-n,y-n) to (x+n,y+n) This way I can avoid iterating over each of the 9 blocks of 400 tiles and checking to see which ones are in view.
Does my maths look right or am I dooing a bunch of extra work.