Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Infinate Map Co-Ordinate question  (Read 1687 times)
0 Members and 1 Guest are viewing this topic.
Offline drakesword

Junior Member


Medals: 1



« 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.
Offline aazimon
« Reply #1 - Posted 2011-09-23 23:40:57 »

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

Junior Member


Medals: 1



« Reply #2 - Posted 2011-09-24 00:11:49 »

Its the global position
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline drakesword

Junior Member


Medals: 1



« 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
Offline 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?
Offline drakesword

Junior Member


Medals: 1



« 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



Offline aazimon
« Reply #7 - Posted 2011-09-24 05:01:44 »

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. Smiley
Offline drakesword

Junior Member


Medals: 1



« 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.
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline drakesword

Junior Member


Medals: 1



« 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)
Offline 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?
Offline drakesword

Junior Member


Medals: 1



« 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
Offline aazimon
« Reply #13 - Posted 2011-09-26 20:27:00 »

I look forward to seeing the finished product.
Offline drakesword

Junior Member


Medals: 1



« 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.
Offline 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?
Offline drakesword

Junior Member


Medals: 1



« 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.
Offline 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.
Offline drakesword

Junior Member


Medals: 1



« 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.
Offline drakesword

Junior Member


Medals: 1



« 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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!