Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:03:49+00:00 2026-05-28T17:03:49+00:00

I am trying to create a pacman game to learn XNA, but I am

  • 0

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. enter image description here

Here is my collision detection:

    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;

Can any one see why my collision detection is not working?

EDIT: I have uploaded the whole project to http://sogaard.us/Pacman.zip

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:03:50+00:00Added an answer on May 28, 2026 at 5:03 pm

    I’m not sure if this is causing the full problem. But on your third tile check (where you check the tile below) you are checking the tile above again.

    This part here:

    tile = map.Tiles[(int)curTitle.Y + 1, (int)curTitle.X];
    

    On the next line you still have this inside the params for the testRect:

    ((int)curTitle.Y - 1) * map.TileSize
    

    Should be:

    ((int)curTitle.Y + 1) * map.TileSize
    

    Full corrected snippet:

    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;
    

    Hope this helped 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a pacman game with pygame but I have run
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I'm trying create a RCP Application with Eclipse, but I can't get past the
I am trying to create a pacman-like game. I have an array that looks
I am trying create alert dailog using glade,but its not working .am i doing
I'm trying to create a basic Pacman game in C++ (I'll use Java syntax
So, im trying to create a pacman game in SFML, i created a class
Trying to create a black line in my view to separate text blocks but
Trying to create a facebook app just to learn and coming across a strange
I'm trying create a ASMX webservice that can perform a HTTP GET request. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.