Game Development Community

How do you enable collisions with tilemaps?

by Chris Newman · in Torque Game Builder · 02/27/2005 (7:22 pm) · 4 replies

Does any one know how to enable collisions with a tilemap layer?
I tried doing it the same way as the tuterial did with normal objects but it does not seem to work with tilemaps.

So far i enabled collisions for the tiles i wanted in the tilemap editor and they show up in the debug mode as having collisions enabled.

#1
02/27/2005 (7:49 pm)
// create the blocks tilemap
$brickMap = new fxTileMap2D() { scenegraph = t2dSceneGraph; };

// load the saved tilemap file
$brickMap.loadTileMap("~/client/maps/bricks.map");

// load the tile layer from the map
$brickLayer = $brickMap.getTileLayer(0);

// position the bricks
$brickLayer.setPosition("0 -125");

// setup the layer collisions
$brickLayer.setGroup(1);
$brickLayer.setLayer(1);
$brickLayer.setCollisionActive(false, true);
$brickLayer.setCollisionMaterial(immovableMaterial);
$brickLayer.setCollisionMasks(BIT(2), BIT(2));
$brickLayer.setCollisionCallback(true);
$brickLayer.setCollisionPhysics(true, false);
That works for me. Make sure you have the layers and groups masked properly, and that the CollisionActive flags are set properly
#2
02/27/2005 (8:26 pm)
Now i got it, dunno what i was doing wrong.
Now all i need to figure out is what tile i hit.
#3
02/27/2005 (9:03 pm)
Function fxSceneObject2D::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)

The two bolded params contain the X/Y of the tile that was hit. So for example:
// tile collision?
if (%srcObj == $brickLayer)
{
	// delete the tile in question
	$brickLayer.clearTile(%srcRef);
}
Make sure you remember to match up the srcDst with the srcRef and vice-versa - got me for a while when I was figuring it out! (thx LabRat!!)
#4
02/27/2005 (10:23 pm)
!! Love seeing this community-generated help. This place is already starting to rock. Thanks for asking good questions and giving good answers guys. :) Melv and I love to help out, but of course the more everyone supports each other, the more time we have to code up cool new stuff for ya.