collision???

This is a discussion on collision??? within the Game Programming forums, part of the General Programming Boards category; I am not even sure is that is what it really is. I am using allegro and i want to ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    collision???

    I am not even sure is that is what it really is. I am using allegro and i want to make it so certain areas on a back ground bitmap are off limits u can't bring the sprite over top of them. I have no idea how to do this????

    thanx?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    208

    just in case

    what I want is there is a sprite which represents a person. I have a bitmap that I load as the map. I only want the sprite to be able to walk on the road portions of the map.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Preseumeably you have coordinates for your player, that you alter when the player presses the move keys.

    So you can prevent him from moving to a certain area by putting conditional statements in the code that deals with the player input.

    For eample say x and y are the variables holding the player position, and when the up key is pressed you decrease y by 10. If there was a portion of the screen you didnt want him to walk over.

    You would have code that looks something like

    Code:
    if (Up-key)
    {
          y -= 10;
          if (y < 25)
                  y = 25;
    }
    now your player cannot move forward past y=25.

    Now that only works if your map is stationary.

    If you have a scrolling map, the easiest way to do it is through colour coding; you line your road with pixels of a specific colour and then in your movement code you only alter the player coordinates if they wont put the player on the road border colour.

  4. #4
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    If you are using a tile map, then you should do it by what each tile equals. Your map should be loaded into an array (most likely you would load it from a text file). Once you do that there is going to be a value for each part of the array. Ok lets say you want you map to be 20x20, so you make a text file that is 20 characters wide and 20 characters long. For simplicity sake lets say that you only want grass, trees, and bushes as apart of your map (and of course your player). So you make the map look something like this: [btw] this is going to be a lot more info than you are looking for, but it is very useful info[/btw]

    Code:
    T T T T T T T T T T T G G G G B T T T
    T T T T T T T T T T T G G G G B T T T
    T T T T T T T T T T G G T G G B T T T
    T T T T T T T T T T T G G G G B T T T
    T T T T T T T T T T T G G G G B T T T
    
    except it's going to be four times longer
    now this is how you would load the map:

    Code:
    #define MAX_X 20
    #define MAX_Y 20
    #define GRASS 0
    #define TREE 1
    #define BUSH 2
    #define PLAYER 3
    #define VALID 1 /*since this number is one that means that the player can only move onto grass tiles*/
    
    char test[MAX_X][MAX_Y]; /*you only need this array if you use letters as your characters instead of numbers in the map.txt file*/
    int grid{MAX_X][MAX_Y];
    
    void load_map()
     {
       ifstream filemap("map.txt");
        for(int b = 0; b < MAX_Y; b++)
         {
         for(int z = 0; z < MAX_X; z++)
          {
           filemap >> test[z][b];
           if(test[z][b] == 'G')
            {
            grid[z][b] = GRASS;
            }
           else if(test[z][b] == 'T')
            {
            grid[z][b] = TREE;
            }
           else if(test[z][b] == 'B')
            {
            grid[z][b] = BUSH;
            }
          }
         }
       filemap.close();
    
      grid[5][5] = PLAYER:  /*this is just to set that part of the map as the player but you should find the player's position by loading it from the map.txt file but you should be able to figure all that out*/
       
      }
    Now I'm going to show you how you would do the collision detection

    Code:
    struct CHARACTER
     {
     int pos_x, pos_y;
     }player;
    
    if(key[KEY_UP] && player.pos_y > 0 && grid[player.pos_x][player.pos_y-1] < VALID)  /*notice I did -1 on the y-coord.  that's needed because you are going to move 1 up*/
     {
      grid[player.pos_x][player.pos_y] = GRASS;
      player.pos_y--;
      grid[player.pos_x][player.pos_y] = PLAYER:
     }
    else if(key[KEY_RIGHT] && player.pos_x < MAX_X - 1 && grid[player.pos_x+1][player.pos_y] < VALID)
     {
      grid[player.pos_x][player.pos_y] = GRASS;
      player.pos_x++;
      grid[player.pos_x][player.pos_y] = PLAYER:
     }
    Well, that's about all you have to do if you are going to use a tile map, which I recommend for most games. In fact, there aren't too many 2D games that you wouldn't want to use a tile map.

    Again, btw, this did show you how to do side scrolling because that's just a little bit more complex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some collision handling fun
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 04-13-2008, 08:45 AM
  2. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  3. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM

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