XNA를 배우기 위해 팩맨 게임을 만들려고하고 있지만 충돌 감지 기능을 작동시키는 데 문제가 있습니다. 게임은 타일 기반이며, 1은 벽이고 0은 걷기가 가능합니다. 그러면 서있는 타일과 주변의 타일 4가 걸리고, 그 중 하나와 충돌하고 타일 값이 0이 아닌 경우 이동 전의 위치로 재설정됩니다. 그러나 어떤 이유로 그것은 단지 작동하지 않습니다, 그것은 무작위로 붙어 얻고 때로는 심지어 벽을 움직일 수 있습니다. 내 충돌 감지가 작동하지 않는 이유팩맨 충돌 감지
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 < 0 || (int)curTitle.X < 0 || (int)curTitle.Y >= map.MapSizeWidth - 1 || (int)curTitle.X >= 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 && 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 && 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 && 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 && 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 && rectangle_collision(testRect, objRect))
isCollided = true;
}
if (isCollided)
Position = oldPos;
가 하나라도 볼 수 있습니다 여기에
내 충돌 감지입니까?
편집 : 내가 업로드 한 http://sogaard.us/Pacman.zip
나는 그것을 뒤로 움직이게하지 않을 것입니다. 나는 그것을 더 효율적으로하고 더 나은 방법으로 움직이는 것을 멈출 것입니다. – niico