So for my tile based platformer, It has a grid of tiles Occupying 1x1 block for each one.

However, What if I want larger tiles? Maybe doors, tables, etc. They wouldnt fit in a 1x1 tile, so what I need is a way to let any method trying to access my tile array, know that a tile could be occupying a larger space.

I can already render larger objects correctly, But I simply am looking for a method to let evrything else know a larger tile is there, So I can perform collision on it, and so other tiles cant be placed on it.

  • \$\begingroup\$ I'm not sure you should be using "the grid"... unless all your tiles completely occupy the space? Otherwise, you might have "floating" collisions. \$\endgroup\$ – Vaughan Hilts Aug 9 '12 at 19:05
  • \$\begingroup\$ Well I am assuming games like terraria or minecraft, That seem to use a grid system, however have a few objects that are different \$\endgroup\$ – Cyral Aug 9 '12 at 19:06
  • \$\begingroup\$ In that case, just assign the tile as occupied and give each object a width and height (so your GridObject that is occupied there) When you query for collisions, just take into account the existence of that many tiles vertically and horizontally as well. \$\endgroup\$ – Vaughan Hilts Aug 9 '12 at 19:08
  • \$\begingroup\$ @VaughanHilts Ive already defined the width/height of each tile, I should have explained clearer, My problem is, How will it know that? I would have to check tiles in each direction, for however long my largest tile is just to find a larger tile \$\endgroup\$ – Cyral Aug 9 '12 at 19:16
  • 1
    \$\begingroup\$ Why not flag them all as 'occupied' then and then just have them all point to the same object? \$\endgroup\$ – Vaughan Hilts Aug 9 '12 at 19:20

So, if you have objects that are larger and you wish to fill a simple collision map (perhaps for sanity checking... you'll want to consider the use of bounding rectangles if that suits your game) let us take a look at how one might implement this using a simple 2D array.

Let us use the example for our collision map:

0, 0, 0, 0
0, 0, 0, 0 
0, 1, 1, 0  
0, 1, 1, 2

0 = null
1 = object with ID 1
2  = object with ID 2

If we use this simple model, upon placing the object we can fill all adjacent nodes in the array that are occupied with the object width and height. When checking for collisions, you can simply check for a 0.. anything else can be assumed to need further checking. If some objects can be passed through, either just write a 0 there to begin with... (you might be able to get away with this) or query for the object from your entity lookup table and check for more information.

  • \$\begingroup\$ I got it so it can set the reference and such (When I add a tile to my array, It will automaticly see how big it is, and add references for tiles near it). Is there a way I can get it so when I use the accessor, it will automaticly redirect to the referenced tile? \$\endgroup\$ – Cyral Aug 10 '12 at 19:39
  • \$\begingroup\$ Sure, store an array of GameObjects instead. A space that's open can just be null; the rest of objects can be just be pointers/reference to the actual tile. If it's not null, you know somethings there to collide with. \$\endgroup\$ – Vaughan Hilts Aug 10 '12 at 19:41
  • \$\begingroup\$ Yea I understand that, But In the accessor, I was wondering how to make it point to it, Or SEARCH and get the pointed tile everytime I need to access that tile \$\endgroup\$ – Cyral Aug 10 '12 at 20:12
  • \$\begingroup\$ I'm not sure I understand the terminology "accessor" here. Do you mean the array? Just store the reference to the object in the array... but I'm sure you knew that. Could you flesh your question out a bit more? \$\endgroup\$ – Vaughan Hilts Aug 10 '12 at 20:15
  • \$\begingroup\$ I mean using encapsulation (get{} set{}) to retrive/redirect to the correct tile \$\endgroup\$ – Cyral Aug 10 '12 at 21:22

I plan to solve that by aggregating larger objects out of simple tiles.

For example, if you want your door to be 3*5 tiles big, you could construct it like this:


1 is a tile that, upon collision, leads you to another map (specified in the tile-descriptor) and 0 is simply a pretty picture.

  • \$\begingroup\$ Im confused. Are you saying that I need to create a new tile array for each of those objects? \$\endgroup\$ – Cyral Aug 9 '12 at 19:08
  • \$\begingroup\$ No. Wherever you describe your tiles, simply create 1 and 0 like normal tiles. And then, in your map editor, place them near to eachother to form a door. \$\endgroup\$ – jcora Aug 9 '12 at 19:09
  • \$\begingroup\$ He's implying you should place each chunk of your object as a separate tile. Painful. \$\endgroup\$ – Vaughan Hilts Aug 9 '12 at 19:10
  • 1
    \$\begingroup\$ Because now you need to set each one separately, and don't forget if that object needs a special property.. such as a door that can be opened and closed. It now needs to be applied to every tile. \$\endgroup\$ – Vaughan Hilts Aug 9 '12 at 19:15
  • 2
    \$\begingroup\$ You don't need to necessarily have each chunk as a separate object. As far as the graphics is concerned, you can draw the door. But it still sets the appropriate bits in the collision map that indicate "non-placeable/non-walkable" \$\endgroup\$ – Jimmy Aug 9 '12 at 19:27

My first idea for a solution would be to have objects that can span several tiles and then have a reference to that object in the tile so you know it's a door or whatever in case you need to open it later. So if an object spans 3x3 tiles when you add a reference to it to all adjacent tiles as well. Then you know that all those tiles contain that specific door with that state (open/closed, hitpoints etc).

If you want to go all crazy maybe you could look at using quadtrees for very large worlds but that might be a later issue. Never done that myself, it just seems really really cool :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.