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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 :
1
2
3
4
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.