View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# My idea for a 2d collision algorithm.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1black_darkness  Members

Posted 16 December 2012 - 12:17 AM

Hello. Feedback appreciated.

So all my algorithm will need are

2 integers
• currentx
• currenty

1 2d matrix of bools
• 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.

Edited by black_darkness, 16 December 2012 - 12:19 AM.

### #2C0lumbo  Members

Posted 16 December 2012 - 12:25 AM

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, 16 December 2012 - 12:26 AM.

### #3black_darkness  Members

Posted 16 December 2012 - 12:41 AM

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.

### #4C0lumbo  Members

Posted 16 December 2012 - 02:29 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.