Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Infinate Map Co-Ordinate question  (Read 2170 times) 0 Members and 1 Guest are viewing this topic.
drakesword

Junior Devvie

Medals: 1

 « Posted 2011-09-23 20:20:45 »

So I have a system in place for a top down tile map.
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 21:40:57 »

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

Junior Devvie

Medals: 1

 « Reply #2 - Posted 2011-09-23 22:11:49 »

Its the global position
aazimon
 « Reply #3 - Posted 2011-09-23 22: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

Junior Devvie

Medals: 1

 « Reply #4 - Posted 2011-09-23 23: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 02: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

Junior Devvie

Medals: 1

 « Reply #6 - Posted 2011-09-24 02: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 03: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).

Sorry to confuse you.

I gave you a medal.
drakesword

Junior Devvie

Medals: 1

 « Reply #8 - Posted 2011-09-24 03: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 16: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.
drakesword

Junior Devvie

Medals: 1

 « Reply #10 - Posted 2011-09-24 17: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)

aazimon
 « Reply #11 - Posted 2011-09-25 23: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

Junior Devvie

Medals: 1

 « Reply #12 - Posted 2011-09-26 05: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 18:27:00 »

I look forward to seeing the finished product.
drakesword

Junior Devvie

Medals: 1

 « Reply #14 - Posted 2011-09-27 05: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 18:22:34 »

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

Junior Devvie

Medals: 1

 « Reply #16 - Posted 2011-10-03 11: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

JGO Kernel

Medals: 202

 « Reply #17 - Posted 2011-10-03 15: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) 0hzException 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

Junior Devvie

Medals: 1

 « Reply #18 - Posted 2011-10-03 21: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

Junior Devvie

Medals: 1

 « Reply #19 - Posted 2011-10-03 22:32:24 »

Patch just for your strange gfx setup! use the switch -window to run in an 800x600 window
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (16 views) 2015-05-23 20:26:16 MrMapcom (22 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (36 views) 2015-05-20 03:42:21 BurntPizza (71 views) 2015-05-10 15:53:18 FrozenShade (56 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (207 views) 2015-05-06 13:33:39 TheLopais (227 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x ra4king 12x theagentd 11x Drenius 11x DavidBVal 11x Husk 11x EgonOlsen 11x princec 10x KevinWorkman 9x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org