I'm building my first platformer using cocos2d and a tilemap. I'm having trouble coming up with a decent way of determining if the character is colliding with an object. More specifically, in which direction is the character colliding with an object.

Following the tutorial here, I have made a separate "meta" layer of collidable tiles. The problem is that unless the character is in the tile, you can't detect the collision. Also, there's no way of telling WHERE the collision is occurring.

The best solution would be one that could tell me if a character is up against a wall, or walking on top of a platform. However, I can't seem to figure out a good technique for this.


1 Answer 1


first of all you don't need the character to be a tile, your character is basically some ccNode Object (it can be a sprite, a tile, an animated image or whatever) and every ccNode instance has a boundingBox (I'm not sure property or function) you can easily check if two bounding boxes collide or not. depending on your layer type you can use either one of these two sudo codes:

// if you have an object layer
    foreach(tile in layer->tiles())
        if (character->boundingbox.isColliding(tile->boundingBox()))

// if you have a normal tile layer
    for(x = character->boundingbox->left / layer->tileWidth;x < character->boundingbox->right / layer->tileWidth; x++)
        for(y = character->boundingbox->yop / layer->tileHeight;x < character->boundingbox->bottom / layer->tileHeight; y++)
            if (layer->tileAt(x,y) == validTile)

to find out from which direction was the collision generated you can easily check which way where your objects moving, just check their relative movement speed, depending on their speed they can only create collision in one direction. this way you can choose if a collision was made from up or down, or if it was made from left or right. to choose between up and left you have to use more complex algorithm. the base idea is to check which type of collision is possible and/or more likely. if two object have intersection in a 1px*20px area you can assume they where already in a same above each other and then one of them fell down on the other object.

if you still didn't get what did I mean just put a comment and i'll try to explain better.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .