Gamelogic
  • Home
  • Blog
  • Tools
    • Grids 2 (for Unity)
      • Features
      • Documentation
    • Grids 1 (Legacy, for Unity)
      • 30 games in 30 days
      • Features
      • Tutorials, Documentation, and FAQ
      • Games made with Grids for Unity
      • Examples
      • What is new?
      • Grid Prototypes and Previews
      • Buy
    • Extensions
    • Colors
      • Features
      • Documentation and tutorials
      • Buy
    • Words
      • Features
      • Documentation
      • Buy
    • Abstract Strategy
      • Features
      • Documentation and tutorials
      • Examples
      • Buy
    • Match
      • Features
    • Grids (for GameMaker)
      • 30 games in 30 days
      • Features
      • Documentation
      • Buy
  • Buy
  • Articles
  • Support
    • Knowledge Base
    • Email Support
  • About
    • Meet our team

Blog

Layered Grids

We have used grids in 3D space in a variety of ways before. Mostly, these come down to mapping a grid in 3D space – often on the surface of a 3D shape – and using 3D objects instead of sprites. This example is a true volumetric grid, representing layers of grids stacked on top of each other.

There are of course many ways to implement this type of grid. The simplest way would be to have an array of grids, and an array of maps the map grids to different heights. Accessing the grid would require the layer index, as well as the 2D grid point. Of course, you can wrap this functionality in proper interfaces, and take advantage of all the Grid algorithms, and this is exactly what we have done here.

There are three components: a LayeredPoint, LayeredGrid, and SimpleLayeredMap. LayeredPoint is simply a struct that holds some other grid point, and a layer index. LayeredGrid is an array of grids, as described above. It provides factor methods that allows you to construct a layered grid of any type of other grid (hex, cairo, rect!) SimpleLayeredMap does the usual conversion between grid points and world points.

The code for setting up a grid and handling cell clicks is similar to the usual logic.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class LayerTest : GLMonoBehaviour
{
   public GameObject cellPrefab;
 
   private LayeredGrid<GameObject, PointyHexPoint> grid;
   private SimpleLayeredMap<PointyHexPoint> map;
 
   public void Start()
   {
      map = new SimpleLayeredMap<PointyHexPoint>(
     new PointyHexMap(new Vector2(69, 80)*5f), 200, 0);
 
      var shapes = new []
      {
         PointyHexGrid<GameObject>.BeginShape().Hexagon(6),
         PointyHexGrid<GameObject>.BeginShape().Hexagon(5),
         PointyHexGrid<GameObject>.BeginShape().Hexagon(4),
         PointyHexGrid<GameObject>.BeginShape().Hexagon(3),
         PointyHexGrid<GameObject>.BeginShape().Hexagon(2),
         PointyHexGrid<GameObject>.BeginShape().Hexagon(1)
      };
 
      grid = LayeredGrid<GameObject, PointyHexPoint>.Make<
         PointyHexShapeInfo<GameObject>,
         PointyHexGrid<GameObject>,
         PointyHexPoint, PointyHexPoint, PointyHexOp<GameObject>>(
    shapes);
 
      foreach (LayeredPoint<PointyHexPoint> point in grid)
      {
         var cell = Instantiate(cellPrefab);
 
         cell.transform.parent = transform;
         cell.transform.localPosition = map[point];
        
         var color = ExampleUtils.colors[point.point.GetColor2_4() + 4];
         cell.renderer.material.color = color;
 
         grid[point] = cell;
      }
   }
 
   public void Update()
   {
      if (Input.GetMouseButtonDown(0))
      {
         var mousePosition = Input.mousePosition;
         var ray = Camera.main.ScreenPointToRay(mousePosition);
 
         RaycastHit hitInfo;
 
         if (Physics.Raycast(ray, out hitInfo))
         {
            var worldPoint = hitInfo.point;
            var gridPoint = map[worldPoint];
 
            if (grid.Contains(gridPoint))
            {
               grid[gridPoint].renderer.material.color =
      ExampleUtils.colors[7];
            }
         }
      }
   }
}

The one thing that is a bit scary is all the type arguments you have to pass to the Make function (figuring out what they should be was quite a challenge!). Fortunately, the need for such complicated type specifications is a rare one, and I think we can make it simpler by introducing another interface (whether we should is a different story; an extra interface just moves the complexity around). Another flaw that is exposed by this example is how the fluent shape building interface can become less intuitive. It’s nice that you can use the shape building tech to build each layer, but the fact that you have BeginShape without corresponding EndShape calls is a bit weird. (The EndShape method is in fact called internally when the grid is actually constructed.) One solution would be to add methods for layered shapes, so that you can write something like the following:

C#
1
2
3
4
5
6
7
8
LayeredGrid<SomeCellType, PointyHexPoint>
   .BeginShape()
      .BeginLayer()
         .Hexagon(1)
      .EndLayer()
      //...
      //etc.
   .EndShape();

Implementing that would be quite complex, and I am not sure it would be worth the effort. But if you use this kind of grid, let us know!

Here is a package containing all the code:

LayeredGrid.unitypackage

Import it into a new project, and import Grids.To use test the example, make a new scene, and add the LayerTest script to onto an empty GameObject. Drag any 3D object into the “cellPrefab” slot. For it to work, the object must have a Renderer attached. The image above was made with spheres rendered with a specular material.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Related

  1. A new look at layered grids: setting up neighbors  The layered grid is a very generic type of grid....
  2. Grids 1.9  Grids 1.9 is now available in the asset store 🙂 For...
  3. Support for Bitmask Shapes and Polyominoes in Grids 2.3  Polyominoes are used as pieces in many puzzle games, and...
  4. One Dimensional Grids  Today I implemented a custom grid that is useful for...
  • Permalink
  • Posted: 7 March 2014
  • Tags: 3D grid, layered grid
  • Author: Herman Tulleken
Recent Posts
  • Tool Documentation Overhaul
  • What is new in Grids Pro 2.3.5
  • What is new in Grids Pro 2.3.2
  • What is new in Abstract Strategy 2.1.1
  • What is new in Words 2.0.2
Categories
  • 30 Games in 30 Days
  • AbstractStrategy
  • Aside
  • Colors
  • Example
  • Extensions
  • General
  • Grids
  • Grids 2
  • Previews
  • Programming
  • Social Media
  • Uncategorized
  • Words
Follow us on Twitter
My Tweets
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
  • Home
  • Features
  • Blog
  • About
  • 14 Eglin Road, Sunninghill, Johannesburg, South Africa
  • support@gamelogic.co.za
Stay Connected
  • Facebook
  • Google
  • Linkedin
  • Rss
  • Twitter
  • Gamelogic © 2013