Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075929 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:00:51 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Tilemap scrolling [last question, I promise]
Pages: [1]
Print
Author Topic: Tilemap scrolling [last question, I promise]  (Read 377 times)
indietom
Level 2
**

Tom


View Profile Email
« on: December 06, 2012, 10:42:45 AM »

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.
Code:
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
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
Code:
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
Code:
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  Coffee
Logged
Christian Knudsen
Level 10
*****



View Profile WWW Email
« Reply #1 on: December 06, 2012, 02:23:10 PM »

Do you apply the camera offset to the collision boxes?
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
Evan Balster
Level 10
*****


I live in this head.


View Profile WWW Email
« Reply #2 on: December 06, 2012, 02:33:46 PM »

Neither the player's position nor the boxes' position should be affected by the camera's position.  The VISUAL position of the player AND the boxes should both be affected, however.  One of these things is not being done right and as a result the illusion fails, that much is clear.
Logged

Creativity births expression.  Curiosity births exploration.
Our work is as soil to these seeds; our art is what grows from them...


Wreath, SoundSelf, Infinite Blank, Cave Story+, <plaid/audio>
Christian Knudsen
Level 10
*****



View Profile WWW Email
« Reply #3 on: December 06, 2012, 02:47:04 PM »

Yes, that's what I was getting at, though there isn't really a visual position of hitboxes. We know from a previous thread that he's already adding the camera offset to the visual presentation of the player -- I can't tell from the code if that offset is also taken into account when doing the collision check, which it shouldn't (neither for the player nor hitboxes).

Also, why aren't you defining all values in your array? You've got 15 rows and 28 columns, but on the first 14, you only define 27 columns, and only 24 in the last. I'm not a C++ programmer, though, so for all I know, it's perfectly valid. Just seems weird to me.
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
indietom
Level 2
**

Tom


View Profile Email
« Reply #4 on: December 27, 2012, 02:15:51 PM »

It's done. My very own tile thing. Thanks for all the help  Coffee
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic