Creating a world map with PHP

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
BobMann
Posts: 93
Joined: Tue Jun 12, 2012 2:16 am

Creating a world map with PHP

Post by BobMann »

Ok I am having some design issues. I am trying to make a 2d map from data out of a database.
There is an ID and an X and Y position for each tile in the database and a variable $_GET["x"] and $_GET["y"].

But I am having trouble writing an algorithm to get the data.

The map would look like:
(x,y)

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

and so forth.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a world map with PHP

Post by Jackolantern »

Since you mentioned $_GET, are you sending this map data through GET data? Because the typical way to write a simple algorithm to populate a 2D map is with a nested FOR statement:

Code: Select all

for ($x = 0; $x < $numberOfColumns; $x++) {
   for ($y = 0; $y < $numberOfRows; $y++) {
      echo "<img src='maptile-$x-$y.jpg' />";
   }
} 
But probably the complication is coming in from how you are actually accessing the map tiles, how they are stored on the database, or both. Can you provide more information on how you have everything set up and how you want your map generated?
The indelible lord of tl;dr
User avatar
BobMann
Posts: 93
Joined: Tue Jun 12, 2012 2:16 am

Re: Creating a world map with PHP

Post by BobMann »

Its all in a database.

4 columns.
ID, Tile type, X position, and Y position.

The complication I am having is trying to write a system to get the tiles it needs. So if you are only looking at a 9x9 area starting at (1,3) the server wont be trying to get data from the coordinates (45,63) or something like that.

it would only get the data of the area based on the area you are looking at.

In short I can't think of any algorithm to do this(Never was good at math).
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Creating a world map with PHP

Post by Callan S. »

4 columns.
ID, Tile type, X position, and Y position.
Do you have to do it that way? I had it set up so the database looked like the map - ie, id 1 was row one, with colum 1 as it's first data, colum 2 as it's second data, etc. Then id 2 was the next row, etc.
User avatar
BobMann
Posts: 93
Joined: Tue Jun 12, 2012 2:16 am

Re: Creating a world map with PHP

Post by BobMann »

Callan S. wrote: Do you have to do it that way? I had it set up so the database looked like the map - ie, id 1 was row one, with colum 1 as it's first data, colum 2 as it's second data, etc. Then id 2 was the next row, etc.
If I get what your saying then no, I plan to also have negative positions like (-3,-6).
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a world map with PHP

Post by Jackolantern »

BobMann wrote: So if you are only looking at a 9x9 area starting at (1,3) the server wont be trying to get data from the coordinates (45,63) or something like that.
This part confused me a bit. The example coordinates you gave were rather large, so are those in reference to the actual x and y coordinates of where they go on the screen (like the css values you would give the tiles to place them correctly on the map)? If so, the actual spatial coordinates of where you place them on the map should not even come into play anywhere near the database. That can easily be done when assembling the map on the page.

To clarify, when you say 9x9, do you mean the whole world? Or do you mean each single map on-screen is made of 9x9 tiles? Because if these are individual maps that are made-up of 9x9 tiles, and your game is made up of many of those maps, how are you going to know which map is which (the ID is unique for each tile and I am assuming the tile type says how to display the tile, which only leaves the x and y, which are needed to assemble the single map)? You would have to add 2 more columns, a GlobalX and a GlobalY column, in that case (this would essentially turn the X and Y coordinates you already have into Local coordinates). Then your SQL query would pull all tiles with, for example, "WHERE GlobalX = 2 AND GlobalY = 4" for the world map at (2, 4). Then you would end up with an array of all of the 9x9 map tiles that make up the world map at coordinates (2,4), and you could then use the nested FOR loops mentioned above to actually put them into place on the screen.

To actually get each tile placed at the correct place, you would take the Local coordinates of each map tile, run them through a nested FOR loop, and inside the nested FOR loop, you would multiply their Local X and Y values by the dimensions of the tiles. For example, say your nested FOR loop is running, and the next tile up for that (2,4) map has Local coordinates of [5, 3]. That is, it is the 5th column 3rd row tile of this specific map. And say each tile is 64x64 pixels. That means the tile's Local X and Y values would be multiplied by 64, giving you "Left: 320px, Top: 192px" (assuming the map starts at 0,0, which is probably not the case meaning you would have to hardcode in offsets to get the map in the right place).

And then of course it would not be that hard to deal with players moving from map to map, since if they go North, you subtract 1 from the GlobalY, if they go East, you add one to GlobalX, etc., to get the correct world map.

That is a pretty simple map setup there, but yet is fairly flexible. It also allows you to get all the tiles for each map in only 1 query. Hope it helps! :)
The indelible lord of tl;dr
User avatar
BobMann
Posts: 93
Joined: Tue Jun 12, 2012 2:16 am

Re: Creating a world map with PHP

Post by BobMann »

Jackolantern wrote: To clarify, when you say 9x9, do you mean the whole world? Or do you mean each single map on-screen is made of 9x9 tiles?
Each coordinate is a tile in the map. 9x9 may just be the current area you are looking at, like if you zoomed out you would be looking at a 12x12 and so forth.

To clear a few things up, I am making a strategy game, not an mmorpg. The map is like the world map in 8realms.

http://static.3ice.hu/images/8realms-Ma ... -proof.png
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Creating a world map with PHP

Post by vitinho444 »

Hey BobMann
You might want to check my OldTimes Engine (PHP Browser Based Game engine) it has the map system that you want there ;)

You can check it here: http://indie-resource.com/forums/viewtopic.php?f=47&t=4775
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Creating a world map with PHP

Post by vitinho444 »

Hey BobMann
You might want to check my OldTimes Engine (PHP Browser Based Game engine) it has the map system that you want there ;)

You can check it here: http://indie-resource.com/forums/viewtopic.php?f=47&t=4775
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
BobMann
Posts: 93
Joined: Tue Jun 12, 2012 2:16 am

Re: Creating a world map with PHP

Post by BobMann »

I am not really wanting to tear apart something to try and integrate it into my code (also the code things on your post to download don't work for me).
Post Reply

Return to “Advanced Help and Support”