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 );
}
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.
Answer
No problem with being new to game development. You just need to make sure to provide enough information so that we can properly help you. Your on the right track with your code and it looks like your isMoveable()
function is sound. I'm not sure what the difference between motion.Y
and position.y
are and why you're setting one vs the other depending on position.Y > screenHeight / 4
, so the following maybe isn't what you want to achieve. However, I would suggest this simple structure for moving your character around.
// current position of the turtle on the tiles
int mapX = (int)(position.X / scalingFactor);
int mapY = (int)(position.Y / scalingFactor);
if (keyboardState.IsKeyDown(Keys.Up))
{
if (isMovable(mapX, mapY-1, map))
{
position.Y = position.Y - scalingFactor;
}
} else if (keyboardState.IsKeyDown(Keys.Down))
{
if (isMovable(mapX, mapY+1, map))
{
position.Y = position.Y + scalingFactor;
}
} else if (keyboardState.IsKeyDown(Keys.Right))
{
if (isMovable(mapX+1, mapY, map))
{
position.X = position.X + scalingFactor;
}
} else if (keyboardState.IsKeyDown(Keys.Left))
{
if (isMovable(mapX-1, mapY, map))
{
position.X = position.X - scalingFactor;
}
}
EDIT
This could be because of the way you're indexing into the map. Note orientation for your map as defined in code is not the way it will display on screen. You're indexing into it like this:
int tile = map[ mapX , mapY ];
So mapX
is referencing the rows of your array and mapY
is referencing the columns. So the top row of tiles on your screen from left to right are referencing the last column in your array from top to bottom.
No comments:
Post a Comment