Check collision

This is a discussion on Check collision within the C++ Programming forums, part of the General Programming Boards category; Hi! You guys won't get rid of me! I have some work to do with my game before it's done. ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Check collision

    Hi!

    You guys won't get rid of me! I have some work to do with my game before it's done. I need to check if my piece hits the square. I don't want to mess around with anyone else's code. It will just become messy and i won't understand it.

    I tested to do like this, just for a test, this is not all checks i'm gonna do, but it should work if my piece comes from the left and hits the square.

    Code:
    //Check if there is a collision
    int check_coll( int sX, int sY, int pX, int pY )
    {
        //Square's position
        int sXl = sX;
        int sXr = sX + 29;
        int sYl = sY;
        int sYr = sY + 29;
        
        //Piece's position
        int pXl = pX;
        int pXr = pX + 75;
        int pYl = pY;
        int pYr = pY + 148;
        
        if (sXl == pXr) 
        {
                //Apply the square
                apply_surface( 400, 100, square, screen );
        }
                
        
    }
    Why aint this working?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    With the sizes of the squares and pieces being different, wouldn't it be better to test if any part of the piece was in any part of the square, rather than comparing the left edge of the square with the right edge of the piece for equality? Is it possible for the piece to move past the edge of the square?

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    How would i do that? To test if any part of the piece was in any part of the square? :s Confused

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I don't know what the best way would be, but you can just figure it out. For example, if the right edge of the piece is between the left and right edge of the square, and the bottom edge of the piece is between the top and bottom edges of the square, then you know it overlaps. You'd also have to check the other corners. If the piece is bigger than the square, you might have to check more positions (like perhaps the middle of the piece) in case it completely covers the square.

    By the way, I don't know if this is your problem, I'm just guessing from your posted code. You have to figure out if this reason makes sense.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Mmm, another collision checking post...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Actually, mine works. BUT i want it to erase the old sqaure forever. How?
    I do like this and it erases every time the sXl is 400 and pXr is 400.

    My function:
    Code:
    //Check if there is a collision
    int check_coll( int sX, int sY, int pX, int pY )
    {
        //Square's position
        int sXl = sX;
        int sXr = sX + 29;
        int sYl = sY;
        int sYr = sY + 29;
        
        //Piece's position
        int pXl = pX;
        int pXr = pX + 75;
        int pYl = pY;
        int pYr = pY + 148;
        
        if (sXl == 400) 
        {
        if (sXl == pXr) 
        {
                SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
                apply_surface( 0, 0, background, screen );
                piece = load_image( "piece.png" ); 
                apply_surface( pXl, pYl, piece, screen ); 
                
                sXl = 100;
                //Apply the square
                apply_surface( sXl, sYl, square, screen );
        }
        if (sXl == pXr)
        {
                sXl = 100;
                //Apply the square
                apply_surface( sXl, sYl, square, screen );
        }
        }
        
        if (sXl == 100) 
        {
        if (sXl == pXr) 
        {
                sXl = 340;
                //Apply the square
                apply_surface( sXl, sYl, square, screen );
        }
        if (sXl == pXr)
        {
                sXl = 340;
                //Apply the square
                apply_surface( sXl, sYl, square, screen );
        }
        }
            
                
        
    }
    Soe code in main:
    Code:
                //Apply the square
                apply_surface( sX, sY, square, screen );
                
                //Check collision and move the square
                check_coll(sX, sY, x, y);

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Bump

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Will you stop bumping your threads!
    Any more will result in thread closure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    But noone cares... Yeah yeah, i'll stop! Please help me!

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    88
    > But noone cares... Yeah yeah, i'll stop! Please help me!
    Actually, I for one find it annoying that you bump your threads expecting us to spoonfeed you a solution to a problem that isn't really that challenging and that you could probably figure out if you read your own code and thought about it for a few minutes.

    >Actually, mine works. BUT i want it to erase the old sqaure forever. How?
    >I do like this and it erases every time the sXl is 400 and pXr is 400.

    Here's a clue, it probably has to do with where you put these two lines:
    Code:
    SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
    apply_surface( 0, 0, background, screen );

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Well, that wasn't my question actually. If it was, i would have solved it for years ago. :P No, my problem is almost the same as i said, BUT it wont disapeare. If my position is the same as the squares, it works but when i move from the old square's postion, the new square shows up and the old disapeares.

    And also, now i have added a random function. It works, it's just that the postion of the square moves constantly. I wan't it to stop after 1 random choice.

    Code:
    //Random number
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
      
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    
    
    //Check if there is a collision
    int check_coll( int sX, int sY, int pX, int pY )
    {
        //Square's position
        int sXl = sX;
        int sXr = sX + 29;
        int sYl = sY;
        int sYr = sY + 29;
        
        //Piece's position
        int pXl = pX;
        int pXr = pX + 75;
        int pYl = pY;
        int pYr = pY + 148;
        
        //New position for the square
        int n;
        n = GetRand(0, 640);
    
    
        if (sXl == pXr || sXr == pXl) 
        {
                SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
                apply_surface( 0, 0, background, screen );
                piece = load_image( "piece.png" ); 
                apply_surface( pXl, pYl, piece, screen ); 
                
                //Apply the square
                apply_surface( n, sYl, square, screen );
        }
    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Deleting your reply and then posting it again is still bumping.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Okey! Sorry, didn't know. I need help please!

  14. #14
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Livijn, if I where to bump like you do, Salem or kermi3 would probably ban me for a week!

  15. #15
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Why?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  3. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  4. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  5. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 05:17 PM

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