Thread: Check collision (REAL THREAD)

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

    Check collision (REAL THREAD)

    Hi peeps!

    Got a poblem here. Can't make my check collision function to work. Not much to say is there? Oh right, when collision happens, i want to move the square somewhere on the surface with a specific height. Got it? Good here's the code:

    Main:
    Code:
        //The square's coordinates
        int sX = 400;
        int sY = 300;
    
        //Check collision and move the square
        check_coll(sX, sY, x, y);
    Check collision:
    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;
        
        //Check if you hit the square
        int n  = sXl;
        if (sXl == pXr || sXr == pXl) 
        {
                //New position for the square
                n = GetRand(0, 640);
                
                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 );
    }
    Random number:
    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);
    }
    I think this is all code that is necessary. If you need more code, tell me!

    Problems:
    1. When if (sXl == pXr || sXr == pXl) gets true, the square switches position instead of staying. So the random function goes weird. It should only get 1 number but it don't.
    2. When if (sXl == pXr || sXr == pXl) has stopped being true, the square goes back to the start position of the square.

    How do i solve theese problems?

    REGARDS!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You will have to make sX and sY reference parameters to do all that inside the check_coll() function.
    Kurt

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    i saw that check_col function returns an int value, but it didnt
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Should it not be && instead of || ?

    Anyway, here is almost the same thing I did a while ago. Its not the best way to do it, but it works
    Code:
    void ball::update()
    { 
         ....
         
         // Collsion against player1 
         if ( (posX > player1.getPosX()) && ((posY < (player1.getPosY() + player1.getSizeY())) && (posY > (player1.getPosY() - player1.getSizeY()))) )
         {
                  // Add some acceleration
                  velX = (velX *-1) * 1.1;
                  velY = ((velY *1) * 1.1);
         }
         
         // Collsion against player2 
         if ( (posX < player2.getPosX()) && ((posY < (player2.getPosY() + player2.getSizeY())) && (posY > (player2.getPosY() - player2.getSizeY()))) )
         {
                  velX = (velX *-1) * 1.1;
                  velY = ((velY *1) * 1.1);
         }
         
            // Collision against roof
            if (posY > 1){
               velY = velY * -1;}
            // Collision against floor
            if (posY < -1){
               velY = velY * -1;}
    
         ....
    }
    Last edited by h3ro; 05-14-2007 at 09:32 AM. Reason: Removed some code

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    ZuK: How do you mean?
    Like this:
    Code:
        //Check if you hit the square
        int &n;
        n = sXl;
        if (sXl == pXr || sXr == pXl) 
        {
                //New position for the square
                n = GetRand(0, 640);
                
                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 );
    Hussain Hani: Fixed

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Changed some of my code...

    Code:
        //Check if you hit the square
        int n = sXl;
        if ((pXr >= sXl && pXr < sXr) || (pXl <= sXr && pXl > sXl) || (sXl >= pXl && sXl <= pXr) || (sXr <= pXr && sXr >= pXl)) 
        {
                //New position for the square
                n = GetRand(0, 640);
                
                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 );
    }
    Same problem!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So why not try debugging it then?

    Put a breakpoint on the if() statement, then examine the variables.

    Decide whether the if() should be true or false based on those values, then single-step the code.

    If at any time the code does something different to what you expect, then you've found the bug.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Livijn View Post
    ZuK: How do you mean?
    You said that on collision you want to move the square away a random distance. If you do that then the calling function has to know about that, otherwise when you call check_coll() the next time the square will move back to the original position. That is why I said that you have to make the position of the square a reference parameter to be able to change the position in the calling function.
    Kurt

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Yeah but how do i do that?

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Quote Originally Posted by Salem View Post
    So why not try debugging it then?

    Put a breakpoint on the if() statement, then examine the variables.

    Decide whether the if() should be true or false based on those values, then single-step the code.

    If at any time the code does something different to what you expect, then you've found the bug.
    There's no code error. I just don't know how to do this...

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Then read your compiler documentation and experiment with very simple programming scenarios. Familiarity, if not proficiency, with your debugger/tools in general is pretty much a fundamental necessity.

    As a quick test you can always try putting in some code to write variable values to file or otherwise display them.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I'm not good at english. What do you mean?

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    std::cout "This is a debug statement showing that variable whatever is: " << whatever << std::endl;
    Add these kind of lines in your code to view the status of variables.

    The same idea can be applied to checking where your program branches.

    If you don't like this approach, figure out how to use a debugger.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > There's no code error. I just don't know how to do this...
    So what's the question, if it already works properly?
    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.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Im responding to your PM here.
    What is the problem with the code you have? Is there something wrong with the collision detection itself or is it only the response that causes problems? Did you look at the code I posted before?

    This might just be me, but I find your code hard to read with those variable name.

    good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  2. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  4. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  5. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM