Hi!
I've been making a few posts about tile maps and scrolling tile maps. Since my last post I have finished almost everything; I have the map scrolling and npcs staying where they are put until I tell 'em to move. Now my current and last problem I see is that the collision is WAY off. When I move the collision boxes moves.
int map[15][28]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
The ones are the hitboxes and zeros are empty.
This is the drawing code
for( x = 0; x < 3; x++) {
clipTiles[ x ].x = x*32;
clipTiles[ x ].y = 0;
clipTiles[ x ].h = 32;
clipTiles[ x ].w = 32;
}
for( y = 0; y < 15; y++)
for( x = 0; x < 28; x++)
{
SDL_Rect tileC = {x*32,y*32,32,32};
SDL_Rect offset;
offset.x = x*32 - camera.x;
offset.y = y*32 - camera.y;
marioX = x*32 - camera.x;
marioY = y*32 - camera.y;
SDL_BlitSurface(tiles, &clipTiles[map[y][x]], screen, &offset);
}
The collision function
bool collisionTile(SDL_Rect* player,int direction)
{
if(direction == 1 && (map[(player->y/32)+1][player->x/32] == 1 || map[(player->y/32)+1][(int)ceil(player->x/32.0)] == 1))
return true;
if(direction == 2 && (map[(int)ceil(player->y/32.)-1][player->x/32] == 1 || map[(int)ceil(player->y/32.)-1][(int)ceil(player->x/32.)] == 1))
return true;
if(direction == 3 && (map[player->y/32][(int)ceil(player->x/32.)-1] == 1 || map[(int)ceil(player->y/32.)][(int)ceil(player->x/32.)-1] == 1))
return true;
if(direction == 4 && (map[player->y/32][(player->x/32)+1] == 1 || map[(int)ceil(player->y/32.)][(player->x/32)+1] == 1))
return true;
return false;
}
and the keys to move
if(keysHeld[SDLK_LEFT])
{
direction=3;
if(!collisionTile(&player,direction))
playerX -= 1;
}
if(keysHeld[SDLK_RIGHT])
{
direction=4;
if(!collisionTile(&player,direction))
playerX += 1;
}
if(keysHeld[SDLK_UP])
{
direction=2;
if(!collisionTile(&player,direction))
playerY -= 1;
}
if(keysHeld[SDLK_DOWN])
{
direction=1;
if(!collisionTile(&player,direction))
playerY += 1;
}
Thanks for any help
