My idea for a 2d collision algorithm.

Started by
2 comments, last by C0lumbo 11 years, 4 months ago
Hello. Feedback appreciated.

So all my algorithm will need are

2 integers

  1. currentx
  2. currenty


1 2d matrix of bools

  1. index[x][y]

So for example I will create the matrix like this
Border is False
5x3 rectangle impassable in center


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
};


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


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]


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

So 3 inital values are as follows

  • curx
  • cury
  • index

Now here is my algorithm to test if square moving to is passable.

I will write my algorithm in C++ notation.

//assuming you are going up.
//translates well into all 4 directions.

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




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.
Advertisement
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.

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.


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.

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.


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.

This topic is closed to new replies.

Advertisement