Followers 0

# My idea for a 2d collision algorithm.

## 3 posts in this topic

Hello. Feedback appreciated.

So all my algorithm will need are

[u]2 integers[/u][list=1]
[*]currentx
[*]currenty
[/list]

[u]1 2d matrix of bools[/u][list=1]
[*]index[x][y]
[/list]
So for example I will create the matrix like this
Border is False
5x3 rectangle impassable in center

[CODE]
bool index[11][11] =
{
{false,false,false,false,false,false,false,false,false,false,false }, // row 0
{false,true,true,true,true,true,true,true,true,true,false }, // row 1
{false,true,true,true,true,true,true,true,true,true,false }, // row 2
{false,true,true,true,true,true,true,true,true,true,false }, // row 3
{false,true,true,false,false,false,false,false,true,true,false }, // row 4
{false,true,true,false,false,false,false,false,true,true,false }, // row 5
{false,true,true,false,false,false,false,false,true,true,false }, // row 6
{false,true,true,true,true,true,true,true,true,true,false }, // row 7
{false,true,true,true,true,true,true,true,true,true,false }, // row 8
{false,true,true,true,true,true,true,true,true,true,false }, // row 9
{false,false,false,false,false,false,false,false,false,false,false }, // row 0
};
[/CODE]

Here is a graph of what it will look like
X = boolean false;
O = boolean true;

[CODE]
XXXXXXXXXXX //row 0
XOOOOOOOOOX //row 1
XOOOOOOOOOX //row 2
XOOOOOOOOOX //row 3
XOOXXXXXOOX //row 4
XOOXXXXXOOX //row 5
XOOXXXXXOOX //row 6
XOOOOOOOOOX //row 7
XOOOOOOOOOX //row 8
XOOOOOOOOOX //row 9
XXXXXXXXXXX //row 10[/font]
[/CODE]

So now all we need is the initial position.
int curx = 1;
int cury = 1;

So 3 inital values are as follows[list]
[*]curx
[*]cury
[*]index
[/list]
Now here is my algorithm to test if square moving to is passable.

I will write my algorithm in C++ notation.
[CODE]
//assuming you are going up.
//translates well into all 4 directions.

if(index[cury-1][curx]==true) {
cury-=1;
}
else {
//collision sound
}

[/CODE]

So when I implement this I think I will implement it as a function. I may create a class for map so it will be easy to create a rectangular grid. Edited by black_darkness
0

##### Share on other sites
That'll work. For the sake of your sanity, if you plan on doing large maps, then it might be worth plugging in code so you can load in your index array from a .bmp or something. Failing that, at the very least you could create your map from a string, so you could use the XOOOOXXOOO style notation. Something like:

[source lang="cpp"]const char *pGameMap[] =
{
"XXXXXOOOOOXXXXX",
"XXXXOOOOOOOXXXX",
};[/source]

You could convert that into a bool array, or just do your tests directly on the string seeing as a char is no bigger than a bool. Edited by C0lumbo
1

##### Share on other sites
[quote name='C0lumbo' timestamp='1355639146' post='5011184']
That'll work. For the sake of your sanity, if you plan on doing large maps, then it might be worth plugging in code so you can load in your index array from a .bmp or something. Failing that, at the very least you could create your map from a string, so you could use the XOOOOXXOOO style notation. Something like:

[source lang="cpp"]const char *pGameMap[] =
{
"XXXXXOOOOOXXXXX",
"XXXXOOOOOOOXXXX",
};[/source]

You could convert that into a bool array, or just do your tests directly on the string seeing as a char is no bigger than a bool.
[/quote]

I still get confused by pointers.

Does the source code snippet create a constant of datatype char* and name it pGameMap[]? if so how can I assign a string to a char* type? That is overly complex for me to conceptualize without a little breakdown.
0

##### Share on other sites
[quote name='black_darkness' timestamp='1355640107' post='5011189']
I still get confused by pointers.

Does the source code snippet create a constant of datatype char* and name it pGameMap[]? if so how can I assign a string to a char* type? That is overly complex for me to conceptualize without a little breakdown.
[/quote]

It's a little complicated to explain exactly what that code is doing. Essentially, each line of "XXXOOOXXX" becomes a NULL terminated string and pGameMap is an array of pointers that point to those strings, I used [] instead of specifying a size of the array so that the compiler can infer the number of strings by the number of entries. C Strings are simply an array of char (8 bit numbers) where a value of 0 indicates the end.

In terms of you using it, your sample code would become:

[source lang="cpp"]//assuming you are going up.
//translates well into all 4 directions.

if(pGameMap[cury-1][curx]=='O') {
cury-=1;
}
else {
//collision sound
}
[/source]
Other things to watch out for are that you would not be allowed to modify pGameMap at runtime which might be an issue if you want a dynamic world (you could copy the data from pGameMap and modify that though). Also I'd recommend throwing a few asserts into your code to make sure all the strings are the same length and that there's a safe border of X around the edge of the map.
1

## Create an account

Register a new account