So I have been trying to use Tiled Maps and to detect collision. I am perfectly able to show the TiledMap in the game but the for collision, the tiles and their collision positions are different.
In the picture you can see the position of tiles but the collision is detected on the Pink cats positions.
Am I making any sense here? Thankful for the time you took for reading the post. Sorry if there are similar questions posted (I cannot make sense out of them). Will be grateful for your help! (
My code:
These two functions convert postions for tileMaps to screen and vice versa
Point HelloWorld::tileCoordForPosition(Point position)
{
int x = position.x / _map1->getTileSize().width;
int y = ((_map1->getMapSize().height * _map1->getTileSize().height) - position.y) / _map1->getTileSize().height;
return Point(x, y);
}
Point HelloWorld::PostionForTileCoord(Point position){
int x = position.x *_map1->getTileSize().width;
int y = (_map1->getMapSize().height * _map1->getTileSize().height)-position.y*_map1->getTileSize().height;
return Point(x, y);
}
I have used this method to show the position of cat where collidable property is set on tile:
void HelloWorld::PaintMeta(){
int te= _map1->getTileSize().width;
for (int x = 0; x < _collidable1->getLayerSize().width; x++)
{
for (int y = 1; y < _collidable1->getLayerSize().height; y++)
{
int tileGid = _collidable1->getTileGIDAt(Point(x,y));
if (tileGid) {
auto properties = _map1->getPropertiesForGID(tileGid).asValueMap();
if (!properties.empty()) {
auto collision = properties["Collidable"].asString();
if ("True" == collision) {
auto testCat= CCSprite::create("cat.png");
testCat->setScale(0.05);
testCat->setColor(ccc3(255, 0, 255));
Point tileCoord= PostionForTileCoord(Point(x,y));
testCat->setPosition(tileCoord);
this->addChild(testCat);
}
}
}
}
}
}
And this is the collision detection code:
auto location = touch->getLocation();
Point tileCoord = this->tileCoordForPosition(location);
int tileGid = _collidable1->getTileGIDAt(tileCoord);
if (tileGid) {
auto properties = _map1->getPropertiesForGID(tileGid).asValueMap();
if (!properties.empty()) {
auto collision = properties["Collidable"].asString();
if ("True" == collision) {
return; //if doesn't return i do something further in the program
}
}
}