Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/208199881016360961
added 149 characters in body
Source Link
Sigh-AniDe
  • 73
  • 1
  • 11

EDIT: What i have tried to do is get the postion of my sprite and over which tile it is, then check whether the next tile is a path or a wall.

EDIT: What i have tried to do is get the postion of my sprite and over which tile it is, then check whether the next tile is a path or a wall.

formatting, and removed code that is not relevant to the isssue
Source Link
House
  • 73.3k
  • 17
  • 185
  • 273

Tile Map Collisionmap collision is not working properly

iI am new to gamedev, 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 doesntdoesn't.

hereHere is what iI 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, },
                   };
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 LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch( GraphicsDevice );

        Texture2D tempTexture = Content.Load<Texture2D>( "turtle" );

        turtle = new TurtleC( tempTexture , screenBounds );


        tiles.Add( Content.Load<Texture2D>( "walltile" ) );
        tiles.Add( Content.Load<Texture2D>( "pathTile" ) );

        screenHeight = GraphicsDevice.Viewport.Height;
        screenWidth = GraphicsDevice.Viewport.Width;
    }

 protected override void Update( GameTime gameTime )
    {
        // Allows the game to exit
        if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
            this.Exit();

        turtle.Update( screenHeight , scalingFactor , map );
 
        base.Update( gameTime );
    }

All help will greatly be appreciated. Thanks

Tile Map Collision not working properly

i am new to gamedev, 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 doesnt.

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, },
                   };
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 LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch( GraphicsDevice );

        Texture2D tempTexture = Content.Load<Texture2D>( "turtle" );

        turtle = new TurtleC( tempTexture , screenBounds );


        tiles.Add( Content.Load<Texture2D>( "walltile" ) );
        tiles.Add( Content.Load<Texture2D>( "pathTile" ) );

        screenHeight = GraphicsDevice.Viewport.Height;
        screenWidth = GraphicsDevice.Viewport.Width;
    }

 protected override void Update( GameTime gameTime )
    {
        // Allows the game to exit
        if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
            this.Exit();

        turtle.Update( screenHeight , scalingFactor , map );
 
        base.Update( gameTime );
    }

All help will greatly be appreciated. Thanks

Tile map collision is not working properly

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, },
                   };
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 );
}
Source Link
Sigh-AniDe
  • 73
  • 1
  • 11

Tile Map Collision not working properly

i am new to gamedev, 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 doesnt.

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 LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch( GraphicsDevice );

        Texture2D tempTexture = Content.Load<Texture2D>( "turtle" );

        turtle = new TurtleC( tempTexture , screenBounds );


        tiles.Add( Content.Load<Texture2D>( "walltile" ) );
        tiles.Add( Content.Load<Texture2D>( "pathTile" ) );

        screenHeight = GraphicsDevice.Viewport.Height;
        screenWidth = GraphicsDevice.Viewport.Width;
    }

 protected override void Update( GameTime gameTime )
    {
        // Allows the game to exit
        if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
            this.Exit();

        turtle.Update( screenHeight , scalingFactor , map );

        base.Update( gameTime );
    }

All help will greatly be appreciated. Thanks