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