Game Development Community

TileMaps and TileCollisions

by Harold "LabRat" Brown · in Torque Game Builder · 02/27/2005 (9:07 pm) · 8 replies

If you scale a TileLayer up in size the Tile Collisions do NOT scale.

You can use the following function to "rewrite" your map at loadtime to reset the collisions to be exactly the same size as your tile (NOTE: This is only for square tiles right now).

function fixCollision(%layer)
{
	%tileCount = %layer.getTileCount();
	for (%x = 0; %x < getWord(%tileCount,0); %x++)
	{
		for (%y = 0; %y < getWord(%tileCount,1); %y++)
		{
			%collision = %layer.getTileCollision(%x SPC %y);
			%tile = %layer.getTileType(%x SPC %y);
			%layer.clearTile(%x SPC %y);
			%layer.setStaticTile(%x SPC %y, getWord(%tile,1));
			%layer.setTileCollisionActive(%x SPC %y, getWord(%collision,0));
		}
	}
}

Usage:

%layer.setPosition( "0 4" );
	%layer.setSize( "100 58.5" );
	%layer.setTileSize( "2.5 2.5" );
                fixCollision(%layer);


NOTE: This is for STATIC TILES at the moment

#1
02/28/2005 (1:17 am)
@Labrat: Thanks for the heads-up! This is a bug and is easy to fix internally. It's now on the list to fix. Big Thanks.

- Melv.
#2
03/01/2005 (12:05 am)
AHA,,, I had that problem also, and thought I was going nuts. Thanks Labrat!

BTW, is there a way to turn on some view for the collision polygon?
#3
03/01/2005 (12:23 am)
Yes, indeed there is.

If you haven't already seen it, there are two functions called "setDebugOn()" and "setDebugOff()".

To these functions you pass a bit-mask which determines which options you want to see. The cool thing about these functions are that you can turn them on for all of the scene or per-object.

To do it for the whole scene, call the "setDebugXX()" from your fxSceneGraph2D object. For per object, do it off the object in question. There are currently six options (all listed in the reference docs) and are the same independant of whether you apply them to the scene or object although the first option is not applicable when running per-object.

To show the collision polygons, use "BIT(5)" as the parameter. Note that you can turn them all on if you wish with "0xFFFFFFFF".

Here's a couple of examples:-

// Show Collision Polys for All scene objects.
new fxSceneGraph2D(myNewScene);
myNewScene.setDebugOn( BIT(5) );

// Show Bounding-Box for my Object.
new fxStaticSprite2D(myNewSprite);
myNewSprite.setDebugOn( BIT(1) );

BIT#0 Statistics Debug Banner.
BIT#1 Object Bounding/Clipping-Boxes.
BIT#2 Object Mount-Nodes.
BIT#3 Object Mount-Links (force tracking paths).
BIT#4 Object World-Limit.
BIT#5 Object Collision Polygon.

- Melv.
#4
03/01/2005 (4:22 pm)
I made a slightly modified version of LabRat's tile resizer thingy, that will do indexed image tiles and animated tiles. I left the active tile test there so that if anyone figures out how to do it, it could be implemented. Thanks again LabRat, my artist would have killed me if i didn't get that working. ;)

function fixCollision(%layer)
{
   %tileCount = %layer.getTileCount();
   for (%x = 0; %x < getWord(%tileCount,0); %x++)
   {
      for (%y = 0; %y < getWord(%tileCount,1); %y++)
      {
         // get tile information
         %collision = %layer.getTileCollision(%x SPC %y);
         %tile = %layer.getTileType(%x SPC %y);
         
         // Set static tile
         if(getWord(%tile, 0) $= "static")
         {
            %layer.clearTile(%x SPC %y);
            %layer.setStaticTile(%x SPC %y, getWord(%tile, 1), getWord(%tile, 2));
            %layer.setTileCollisionActive(%x SPC %y, getWord(%collision,0));
         }

         // Set animated tile
         if(getWord(%tile, 0) $= "animated")
         {
            %layer.clearTile(%x SPC %y);
            %layer.setAnimatedTile(%x SPC %y, getWord(%tile, 1));
            %layer.setTileCollisionActive(%x SPC %y, getWord(%collision,0));
         }

         // Set active tile
         if(getWord(%tile, 0) $= "active")
         {

         }
      }
   }
}
#5
03/01/2005 (4:25 pm)
This actually shows some interesting things you can do with the TileMaps. You can do Dynamic Tile changes within your game.

Actually.. this method could be used to do a game of GO using nothing but tiles.
#6
03/01/2005 (6:17 pm)
Very cool, thanks guys. :)
#7
03/01/2005 (11:40 pm)
Labrat, Melv, Ray--

thanks a ton! Now it's starting to feel like it's working, which is very, very sweet.
#8
03/07/2005 (4:40 am)
FYI

I have fixed this problem. It will be included in the next update.

Thanks for the report.

- Melv.