Tile map collision is not working properly

Posted by Sigh-AniDe on Game Development See other posts from Game Development or by Sigh-AniDe
Published on 2012-05-30T11:10:35Z Indexed on 2012/05/30 17:01 UTC
Read the original article Hit count: 337

Filed under:
|
|
|

I am having problems setting collision between my sprite and the tiles. I have only done the code for colision for moving upwards but some places on the map it moves up and some places it doesn't.

Here is what I have so far:

Vector2 position;
private static float scalingFactor = 32;

static int tileWidth = 32;
static int tileHeight = 32;

int[ , ] map = {
                        {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, 1, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 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, 1, 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, 1, 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, 1, 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, 1, 0, 0, 0, 0, },
                        {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 1, 1, 1, 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, 1, 1, 1, 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, 1, 1, 1, 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, },
                   };

// This is in turtle.update

if ( keyboardState.IsKeyDown( Keys.Up ) )
{
    if ( position.Y > screenHeight / 4 )
    {
        //// current position of the turtle on the tiles
        int mapX = ( int )( position.X / scalingFactor );
        int mapY = ( int )( position.Y / scalingFactor ) - 1;

        if ( isMovable( mapX , mapY , map ) )
        {
            position.Y = position.Y - scalingFactor;
        }
    }
    else
    {
        MoveUp();
    }
}

private void MoveUp()
{
    motion.Y = -1;
}

public bool isMovable( int mapX , int mapY , int[ , ] map )
{
    if ( mapX < 0 || mapX > 19 || mapY < 0 || mapY > 20 )
    {
        return false;
    }
    int tile = map[ mapX , mapY ];
    if ( tile == 0 )
    {
        return false;
    }

    return true;
}

protected override void Update( GameTime gameTime )
{
    turtle.Update( screenHeight , scalingFactor , map );
    base.Update( gameTime );
}

© Game Development or respective owner

Related posts about XNA

Related posts about c#