Game Development Community

Collision & Tilemap

by Jean-Fran · in Torque Game Builder · 07/27/2005 (1:50 pm) · 10 replies

Good day,

I'm trying to get collision working between a sprite and a tilemap. The sprite-to-sprite collision works without problems, but not sprite-to-tilemap.

I've looked at a lot of threads concerning similar problems, but could not find an reason why my sprite-to-tilemap collision is not working.

Here's the code I use to setup collision for an object and the tilemap. I'm pretty sure I'm doing the same thing for both, but I'm thinking a second set of eyes could probably spot my silly mistake.

// Test tilemap:
    $tilemap = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
    $tilemap.loadTileMap("./maps/test.map");
    $tilemap.setPosition("0 0");
    $tilemap.setSize("10 10");
    
    %layer = $tilemap.getTileLayer(0);
    
    %layer.setCollisionActive(true, true);
    %layer.setCollisionPhysics(false, true);
    %layer.setCollisionMasks(1, 1);
    %layer.setCollisionMaterial(immovableMaterial);
    %layer.setImmovable();
    %layer.SetDebugOn(BIT(5));
    // At this point, tile map is up and running - but no collision.

    // Test object:
    $block = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
    $block.setPosition("20 20");
    $block.setPolyPrimitive(20);
    $block.setImageMap(testImg);
    $block.setCollisionActive(true, true);
    $block.setCollisionPhysics(false, true);
    $block.setCollisionMasks(1, 1);
    $block.setCollisionMaterial(immovableMaterial);
    $block.setImmovable();
    // At this point, the object is up and running - full collision!
    // What am I missing?

Thanks in advance!

J-F

#1
07/27/2005 (2:13 pm)
When you call setCollisonMasks(1, 1); try this instead...

setCollisionMasks(BIT(1), BIT(1));

See if that helps.

I've just come back to T2D after a several month break, so I may be way off though...
#2
07/27/2005 (2:28 pm)
@Chris, thanks for the suggestion. It didn't solve the problem, but it may put me on the right path.

I've used setCollisionMasks(BIT(1), BIT(1)), as you proposed for all my sprites, including the tilemap, and surprisingly, _all_ my collisions stopped working. Just for the fun of it, I've tried setCollisionMasks(BIT(0), BIT(0)); (which should be the equivalent of setCollisionMasks(1,1);), and that worked.

Any ideas why group/layer 1 (BIT(0)) would work as opposed to group/layer 2 (BIT(1))? I think I need to do more reading on groups and layers :P

J-F
#3
07/27/2005 (2:42 pm)
When you call setCollisonMasks(1, 1); try this instead...

setCollisionMasks(BIT(1), BIT(1));

See if that helps.

I've just come back to T2D after a several month break, so I may be way off though...
#4
07/27/2005 (2:58 pm)
From the posted code, it looks like you are not setting a group and layer for either the tile layer or the block. You need to do something like

$block.setGroup( 2 );
$block.setLayer( 2 );

and then use the corresponding masks,
e.g.

%layer.setCollisionMasks(BIT(2), BIT(2));

etc...

I suspect that the objects are defaulting to groups and layers of 0 and this is why it works as is.
#5
07/28/2005 (3:03 am)
My forehead is now bleeding from banging my head against the desk. Duh!

Thanks Chris and Brian for your suggestions. If I would have carefully read your code, I would have fixed my problem a long time ago.

My problem was: $block.setGroup(BIT(2)) instead of $block.setGroup(2)
#6
07/28/2005 (4:28 am)
LOL We've all been there...just glad to help!
#7
09/19/2005 (11:28 am)
I'm bumping this post with my code because I have the same problem. Simply no collisions. I have gotten the exact same thing to work between two non-tile sprites, no problem.

function createBlock()
{
   $blocks::names[$blocks::amount] = "block" @ $blocks::amount;
   // Create Bouncy Ball.
   %ball = new fxStaticSprite2D($blocks::names[$blocks::amount]) { scenegraph = t2dSceneGraph; };
   %ball.setSize( 4 );
   %ball.setImageMap( tileMapImageMap );
   %ball.setCollisionActive( true, true );
   %ball.setCollisionPhysics( true, true );
   %ball.setCollisionMasks( BIT(0), BIT(0) );
   %ball.setCollisionMaterial( bouncyMaterial );
   %ball.setLayer( 1 );
   %ball.setGroup( 1 );
   // Give it a random direction.
   %ball.setImpulseForcePolar( getRandom()*360, 700 );

   $blocks::amount++;
}


function createTile()
{
    %scrollerMap = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
    %scrollerMap.loadTileMap("~/client/maps/floor.map");
    %Layer0 = %scrollerMap.getTileLayer( 0 );
    %Layer0.setPosition( "0 -10" );
    %Layer0.setSize( "140 60" );
    %Layer0.setCollisionActive(true, true);
    %Layer0.setCollisionPhysics(false, true);
    %Layer0.setCollisionMasks(BIT(1), BIT(1));
    %Layer0.setCollisionMaterial(immovableMaterial);
    %Layer0.setImmovable();
    %Layer0.setLayer( 0 );
    %Layer0.setGroup( 0 );
}

I've tried all kinds of permutations. What could it be?
#8
09/19/2005 (11:56 am)
And you have defined collision polygons for the tiles in tile editor? This may be too obvious but I didn't do it before I read the editor docs entirely :)
#9
09/19/2005 (1:05 pm)
That did it. It showed that many of my tiles didn't have collisions set up. I really thought I was careful (to turn the red X to a green check) when I was creating them, but I must have been sloppy.

-ty
#10
09/19/2005 (6:28 pm)
Useful tip w/ tile map editor: Create one tile, set the collision active flag on it, then create a brush out of that tile. Then you won't have to remember to set collision flags :-)