checking for tile collisions - teleportation?

This is a discussion on checking for tile collisions - teleportation? within the Game Programming forums, part of the General Programming Boards category; hi, i have written some code for my game's tile-player collision, but i sometimes get teleported when touching a side ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    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;
            }
    
          }
    
        }
    Last edited by Salem; 10-27-2011 at 08:28 AM. Reason: remove pointless tags

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    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.
    Last edited by GReaper; 10-27-2011 at 09:16 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum teleportation across 10 miles
    By VirtualAce in forum General Discussions
    Replies: 36
    Last Post: 05-23-2010, 09:49 AM
  2. clear collisions
    By Benzakhar in forum Game Programming
    Replies: 4
    Last Post: 01-10-2004, 05:22 AM
  3. tile map bounds checking
    By Natase in forum Game Programming
    Replies: 1
    Last Post: 10-08-2003, 11:00 AM
  4. Teleportation
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 02-11-2003, 09:40 AM
  5. Help with collisions.
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 06-25-2002, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21