drakesword
|
 |
«
Posted
2011-09-23 22:20:45 » |
|
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_x*r_width*tiles_per_block_x) local_y = Player.Y - (block_y*r_height*tiles_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.
|
|
|
|
|
aazimon
|
 |
«
Reply #1 - Posted
2011-09-23 23:40:57 » |
|
Is Player_x based on the screen location? or the map location?
|
|
|
|
|
drakesword
|
 |
«
Reply #2 - Posted
2011-09-24 00:11:49 » |
|
Its the global position
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
aazimon
|
 |
«
Reply #3 - Posted
2011-09-24 00:53:09 » |
|
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?
|
|
|
|
|
drakesword
|
 |
«
Reply #4 - Posted
2011-09-24 01:20:35 » |
|
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?
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 Then (Player_x - (r_width * tiles_per_block_x) ) / r_width would give the tile location within that block. Right?
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
|
|
|
|
|
aazimon
|
 |
«
Reply #5 - Posted
2011-09-24 04:09:23 » |
|
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_x*r_width*tiles_per_block_x) 20 - (2* 5 * 2) = 20 - (10 * 2 ) = 20 - 20 = 0
Does that get you what you want?
|
|
|
|
|
drakesword
|
 |
«
Reply #6 - Posted
2011-09-24 04:14:43 » |
|
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_x*tiles_per_block_x) = (303/5)-(30*2) = (60.6) - (60) = 0.6 (int) = 0
|
|
|
|
|
aazimon
|
 |
«
Reply #7 - Posted
2011-09-24 05:01:44 » |
|
Scratch that. I was thinking too much about going home.  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. 
|
|
|
|
|
drakesword
|
 |
«
Reply #8 - Posted
2011-09-24 05:22:38 » |
|
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.
|
|
|
|
|
aazimon
|
 |
«
Reply #9 - Posted
2011-09-24 18:03:54 » |
|
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.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
drakesword
|
 |
«
Reply #10 - Posted
2011-09-24 19:55:00 » |
|
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)
|
|
|
|
|
aazimon
|
 |
«
Reply #11 - Posted
2011-09-26 01:12:49 » |
|
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?
|
|
|
|
|
drakesword
|
 |
«
Reply #12 - Posted
2011-09-26 07:23:28 » |
|
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
|
|
|
|
|
aazimon
|
 |
«
Reply #13 - Posted
2011-09-26 20:27:00 » |
|
I look forward to seeing the finished product.
|
|
|
|
|
drakesword
|
 |
«
Reply #14 - Posted
2011-09-27 07:03:19 » |
|
+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.
|
|
|
|
|
aazimon
|
 |
«
Reply #15 - Posted
2011-09-28 20:22:34 » |
|
Could you possible make the tiles bigger? or sub-divide the tiles to allow for a larger map?
|
|
|
|
|
drakesword
|
 |
«
Reply #16 - Posted
2011-10-03 13:50:04 » |
|
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.
|
|
|
|
|
sproingie
|
 |
«
Reply #17 - Posted
2011-10-03 17:28:39 » |
|
I tried to run it and got this: 1 2 3 4 5 6
| 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.
|
|
|
|
|
drakesword
|
 |
«
Reply #18 - Posted
2011-10-03 23:33:20 » |
|
wow I did not plan for resolutions above 800x600 lol why is the only mode available so large >.> and at 0hZ straaaaaaaaaaaaaange.
|
|
|
|
|
drakesword
|
 |
«
Reply #19 - Posted
2011-10-04 00:32:24 » |
|
Patch just for your strange gfx setup! use the switch -window to run in an 800x600 window
|
|
|
|
|
|