Public Group

# Collison Detection

This topic is 4502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I was having problems wityh my collision detection with my tiles and i was wondering if anyone could help. int TileRow=X/32; int TileColumn=Y/32; if(m_CharacterRect.bottom+Y > Tiles[TileColumn][TileRow].TileRect.top && Tiles[TileColumn][TileRow].bWalkable==false) return false; if(m_CharacterRect.top-Y < Tiles[TileColumn][TileRow].TileRect.bottom && Tiles[TileColumn][TileRow].bWalkable==false) return false; if(m_CharacterRect.right+X > Tiles[TileColumn][TileRow].TileRect.left && Tiles[TileColumn][TileRow].bWalkable==false) return false; if(m_CharacterRect.left-X < Tiles[TileColumn][TileRow].TileRect.right && Tiles[TileColumn][TileRow].bWalkable==false) return false; is my collion and Tiles[row][column].TileRect.top=column*32; Tiles[row][column].TileRect.left=row*32; Tiles[row][column].TileRect.bottom=Tiles[row][column].TileRect.top+64; Tiles[row][column].TileRect.right=Tiles[row][column].TileRect.left+32; is where i set up the tile rect in my loadmap function no matter what I cant seem to get the bottom and right collison to work. the top and left work fine (except when the top left corner is on a walkable tile, and i move up, it will go through unwalable tiles) Thanks for any help

##### Share on other sites
Since
   Column 1      Column 2     |             |     |             |     |             |     |             |     |             |     |             |     |             |Row 1  --------------------Row 2  --------------------

Quote:
 Original post by Mathius20int TileRow=X/32;int TileColumn=Y/32;

should be
int TileRow=Y/32;int TileColumn=X/32;

even if your current code is logically correct, it is very confusing for someone who reads the code (and for yourself also, when you reads someoneelses code [smile]).

Did changing these lines fix your problem?

##### Share on other sites
That actually makes it not work at all, the top left corner works for collision which is why left and top work but the others dont:(

##### Share on other sites
That is very confusing to look at.

##### Share on other sites
You mix up rows and columns.

In the ifs you have Tiles[TileColumn][TileRow] and later Tiles[row][column].

You need to be consequent, so my advice it that you once and for all look through all your code (you probably need to look into code that you didn't post here) and make sure that you use column- and rowvariabels correctly.

If that doesn't solve your problem, post as much relevant code here as possible, and don't forget the [ code][ /code ] tags which make your code more readable. (Look through the Forum FAQ for other useful tags.)

##### Share on other sites
I...didn't even bother trying to figure out the details of your collision since its seems a bit obscure. It looks like you're testing collisions between two rectangles.

Fortunately for you, MSDN already has functions to check if two rects are bounding, or if a point is within a rect~!

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_1mcz.asp

Oh, one other thing: Try to avoid using "Magic Numbers" like 32. What is 32? Is that the number of colums? The width of a rect? who knows! What if this magic number had to be changed? :) how many places in your code rely on this number?

• 10
• 18
• 14
• 18
• 15