# checking for tile collisions - teleportation?

• 10-27-2011
Gamerz
checking for tile collisions - teleportation?
hi, i have written some code for my game's tile-player collision, but i sometimes get teleported when touching a side of the tile. i don't know if i am going to the right direction here, although the collision works... somewhat. any help on how to make a fully working collision would be much appreciated.
Code:

```void collides() {   top = (player->y() - 0) / 32;   bottom = (player->y() + player->height() + 32) / 32;   left = (player->x() - 0) / 32;   right = (player->x() + player->width() + 32) / 32;   for (int my = top; my < bottom; my++) {     for (int mx = left; mx < right; mx++) {       number = tile[my][mx];       if (number == 1) {         if (player->x() <= mx * 32 + 32) {           xvelocity = 0;         }         else if (player->y() + player->height() >= my * 32) {           yvelocity = 0;         }         else if (player->x() + player->width() >= mx * 32) {           xvelocity = 0;         }         else if (player->y() <= my * 32 + 32) {           yvelocity = 0;         }       }     }```
• 10-27-2011
GReaper
You need to check for both conditions at once, like this:
EDIT: You actually messed it up a little:
Code:

```if (player->x() >= mx * 32 && player->x() <= mx * 32 + 32)     xvelocity = 0; else if (player->x() + player->width() >= mx * 32 && player->x() + player->width() <= mx * 32 + 32)     xvelocity = 0; else if (player->y() >= my * 32 && player->y() <= my * 32 + 32)     yvelocity = 0; else if (player->y() + player->height() >= my * 32 && player->y() + player->height() <= my * 32 + 32)     yvelocity = 0;```
Apart from that, I don't know what may be the problem.