Note that there are some explanatory texts on larger screens.

plurals
  1. POPacman collision detection
    text
    copied!<p>I am trying to create a pacman game to learn XNA, but I am having some problems to get the collision detection working. The game is tile based, where 1 is a wall and 0 is walkable. It then takes the tile you are standing on plus the 4 around it, and if it collides with one of them and the tile value is not 0 will it reset the position to what it was before the move. For some reason however it just doesn't work, it gets randomly stuck and sometimes I can even move though walls. <img src="https://i.stack.imgur.com/Fx20E.png" alt="enter image description here"></p> <p>Here is my collision detection:</p> <pre><code> var oldPos = Position; // Updates the Position base.Update(theGameTime, mSpeed, mDirection); // Test Collidetion Rectangle objRect = new Rectangle((int)Position.X, (int)Position.Y, 32, 32); bool isCollided = false; Vector2 curTitle = GetCurrentTitle(); // Test UP, DOWN, LEFT, RIGHT int tile; Rectangle testRect; if ((int)curTitle.Y &lt; 0 || (int)curTitle.X &lt; 0 || (int)curTitle.Y &gt;= map.MapSizeWidth - 1 || (int)curTitle.X &gt;= map.MapSizeHeight - 1) isCollided = true; if (!isCollided) { tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X]; testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); if (tile != 0 &amp;&amp; rectangle_collision(testRect, objRect)) isCollided = true; if (curTitle.Y != 0) { tile = map.Tiles[(int)curTitle.Y - 1, (int)curTitle.X]; testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y - 1) * map.TileSize, map.TileSize, map.TileSize); if (tile != 0 &amp;&amp; rectangle_collision(testRect, objRect)) isCollided = true; } tile = map.Tiles[(int)curTitle.Y + 1, (int)curTitle.X]; testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y - 1) * map.TileSize, map.TileSize, map.TileSize); if (tile != 0 &amp;&amp; rectangle_collision(testRect, objRect)) isCollided = true; if (curTitle.X != 0) { tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X - 1]; testRect = new Rectangle(((int)curTitle.X - 1) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); if (tile != 0 &amp;&amp; rectangle_collision(testRect, objRect)) isCollided = true; } tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X + 1]; testRect = new Rectangle(((int)curTitle.X + 1) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); if (tile != 0 &amp;&amp; rectangle_collision(testRect, objRect)) isCollided = true; } if (isCollided) Position = oldPos; </code></pre> <p>Can any one see why my collision detection is not working?</p> <p><strong>EDIT:</strong> I have uploaded the whole project to <a href="http://sogaard.us/Pacman.zip" rel="nofollow noreferrer">http://sogaard.us/Pacman.zip</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload