TMXTiledMap Scrolling & Collisions


#1

Hi Everyone,
I’m currently making a Space Shooter game. I’m using a TMX Map for the level design.
When I load the TMX Map (without scrolling), I can detect collision on the collision layer no problem. The problem occurs when I begin to have the map scroll.
I don’t think I’m converting the coordinate for the detection appropriately. If anyone could point me in the right direction, it would be a big help.

Here’s the code for moving the map (CCTMXTileMap object) located in my update method:
CPoint backgroundScrollVert = ccp(0,30) ;
*tileMap
>setPosition,ccpMult));
Here’s method to convert the laser position to the tilemap position
CCPoint GameMain::tileCoordForPosition
{
int x = position.x / *tileMap~~>getTileSize.width;
int y = *tileMap~~>getMapSize.height~~ .height);
return ccp;
}
Here’s the code for the collision detection:
CCObject itLaser;
CCARRAY_FOREACH {
CCSprite
shipLaser = itLaser;
CCPoint tilept = tileCoordForPosition);
if
{
int tileGid =*meta~~>tileGIDAt; //get tile id from collision layer
if )
continue ;
if
{
CCDictionary *props =*tileMap~~>propertiesForGID;
if
{
*turretLayer~~>removeTileAt(tilept);
_meta~~>removeTileAt;
shipLaser~~>setVisible(false) ;
}
}
}
}

Update #1:
Did some debugging. I have some interesting numbers. Now my ship is static, always floating at the bottom of the screen. The map size is 320 x 960. When the tilemap gets to the end and stops, and the ship shoots it’s lasers,
the laser coordinates are still reading as if they’re starting at the bottom center of the screen (160, 10) as opposed to being (160, 480). Since the coordinates are read as that, then when they get converted to the tilemap coordinates, they’re incorrect, because we already passed those points on the tilemap. This is certainly not a fix, but a step in the right direction. I believe the viewport coordinates are not properly being updated. Any suggestions?


#2

Hi Everyone,
I just wanted to update this thread. I was able to figure out the issue.
For the collision detection, where I convert the laser position to the tilemap position, I needed to add the tileset position as an offset like this:
CCPoint actualLaserPosition = ccpAdd(shipLaser~~>getPosition,_tilemap~~>getPosition());
CCPoint tilept = tileCoordForPosition(actualLaserPosition);

This would then give me the correct tile coordinates for me to cross reference with my collision layer.