Collisions with tiles on a Layer
by Chris · in Torque Game Builder · 05/01/2005 (11:43 pm) · 6 replies
// This is what I use to load the map
%map = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
%map.loadTileMap("tileEditor/client/maps/testmap1.map");
%layer1 = %map.getTileLayer(0);
%layer1.setPosition("0 0");
%layer1.setLayer(2);
%layer1.setGroup(2);
$layer = %layer1;
$map = %map;
// load map code done
function proccessLayer() {
for(%x = 0; %x < getWord($layer.getTileCount(), 0); %x++) {
for(%y = 0; %y < getWord($layer.getTileCount(), 1); %y++) {
%type = $layer.getTileType(%x SPC %y);
if(getWord(%type, 1) $= "nowalk") {
$layer.setTileCollisionActive(%x SPC %y, true);
$layer.setTileCollisionPolyPrimitive(%x SPC %y, 4);
$layer.setTileCollisionScale(%x SPC %y, "1 1");
}
}
}
}I think this is the easiest way to allow a tile to be collidable. The problem is my character still moves right through it.Character Code
function setupTestCharacter() {
$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
$player.setPosition("0 0");
$player.setImageMap(ggLogoImageMap);
$player.setSizeX(4);
$player.setSizeY(4);
$player.setLayer(1);
$player.setGroup(1);
$player.setCollisionCallback(true);
$player.setCollisionActive(true, true);
$player.setCollisionPhysics(false, false);
$player.setCollisionScale("1 1");
$player.setCollisionMasks(BIT(2),BIT(2));
new ActionMap(playerMap);
playerMap.bindCmd(keyboard, "w", "moveUp();", "stopY();");
playerMap.bindCmd(keyboard, "s", "moveDown();", "stopY();");
playerMap.bindCmd(keyboard, "a", "moveLeft();", "stopX();");
playerMap.bindCmd(keyboard, "d", "moveRight();", "stopX();");
playerMap.push();
}I think it is because of the collision Masks havnt been set for the layer (dont think you can). I tried setting the layer and group for the tilelayer but that didn't do anything. Help please.
About the author
#2
Here is a snippet:
Thanks!
-Chad
05/28/2005 (9:44 pm)
I've tried to do that in my little test game and it doesn't seem to work. It seems like the only wayI can can get any collision is by making my one tile-layer a static sprite. Any ideas?Here is a snippet:
%groundLayer = %desertMap.getTileLayer(2);
%groundLayer.setImageMap(grImageMap);
%groundLayer.setPosition("0 0");
%groundLayer.setTileSize("100 75");
%groundLayer.setSize("100 75");
%groundLayer.setLayer(1);
%groundLayer.setGroup(1);
%groundLayer.setCollisionActive(false,true);
%groundLayer.setCollisionPolyCustom(4, "-1.000000 0.800000 1.000000 0.800000 1.000000 1.000000 -1.000000 1.000000");
%groundLayer.setCollisionScale("1 1");
%groundLayer.setCollisionPhysics(false,true);
%groundLayer.setCollisionMaterial(immovableMaterial);
%groundLayer.setCollisionMasks(BIT(1),BIT(1));
%groundLayer.setDebugOn(BIT(5));
%groundLayer.setImmovable();Thanks!
-Chad
#3
Try something like this:
I suspect it will also be easier for you to set collision poly in the tile editor itself so they are part of the map rather then spend a significant amount of game time checking your tile layer and creating the polys on the fly.
The TileEditor.pdf file that comes with T2D explains this, although I must admit I haven't used it yet.
05/29/2005 (12:20 am)
What have you got in your onCollision callback? If you haven't got a callback telling your game what to do when your player hits something, then nothing will happen.Try something like this:
function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
// player hits something
if (%srcObj = $player)
{
$player.setAtRest();
}
}I suspect it will also be easier for you to set collision poly in the tile editor itself so they are part of the map rather then spend a significant amount of game time checking your tile layer and creating the polys on the fly.
The TileEditor.pdf file that comes with T2D explains this, although I must admit I haven't used it yet.
#4
Oh hey that worked. I had an onCollision callback but I didn't have the same number of parameters... crazy. I plan to have the collision poly definitions in the tile editor but for the sake of experimenting I am doing it on the fly. Thanks!!
-Chad
05/29/2005 (7:09 am)
Philip, Oh hey that worked. I had an onCollision callback but I didn't have the same number of parameters... crazy. I plan to have the collision poly definitions in the tile editor but for the sake of experimenting I am doing it on the fly. Thanks!!
-Chad
#5
05/29/2005 (7:24 am)
Excellent, glad it worked :)
#6
05/29/2005 (2:17 pm)
Hrm... you know I might have spoken too soon. I had a typo so T2D reverted to the last working .dso rather then using my changes. What a bummer. I still seem to be unable to get collisions with tile layers.
Torque Owner Gary Preston
// wall layer - determins which tiles are walkable %w_layer = %this.levelMap.getTileLayer($Game::map::WALL_MAP_LAYER); %w_layer.setLayer( $Game::rendering::WALL_LAYER ); %w_layer.setGroup( $Game::collision::WALL_GROUP ); %w_layer.setCollisionMaterial( immovableMaterial ); %w_layer.setCollisionActive( false, true ); %w_layer.setCollisionCallback( true ); %w_layer.setCollisionMasks( BIT( $Game::collision::PLAYER_GROUP ) | BIT( $Game::collision::ENEMY_GROUP ), BIT( $Game::rendering::PLAYER_LAYER ) | BIT( $Game::collision::ENEMY_LAYER )); %w_layer.setCollisionPhysics(false, false); // we're using our own modelFrom what I can tell, you just get the tile layer then set the collision masks/callbacks as you would any other object.