2

I tried cocos2d TileMap sample from here, In this tilecord returned for given player position is wrong.

Here is code

Point HelloWorld::tileCoordForPosition(Point position)
{
    int x = position.x / mTileMap->getTileSize().width;
    int y = ((mTileMap->getMapSize().height * mTileMap->getTileSize().height) - position.y) / mTileMap->getTileSize().height;
    return Point(x, y);
}

Here is Map Screenshot enter image description here

Also tileCord returned is wrong. So collision is not working. I used it from sample in Ray Wenderlich site

Here is full sample code https://app.box.com/s/whunv70tstwxbgzxdvxfeu080y6gwucb

If anyone has time, then check it and please help me to find bug.

Guru
  • 21,652
  • 10
  • 63
  • 102
  • 1
    I can't be of your real help, but the first thing came in my mind is difference in anchor point position. The gaming engines like cocos2D often use coordinate system which starts its Y position from `left-bottom` instead of top-left. – Naveed Abbas Sep 04 '18 at 12:11
  • @ToughGuy Thanks for your reply, I understand your concern, TileMap coordinates system's y starts at top left, but cocos2d starts at bottom left. That's why Y position is flipped in above function. Same logic worked perfect for me in cocos2d-Obj.C project...now in Cocos2d-x something else wrong. – Guru Sep 04 '18 at 16:56
  • 1
    Try Logging the x y of both collision objects, (also in cocos2d-iphone) it will surely give you hint. – Naveed Abbas Sep 05 '18 at 06:35
  • 1
    @ToughGuy Thanks for your great hint...In cocos2dx for some reason spawn points returns exactly half of cocos2d-ObjC. If I multiply input touch point in tileCoordForPosition function then it returns right coordinates. Thanks for great tips – Guru Sep 05 '18 at 18:33
  • So I guess you answered you own question :) I'm wondering what would happen to the bounty? :D – Naveed Abbas Sep 07 '18 at 06:39
  • BTW check if cocos2d-iPhone is returning the points in screen rather than pixels in image and cocos2d-x is returning image pixels. – Naveed Abbas Sep 07 '18 at 06:51
  • @ToughGuy not getting what you mean. In cocos2dx, spawn-point(Object layer) returned also half of cocos2d-ObjC. Definitely its bug in cocos2dx right ? – Guru Sep 07 '18 at 14:05
  • Is your canvas or mainWindow size is the same in both projects? – Naveed Abbas Sep 08 '18 at 06:37
  • I didn't change anything...used default from new project...also tileMap spawn points are independent of window size right..still its coming half. – Guru Sep 08 '18 at 17:24
  • Everything at default settings and pixels are reporting half in one platform. Looks strange, did you use two images one with `@2x` for retina display? Chances are the `@2x` image is not loaded. – Naveed Abbas Sep 10 '18 at 04:32
  • not using any extension for image, I used separate folder for iPhone and iPad and provided search paths. – Guru Sep 10 '18 at 14:12

1 Answers1

0

In cocos2dx 3.17, spawn points returned is exactly half, same tileMap returns proper value in Cocos2d-ObjC project. So temporary fix is to multiply input value inside tileCoordForPosition by 2. This is temporary solution...still waiting for proper fix.

Point HelloWorld::tileCoordForPosition(Point position)
{
    Point newPos = Vec2(position.x*2, position.y*2);

    int x = newPos.x / mTileMap->getTileSize().width;
    int y = ((mTileMap->getMapSize().height * mTileMap->getTileSize().height) - newPos.y) / mTileMap->getTileSize().height;
    return Point(x, y);
}

Here is full working TileMap sample : https://app.box.com/s/r3kglzbx6naig896bq4my7opfeg6ftwz

Guru
  • 21,652
  • 10
  • 63
  • 102