Infinate Map Co-Ordinate question

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.

Is Player_x based on the screen location? or the map location?

Its the global position

If its the global position, then wouldn’t Player_x / r_width give you the tile location (of all tiles) then divide that by tiles_per_block_x, to get you block location. Right?
Then (Player_x - (r_width * tiles_per_block_x) ) / r_width would give the tile location within that block.
Right?

So Player_x/r_width/tiles_per_block_x?
That’s the same as
block_x = Player.x / (r_width * tiles_per_block_x)

for example:
20/5/2 = 4/2 = 2
20 / (5*2) = 20 / 10 = 2

I think that is incorrect.
That simplifies to Player_x/r_width - tiles_per_block_x
which would be Global tile location - tiles_per_block if I maths right

We came up with the same block.
So you’re using an r_width of 5 and 2 tiles per block. player_x at 20, we know he will end up in block 2 and tile 2, (both starting with 1 not 0).

Let me see if I can my formula right.
(Player_x - (r_width * tiles_per_block_x) ) / r_width
(20 - (5 * 2) ) /5 = (20 - 10) / 5 = 10 / 5 = 2

The other formula
Player.X - (block_xr_widthtiles_per_block_x)
20 - (2* 5 * 2) = 20 - (10 * 2 ) = 20 - 20 = 0

Does that get you what you want?

Doh! Gotcha. My maths are a little skewed with the little sleep I have been getting.

Just kidding. Still dont have you. Same example

r_width of 5 and 2 tiles per block

Now lets move player to 303 which should be block 30 tile 1 (starting at 1) {29 0 starting at 0}
(Player_x - (r_width * tiles_per_block_x) ) / r_width
(303 - (5 * 2) ) /5 = (303 - 10) / 5 = 293 / 5 = 58.6 (int) = 58

58 is a little out of bounds for the array

lets try - Tiles and blocks starting at 0
block_x = Player.x / (r_width * tiles_per_block_x) - 1
= 303/(5*2) = 303/(10) - 1 = 29.3 (int) = 29

tile_x = (Player.X/r_width) - (block_xtiles_per_block_x)
= (303/5)-(30
2) = (60.6) - (60) = 0.6 (int) = 0

Scratch that. I was thinking too much about going home. :persecutioncomplex: You need a zero base system, I keep forgetting that.
if player_x is 20, he would be the third block over (block 2) and the first tile (tile 0).
So your formulas are correct.

Sorry to confuse you.

I gave you a medal. :slight_smile:

Hah i actually completely forgot about modulus -_-

I use it all the time why not here??? Who knows.

Simpler form
tile_x = (Player.X/r_width)%tiles_per_block_x;

I am tooooo tired today.

Now you got the math worked out. How do the blocks work? I’m guessing you will have X number of blocks loaded at a given time, and as the player moves in one direction, you will load more blocks that way and remove blocks in the other direction.

Yes I have a 3x3 array of blocks. When the block_x or block_y changes it shifts the array accordingly and loads fresh blocks into the row or column. So say you are going left and you jump into a new block space.
(from 0)
(2,0) = (1,0)
(2,1) = (1,1)
(2,2) = (1,2)

(1,0) = (1,0)
(1,1) = (1,1)
(1,2) = (1,2)

(0,0) = Load(block_x-1, block_y-1)
(0,1) = Load(block_x-1, block_y+0)
(0,2) = Load(block_x-1, block_y+1)

Interesting. How are you going to balance it out, so you are not constantly loading new blocks?
Are you using a custom object for the block, with links to it’s adjacent blocks?

Block objects are serialized to the disk at the “xy.block” so it can be 00.block or 99-99.block etc. Loading is quick and processor non-intensive

I look forward to seeing the finished product.

+1 I do too. I calculated I can have a max of 7.37869763 × 10^21 tiles before the int loops back. Dunno If I will have enough space guys. If I switch to longs I could have 1.36112947 × 10^41 tiles but that’s still very tight. I will have a demo of the engine available soon.

Could you possible make the tiles bigger? or sub-divide the tiles to allow for a larger map?

Or have more tiles per block …

Just a fyi I uploaded an early prototype to my website last week. I will be doing some modifications and uploading an update by friday.

SatanicSpider.com

Projects -> Software -> Java Game Engine

Sorry bout the sloppy website. I’m building a cms on a different part of my server and slowly integrating it.

I tried to run it and got this:


Error no valid mode passed. Valid Modes:
	-1920x1200 (-1) 0hz
Exception in thread "main" java.lang.NullPointerException
	at jGame.CharacterLoadTest.gameInit(CharacterLoadTest.java:58)
	at jGame.GameCore.run(GameCore.java:55)
	at jGame.CharacterLoadTest.main(CharacterLoadTest.java:37)

If you’re planning on making source available, I’d gladly send a patch.

wow I did not plan for resolutions above 800x600 lol why is the only mode available so large >.> and at 0hZ straaaaaaaaaaaaaange.

Patch just for your strange gfx setup! use the switch -window to run in an 800x600 window