Help displaying my varibles on the screen

Started by
9 comments, last by evillive2 19 years, 4 months ago
I don't quite understand the question but maybe some of this might help:
// given world x and y values, convert them to screen x and y valuesvoid world_to_screen( int *x, int *y ){    *x -= scrollx;    *y -= scrolly;}// given screen x and y values, convert them to world x and y valuesvoid screen_to_world( int *x, int *y ){    *x += scrollx;    *y += scrolly;}// given world coordinates, convert them to tilemap coordinatesvoid world_to_tilemap( int *x, int *y ){    *x /= tile_width;    *y /= tile_height;}// given tilemap coordinates, fill a rectangle with that tiles world coordinatesvoid tilemap_to_world_tile_rect( int x, int y, RECT *rc ){    rc->left   = x*tile_width;    rc->top    = y*tile_height;    rc->right  = rc->left+tile_width;    rc->bottom = rc->top+tile_height;}

It looks like you are trying to tackle collision detection which can be confusing for anyone. With the pseudo code I gave you, you might want to try this out:
// destx and desty are the coordinates you WANT to move the character to but// haven't yet.// destx is player.worldx + movex// desty is player.worldy + moveybool player_can_move_to( int destx, int desty ){    int tilex, tiley;    // get the tilemap coordinates    tilex = destx;    tiley = desty;    world_to_tilemap( &tilex, &tiley );    // check if the tile is walkable or not    if ( map[tilex][tiley] == 1 )        return FALSE; // tile is not walkable    return TRUE;}

It is very important to check this before you move or your character may appear to jump back when he collides with a non walkable tile.

The above example is a very simple and not very exact way of detecting collision between a player and a non walkable tile. A better way would be to use a bounding rectangle around the player and see if that collides with any neighboring non-walkable tiles.

Much like using an anchor point for a sprite so that it gets drawn in the correct spot, my collision rectangle is usually just an offset around the players position. For example, lets say I have a sprite that is 32x32 pixels and his anchor point is right in the middle of his feet. The anchor point would look something like (16,31) which is an offset from the top left of the sprite (0,0) or it's origin. Using this same theory, we can say I want my sprite to have an 8x6 pixel bounding box using it's anchor point as it's origin. The bounding box's coordinates would look something like ( -4,-3, 4,3 ). Here is an example of how this would get implemented using this sprite:
bool player_can_move( Player *player, int destx, int desty ){    RECT player_rect, tile_rect, temp;    int tilex, tiley;    // get the tilemap coordinates    tilex = destx;    tiley = desty;    world_to_tilemap( &tilex, &tiley );    // get the world coordinates of the players bounding box    // if he were to move to that position.    // Note: CopyRect, OffsetRect and IntersectRect are Win32 specific    // but you should have access to similar functions handy anyway.    CopyRect( &player_rect, &player->bounding_box );    OffsetRect( &player_rect, destx, desty );    // before we only checked if we were on a non waklable tile    // now we are going to check if we are colliding with any non walkable tiles    // around us as well    // check if the tile is walkable or not    // we are on it so we don't have to check the bounding box    if ( map[tilex][tiley] == 1 )        return FALSE; // tile is not walkable    // check the 8 tiles around us    for ( int offy = -1; offy <= 1; offy++ )    {        for ( int offx = -1; offx <= 1; offx++ )        {            if ( map[tilex+offx][tiley+offy] == 1 ) // non walkable            {                tilemap_to_world_tile_rect(tilex+offx, tiley+offy, &tile_rect);                if ( IntersectRect( &temp, &player_rect, &tile_rect ) )                    return FALSE; // BAM!            }        }    }    return TRUE;}

That isn't optimized but should provide a solid base for simple bounding box collision in your case.

Hope that helped.
Evillive2

This topic is closed to new replies.

Advertisement