Game Development Community

Creating a Grid Based Game Board/Terrain...

by Quinton Delpeche · in Torque 3D Professional · 07/30/2012 (10:50 pm) · 7 replies

Hi Fellow Torquers,

So I have seen some awesome work done by the likes of Steve Acaster with his current project using a Hex Based Grid System (www.garagegames.com/community/blogs/view/21789) and some of the older work by Guimo on his Warscale project (www.garagegames.com/community/blogs/view/21333) and I was wondering how did they do it?

I need to implement a grid based system that supports both Hexagons and Squares, it needs to be scalable, and fairly customisable (South African spelling) and I have no idea where to start. I was thinking of posting this on HirePin as I am willing to pay for the system ... however first prize would be for me to try and create it myself.

I don't have a huge budget for the payment of this, so before I go the HirePin route, I was hoping I could get some advice or tips from here first.

Thanks in advance.

About the author

Gobbo Games is an Independent Games Development company operating from Durban in South Africa. We believe in creating high-quality cost-effective games that remain true to the belief of Independent Game Developers around the world.


#1
07/31/2012 (7:58 am)
Quote:
customisable (South African spelling)
You mean, of course, the Queen's English.

Personally I used standard TsStatics for each hex (because I need to be able to select and store variables in them individually), and then came up with a formula for laying them out in rows based on their dimensions/size so that they didn't overlap.
#2
07/31/2012 (10:09 am)
@steve: Well we are a former colony ... but I would hate for the anyone to hear me. 8-p

Thanks for the tip dude, I will start playing around and see what I come up with. Much appreciated.
#3
07/31/2012 (11:36 am)
I use a square grid system for my game and in the end I just use a global, 2 dimensional script array to store all my info. Ultimately I don't do anything special outside of just handling certain functions' positions in regards to the grid.

So say, a new object is to be placed. My grid system uses tiles that are 5x5 in size. So, I'd look at the exact real position that the object is looking to be placed, round it to the nearest 5, divide it out, and that gives me my single tile. If applicable, I then update the global array for that tile for whatever I need it to store.

It works fine for squares, but for a hex grid it may be a bit trickier. I don't use a static method like Steve cause with as many squares as I have, it just becomes too much stuff in the world.

So a quick example would be $tileInfo[53][73] = "2 1 1". 2 is the tile type, in this case a claimed tile. 1 is the Owner, in this case you. And the other 1 is if this is a "torch tile" or not. The game is underground with tunneling and what not, and so that variable is to decide if a torch should be placed on the wall for that tile or not. And now any function anywhere in script can grab this info with a simple getWord. And you can just add in more info as you need it.
#4
07/31/2012 (12:07 pm)
@jacob: Thanks for the direction. Much appreciated. I will try your idea as well and see what I come up with. Thanks.
#5
07/31/2012 (3:17 pm)
Generating anything in squares and rectangles and then obtaining the information later on is done via nested for loops.

You might consider writing a class (scriptObject base) that generates random information and then follow Steve's suggestion (use a tsStatic for each type of terrain). This class would then be responsible for getting the needed information and storing the terrain in grid references:

//Free-wrote in browser, errors may be present
$control = new ScriptObject() {
   class = "myGrid";
};

function myGrid::generate(%this, %x, %y) {
   for(%ix = 0; %ix < %x; %ix++) {
      for(%iy = 0; %iy < %y; %iy++) {
         %newProperty = getRandom(0, #); //insert # for properties, IE: 0 - land, 1 - sea, ect...
         %this.square[%ix, %iy] = %newProperty;   
      }
   }
   %this.xSquares = %x;
   %this.ySquares = %y;
   %this.buildMap();
}

function myGrid::buildMap(%this) {
   for(%ix = 0; %ix < %this.xSquares; %ix++) {
      for(%iy = 0; %iy < %this.ySquares; %iy++) {
         //spawn objects here!
      }
   }
}

$control.generate(25, 25); //make a 25x25 map

Rough idea, but it should visualize the point. Good luck!
#6
07/31/2012 (9:08 pm)
@all: You guys all rock. Thanks for the pointers ... I am a Java Developer by professon and know how I would do it in Java ... but my C++ is a little weak and needs some serious focus.

Anyway, thanks for pointing me in the right direction ... Much appreciated.

Hi Ho ... Hi Ho ... It is off to coding I go. 8-)
#7
08/04/2012 (3:52 am)
You should look in Google for 2D grid system. I used to find good stuff on this topic in an university blog.
Fyi i built a 2D grid system in c++ that will create a plane based on parameters with a material that will make thé square. The material is used for each square. You can look in the ground object to start. It will show you how to create the vertex you need to support your material.
Then just add some functions to get à square id from coordinates, imPlement a 2D raycast, some path finding and your game dream come true :)
Hex board are more tricky to implement, but you can find ressources also on university blogs. I Will try to post you the URL when I m back.