### Problem with detecting walls

I am a student practicing C++ programming, and I am creating a simple 2d maze game, kind of like pac-man. I ran into a problem though. When the player hits a wall, they are able to go through it if they run into it from a certain side. For example if they run into a horizontal wall from left or right, the player goes right through the wall. If they run into a horizontal wall from top or bottom, the wall works like it's supposed to and prevents movement. The same goes for the vertical walls, except the problem occurs when a player runs into it from top to bottom.
This is my first game with graphics and I am really confused as to why my code isn't working.
Here is some of my code. Any help would be appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135`` ``````void Application2D::update(float deltaTime) { m_timer += deltaTime; int iter = 0; Vector2 moveUp(0, (400 * deltaTime)); Vector2 moveDown(0, (-400 * deltaTime)); Vector2 moveLeft((-400 * deltaTime), 0); Vector2 moveRight((400 * deltaTime), 0); //Vertical Walls mVerticalWall[0].SetPosition(110, 105); mVerticalWall[1].SetPosition(150, 105); //Horizontal Walls mHorizontalWall[0].SetPosition(65, 500); mHorizontalWall[1].SetPosition(500, 500); mHorizontalWall[2].SetPosition(450, 500); mHorizontalWall[3].SetPosition(400, 500); mHorizontalWall[4].SetPosition(350, 500); int facingVertWall = 0; int facingHorzWall = 0; int facingVertWallCorner = 0; int facingHorzWallCorner = 0; int horzWall; int vertWall; bool hitVertWall; bool hitHorzWall; //Check for vertical wall for player for (int i = 0; i < 10; i++) { if ((mPlayer->GetX() + 25) >= mVerticalWall[i].GetX() && (mPlayer->GetX() - 25) <= mVerticalWall[i].GetX()) { if ((mPlayer->GetY() + 20) >= (mVerticalWall[i].GetY() - 50) && (mPlayer->GetY() - 20) <= (mVerticalWall[i].GetY() + 50)) { std::cout << "You hit a vertical wall" << std::endl; hitVertWall = true; vertWall = i; break; } } else { hitVertWall = false; } } //Checks for Horizontal Wall for player for (int i = 0; i < 10; i++) { if ((mPlayer->GetY() + 15) >= (mHorizontalWall[i].GetY() - 10) && (mPlayer->GetY() - 15) <= (mHorizontalWall[i].GetY() + 10)) { if ((mPlayer->GetX() + 15) >= (mHorizontalWall[i].GetX() - 50) && (mPlayer->GetX() - 15) <= (mHorizontalWall[i].GetX() + 50)) { std::cout << "you hit a horizontal wall" << std::endl; hitHorzWall = true; horzWall = i; break; } } else { hitHorzWall = false; } } if (hitVertWall == true) { if (mPlayer->GetX() <= mVerticalWall[vertWall].GetX()) { facingVertWall = 1; //Can't move RIGHT } else if (mPlayer->GetX() >= mVerticalWall[vertWall].GetX()) { facingVertWall = 2; //Can't move LEFT } if (mPlayer->GetY() <= mVerticalWall[vertWall].GetY()) { facingVertWallCorner = 3; //Can't move UP } else if (mPlayer->GetY() >= mVerticalWall[vertWall].GetY()) { facingVertWallCorner = 4; //Can't move DOWN } } if (hitHorzWall == true) { if (mPlayer->GetY() <= mHorizontalWall[horzWall].GetY()) { facingHorzWall = 3; //Can't move UP } else if (mPlayer->GetY() >= mHorizontalWall[horzWall].GetY()) { facingHorzWall = 4; //Can't move DOWN } if (mPlayer->GetX() == mHorizontalWall[horzWall].GetX() ) { facingHorzWallCorner = 1; //Can't move RIGHT } else if (mPlayer->GetX() == mHorizontalWall[horzWall].GetX()) { facingHorzWallCorner = 2; //Can't move LEFT } } aie::Input* input = aie::Input::getInstance(); if (input->isKeyDown(aie::INPUT_KEY_W) && facingHorzWall != 3 && facingVertWallCorner != 3) mPlayer->ChangePos(moveUp); if (input->isKeyDown(aie::INPUT_KEY_S) && facingHorzWall != 4 && facingVertWallCorner != 4) mPlayer->ChangePos(moveDown); if (input->isKeyDown(aie::INPUT_KEY_A) && facingVertWall != 2 && facingHorzWallCorner != 2) mPlayer->ChangePos(moveLeft); if (input->isKeyDown(aie::INPUT_KEY_D) && facingVertWall != 1 && facingHorzWallCorner != 1) mPlayer->ChangePos(moveRight); // exit the application if (input->isKeyDown(aie::INPUT_KEY_ESCAPE)) quit();``````

Last edited on
You may want to use AABB (axis aligned bounding boxes) and have a general collision function. Something like :
 ``1234`` ``````max(x1) > min(x2) && max(x2) > min(x1) && max(y1) > min(y2) && max(y1) > min(y2)``````

Last edited on
A wall is a wall. When you approach a "vertical" wall from the bottom, it looks like a very narrow, very deep "horizontal" wall. So, all walls must be considered both horizontal and vertical.
Topic archived. No new replies allowed.