Jump to content
  • Advertisement
Sign in to follow this  
Mathius20

Collison Detection

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Since

Column 1 Column 2
| |
| |
| |
| |
| |
| |
| |




Row 1 --------------------



Row 2 --------------------


Quote:
Original post by Mathius20
int 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!