rougelike game dev help needed with wall collison detection!!!

Hello I am new to these forums and need help, I walking through walls. I found its the if statements but I cant find the bug. (keep in mind still a huge work in progress.)

walls are nines and to move hit a w a s d key then enter

p.s. I am 12 years old so please don't judge me for my mistakes thanks!

os: windows 8

compiler: Microsoft visual c++ 2010 express

here is my code:

#include <iostream>
#include <string>
using namespace std;
int main()
{

int wc = 9;

//command var.

string command ;

//end

//worldsize

int world[6][4];

//end

//spawn pos.

int x = 3;
int y = 2;

//end

start:

//map

world[0][0] = 9;
world[1][0] = 9;
world[2][0] = 9;
world[3][0] = 9;
world[4][0] = 9;
world[5][0] = 9;
world[6][0] = 9;
world[0][1] = 9;
world[1][1] = 0;
world[2][1] = 0;
world[3][1] = 0;
world[4][1] = 0;
world[5][1] = 0;
world[6][1] = 9;
world[0][2] = 9;
world[1][2] = 0;
world[2][2] = 0;
world[3][2] = 0;
world[4][2] = 0;
world[5][2] = 0;
world[6][2] = 9;
world[0][3] = 9;
world[1][3] = 0;
world[2][3] = 0;
world[3][3] = 0;
world[4][3] = 0;
world[5][3] = 0;
world[6][3] = 9;
world[0][4] = 9;
world[1][4] = 9;
world[2][4] = 9;
world[3][4] = 9;
world[4][4] = 9;
world[5][4] = 9;
world[6][4] = 9;

//end

//wall collide script

if(wc = world[x][y])
{
if( command == "w") y = y - 1;
if( command == "s") y = y + 1;
if( command == "a") x = x + 1;
if( command == "d") x = x - 1;

}

//end

//avatar

world[x][y] = 1;

//end

//graphics script

cout << world[x+2][y+1];
cout << world[x-1][y+1];
cout << world[x][y+1];
cout << world[x-1][y+1];
cout << world[x-2][y+1];
cout << endl;
cout << world[x+2][y];
cout << world[x+1][y];
cout << world[x][y];
cout << world[x-1][y];
cout << world[x-2][y];
cout << endl;
cout << world[x+2][y-1];
cout << world[x+1][y-1];
cout << world[x][y-1];
cout << world[x-1][y-1];
cout << world[x-2][y-1];
cout << endl;

//end

//movement script

cin >> command;
if (command == "w") y = y + 1;
if (command == "s") y = y - 1;
if (command == "d") x = x - 1;
if (command == "a") x = x + 1;

//end

goto start;

return 0;

}
Last edited on
Congratz on trying to learn C++ :D. Just to give you a helping hand there is a button when your writting your messages on here So that when you add code it looks all fancy and neat.


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

here is my code:

#include <iostream>
#include <string>

using namespace std;


int main()
{

     int wc = 9;

     int x1;
     int y1;

     //command var.

     string command ;

     //end

    //worldsize

     int world[6][4];

     //end

     //spawn pos.

     int x = 3;
     int y = 2;

     //end

     start:

     //map

     world[0][0] = 9;
     world[1][0] = 9;
     world[2][0] = 9;
     world[3][0] = 9; 
     world[4][0] = 9;
     world[5][0] = 9;
     world[6][0] = 9;
     world[0][1] = 9;
     world[1][1] = 0;
     world[2][1] = 0;
     world[3][1] = 0;
     world[4][1] = 0;
     world[5][1] = 0;
     world[6][1] = 9;
     world[0][2] = 9;
     world[1][2] = 0;
     world[2][2] = 0;
     world[3][2] = 0;
     world[4][2] = 0;
     world[5][2] = 0;
     world[6][2] = 9;
     world[0][3] = 9;
     world[1][3] = 0;
     world[2][3] = 0;
     world[3][3] = 0;
     world[4][3] = 0;
     world[5][3] = 0;
     world[6][3] = 9;
     world[0][4] = 9;
     world[1][4] = 9;
     world[2][4] = 9;
     world[3][4] = 9;
     world[4][4] = 9;
     world[5][4] = 9;
     world[6][4] = 9;

     //end

     //wall collide script

     if(wc = world[x][y])
     {
          if( command == "w") y = y + 1;
          if( command == "s") y = y - 1;
          if( command == "a") x = x - 1;
          if( command == "d") x = x + 1;

     }

     //end

     //avatar	

     world[x][y] = 1;

     //end

     //graphics script

     cout << world[x+2][y+1];
     cout << world[x-1][y+1];
     cout << world[x][y+1];
     cout << world[x-1][y+1];
     cout << world[x-2][y+1];
     cout << endl;
     cout << world[x+2][y];
     cout << world[x+1][y];
     cout << world[x][y];
     cout << world[x-1][y];
     cout << world[x-2][y];
     cout << endl;
     cout << world[x+2][y-1];
     cout << world[x+1][y-1];
     cout << world[x][y-1];
     cout << world[x-1][y-1];
     cout << world[x-2][y-1];
     cout << endl;

     //end

     //movement script

     cin >> command;
     if (command == "w") y = y + 1;
     if (command == "s") y = y - 1;
     if (command == "d") x = x - 1;
     if (command == "a") x = x + 1;

     //end

     goto start;

     return 0;

}



I just looked at it and at least from the quick look I got. your wall collision and movement O_o look to be the same. You tell it to do the same thing
Last edited on
still wont work!?!?!?
Last edited on
Well, there's no collision detection at all, so not really all that surprising that you're walking through walls. You should check if the position you're moving to is a wall or not before you actually move. Which means that you should simply check if world[x][y+1] == 9 if you're moving up. And like Stormhawk44 said your collision and movement are the same, so just get rid of the collision one for now.
actually I changed that... I think
tried it still wont work!?!?!?
What exactly did you do? Post the part of your code that you changed
is that bad
No?
I just won't be able to help if I don't know what you did
well yes I did that
well I stopped though
if (command == "w" && world[x][y+1] != 9) y = y + 1;
Is what you did similar to this?
kinda here it is
//wall collide script

if(world[x][y+1] == 9)
{
if( command == "w") y = y - 1;

}

if(world[x][y-1] == 9)
{
if( command == "s") y = y + 1;

}

if(world[x][x-1] == 9)
{
if( command == "a") x = x + 1;

}

if(world[x][x+1] == 9)
{
if( command == "d") x = x - 1;

}
Change it to
1
2
3
4
if (command == "w" && world[x][y+1] != 9) y = y + 1;
if (command == "s" && world[x][y-1] != 9) y = y - 1;
if (command == "d" && world[x-1][y] != 9) x = x - 1;
if (command == "a" && world[x+1][y] != 9) x = x + 1;

and it should work just fine
Last edited on
still wont work???
Last edited on
Here's the entire code

Note that I removed the wall collision check scrip part of the code, before you would move two steps every time.

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
#include <iostream>
#include <string>

using namespace std;


int main()
{

     int wc = 9;

     int x1;
     int y1;

     //command var.

     string command ;

     //end

    //worldsize

     int world[6][4];

     //end

     //spawn pos.

     int x = 3;
     int y = 2;

     //end

     start:

     //map

     world[0][0] = 9;
     world[1][0] = 9;
     world[2][0] = 9;
     world[3][0] = 9; 
     world[4][0] = 9;
     world[5][0] = 9;
     world[6][0] = 9;
     world[0][1] = 9;
     world[1][1] = 0;
     world[2][1] = 0;
     world[3][1] = 0;
     world[4][1] = 0;
     world[5][1] = 0;
     world[6][1] = 9;
     world[0][2] = 9;
     world[1][2] = 0;
     world[2][2] = 0;
     world[3][2] = 0;
     world[4][2] = 0;
     world[5][2] = 0;
     world[6][2] = 9;
     world[0][3] = 9;
     world[1][3] = 0;
     world[2][3] = 0;
     world[3][3] = 0;
     world[4][3] = 0;
     world[5][3] = 0;
     world[6][3] = 9;
     world[0][4] = 9;
     world[1][4] = 9;
     world[2][4] = 9;
     world[3][4] = 9;
     world[4][4] = 9;
     world[5][4] = 9;
     world[6][4] = 9;

     //end



     //end

     //avatar	

     world[x][y] = 1;

     //end

     //graphics script

     cout << world[x+2][y+1];
     cout << world[x-1][y+1];
     cout << world[x][y+1];
     cout << world[x-1][y+1];
     cout << world[x-2][y+1];
     cout << endl;
     cout << world[x+2][y];
     cout << world[x+1][y];
     cout << world[x][y];
     cout << world[x-1][y];
     cout << world[x-2][y];
     cout << endl;
     cout << world[x+2][y-1];
     cout << world[x+1][y-1];
     cout << world[x][y-1];
     cout << world[x-1][y-1];
     cout << world[x-2][y-1];
     cout << endl;

     //end

     //movement script

     cin >> command;
     if (command == "w" && world[x][y+1] != 9) y = y + 1;
     if (command == "s" && world[x][y-1] != 9) y = y - 1;
     if (command == "d" && world[x-1][y] != 9) x = x - 1;
     if (command == "a" && world[x+1][y] != 9) x = x + 1;

     //end

     goto start;

     return 0;

}
Thanks!!! this is great just great thanks a million times thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

again TTTTTTTTTTTTTTTTTHHHHHHHHHHHHHHHHHHHHAAAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNKKKKKKKKKKKKKKKKKKKKSSSSSSSSSSSSSSSSSSSSSSSSS!!!!!!!!!!!!!!!!!!!!!!!!!!
Right then, if you feel like you need to know more of the language you should check out the tutorial on the site, it should put you on the right track
http://cplusplus.com/doc/tutorial/
You probably have a decent understanding of the basics and should be able to jump into the control structures and functions
Last edited on
haha that's where I learned!!!
Topic archived. No new replies allowed.