1
votes

Is there any chance to get bounding box of tile of tiled map and check if this bouding box collide with sprite bounding box?

1
I'm sure there is, but I imagine that you wouldn't want this to occur for every tile. It would probably be faster to check what tiles are under the player using the player's world coordinates and then do something with those. - Richard Marskell - Drackir
It is not so simple I must check tiles over,under,left and right - Faraona
You mean you want to check the tiles over, under and to the left and right of the tile the player is on? Once you have the tiles the player is on, it's simple to get the ones next to them. - Richard Marskell - Drackir

1 Answers

3
votes

To get the bounding box of a tile at a given position of the tilemap:

- (void) setupTilemap
{
    NSString * aFilename = @"tilemap.tmx";
    theTilemap = [CCTMXTiledMap tiledMapWithTMXFile:aFilename];
    [self addChild:theTilemap];   
}

- (CGRect) tileBoundingBoxAt:(CGPoint)mapCoordinate
{
    CGPoint tilepos;
    tilepos.x = abs(mapCoordinate.x / theTilemap.tileSize.width);
    tilepos.y = abs(mapCoordinate.y / theTilemap.tileSize.height);

    CGRect boundingBox;
    boundingBox.origin.x = tilepos.x * theTilemap.tileSize.width;
    boundingBox.origin.y = tilepos.y * theTilemap.tileSize.height;
    boundingBox.size.width = theTilemap.tileSize.width;
    boundingBox.size.height = theTilemap.tileSize.height;

    return boundingBox;
}

But, as already said in the comments, this is probably not the most effective way of doing it.

For instance if you want to check if a sprite's bounding box is (or is about to) collide with a certain type of tile (say a wall or water) then you can check for the type of tiles at a several points around your sprite.

So, instead of just testing the tile at your anchor point (i.e. middle of your sprite)

-------
|     |
|  X  |
|     |
-------

You can instead test all 4 corners of your sprites bounding box.

X-----X
|     |
|     |
|     |
X-----X

You can test as many points as you need for your games requirements. Depending on what you need other offsets might make even more sense for you.

For instance only horizontally left and right, but with an offset ahead of the sprite:

   -------
   |     |
 X |     | X
   |     |
   -------

   -------
   |     |
 X |     | X
   |     |
   ------- 
 X    X    X

The benefit is that you only do a test for each of your "sensor" points. If you use the bounding box you have to test for each tile that is relevant. As soon as you test against more than a handful of tiles it is less effective.

Update. Example for a bouncing ball:

Assuming the ball collides with only 1 wall (a floor) which is always below the ball Say your ball has its anchor point in the center. you calculate (X) using the radius of the ball. you can calculate the radius by dividing the sprite's bounding box height / 2.

   -------
   |     |
   |  .  |
   |     |
   ---X--- 

TTTTTTTTTTTTTTTTTTTTTTT

. = anchor point X = location to test for collision with floor T = tiles

get the coordinate of your sprite which is the anchor point (.) calculate coordinate of point to test (X) testpoint xpos = the same as your ball sprite testpoint ypos = ball sprite y pos + radius (if you want + a little offset to test ahead)

each frame you get the tile at coordinate (X) and check if it is a tile with collision property. if it is then you have a collision and react accordingly (change of motion direction in this case).

if you want the ball to be able to collide with the ceiling and the side walls as well then check at all 4 edges like that:

      X
   -------
   |     |
 X |  .  | X
   |     |
   ------- 
      X