Hi again folks. I am still having problems getting to grips with tile based collisions. I am using C# and XNA. Below is my complete Game1 class for the program. I managed to get the walkable/non-walkable areas working just fine. But I want to make it possible for my character to slide around the edges of the map and around obstacles. Could anyone give me some pointers on how to change my code to make sliding possible? Any help greatly appreciated.
namespace OMGTILESAGAIN{ /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; SpriteBatch spriteBatch; Texture2D[] tiles = new Texture2D[2]; Texture2D playerTexture; Vector2 playerLocation = new Vector2(32, 32); Vector2 playerVelocity = new Vector2(50, 50); KeyboardState keyboardState; const int mapHeight = 8; const int mapWidth = 8; int tileWidth = 32; int tileHeight = 32; // tiles the player is touching int PlayerTileX; int PlayerTileY; // map to be drawn int[,] Map = new int[mapHeight, mapWidth] { { 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1}, }; // Walkable/ non-walkable areas int[,] Walkable = new int[mapHeight, mapWidth] { { 1, 1, 1, 1, 1, 1, 1, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 1, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 0, 0, 0, 0, 0, 0, 1}, { 1, 1, 1, 1, 1, 1, 1, 1}, }; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here spriteBatch = new SpriteBatch(graphics.GraphicsDevice); base.Initialize(); } /// <summary> /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// </summary> /// <param name="loadAllContent">Which type of content to load.</param> protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content tiles[0] = content.Load<Texture2D>("whiteTile"); tiles[1] = content.Load<Texture2D>("blackTile"); playerTexture = content.Load<Texture2D>("redTile"); } // TODO: Load any ResourceManagementMode.Manual content } /// <summary> /// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// </summary> /// <param name="unloadAllContent">Which type of content to unload.</param> protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { // TODO: Unload any ResourceManagementMode.Automatic content content.Unload(); } // TODO: Unload any ResourceManagementMode.Manual content } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here //Find the tile that the player is on, on the X axis PlayerTileX = (int)playerLocation.X / tileWidth; //Find the tile that the player is on, on the Y axis PlayerTileY = (int)playerLocation.Y / tileHeight; keyboardState = Keyboard.GetState(); //move the player if (keyboardState.IsKeyDown(Keys.Up)) { if (Walkable[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY, PlayerTileX + 1] == 0) { playerLocation.Y -= playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Down)) { if (Walkable[PlayerTileY + 1, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLocation.Y += playerVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Left)) { if (Walkable[PlayerTileY, PlayerTileX] == 0 && Map[PlayerTileY + 1, PlayerTileX] == 0) { playerLocation.X -= playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } } if (keyboardState.IsKeyDown(Keys.Right)) { if (Walkable[PlayerTileY, PlayerTileX + 1] == 0 && Map[PlayerTileY + 1, PlayerTileX + 1] == 0) { playerLocation.X += playerVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds; } } base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); // Draw the map for (int y = 0; y < mapHeight; y++) { for (int x = 0; x < mapWidth; x++) { spriteBatch.Draw(tiles[Map[y, x]], new Vector2(x * tileWidth, y * tileHeight), Color.White); } } // Draw the player. spriteBatch.Draw(playerTexture, playerLocation, Color.White); spriteBatch.End(); base.Draw(gameTime); } }}