Results 1 to 10 of 10

Thread: Collision Detection

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Collision Detection

    Hi all...

    I'm prorgramming a breakout/paddle game and its comming together nicely, but now I have gotten to the Collision Detection(when the ball hits the blocks)...

    I create objects(BOB's) using my engine which has a lot of wrapper functions, fx Collison_BOBS:
    PHP Code:
    int Collision_BOBS(BOB_PTR bob1,     // Pointer to first BOB
                                  
    BOB_PTR bob2);   // Pointer to second BOB 
    The function returns TRUE is the two bounding rectangles overlap.

    So i have tried to make for loop to check whenever the ball BOB hits the box[28] BOB, but my machine hanged....

    I have also tried to do it manually like this
    PHP Code:
    if(Collision_BOBS(&ball, &box[1])){
          ......

    etc.....

    Hanged again....anyone have any ideas....my main game loop look like this:

    PHP Code:
    int Game_Main(void *parms)
    {

    // check of user is trying to exit
    if (KEY_DOWN(VK_ESCAPE))
        
    PostMessage(main_window_handleWM_DESTROY,0,0);
    if (
    KEY_DOWN(VK_LEFT)) {
        if (
    paddle.>5) {
            
    paddle.paddle.-10;
            
    Move_BOB(&paddle);
        }
    } else if(
    KEY_DOWN(VK_RIGHT)) {
        if(
    paddle.paddle.width  SCREEN_WIDTH) {
            
    paddle.paddle.+10;
            
    Move_BOB(&paddle);
        }
    }

    // start the timing clock
    Start_Clock();

    // clear the drawing surface
    DD_Fill_Surface(lpddsback0);

    Draw_BOB(&paddle,lpddsback);
    Draw_BOB(&ball,lpddsback);
    for (
    int draw 0draw<28draw++)
    Draw_BOB(&box[draw],lpddsback);

    ball.xv xangle;
    ball.yv yangle;
    Move_BOB(&ball);

    //the ball has gone outside the screen
    if(ball.<= ball.>= SCREEN_WIDTH ball.width ){
        
    xangle = -xangle;
    }
    // the ball has gone outside the screen
    if(ball.<= |ball.== SCREEN_HEIGHT ball.height){
        
    yangle = -yangle;
    }
    // the ball has hit the paddle
    if(Collision_BOBS(&ball,&paddle)){
        
    yangle=-yangle// change direction
    }



    // flip the surfaces
    DD_Flip();


    // sync to 30ish fps
    Wait_Clock(30);

    // return success
    return(1);

    // end Game_Main 
    please let me know if you need more source code...
    Thanks in advance!
    Last edited by CyberCarsten; Nov 10th, 2002 at 04:13 PM.
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    box[28] doesnt exist. Remember, in C++, 28 elements go from 0..27.

    Z.

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    oh typo...but it still doesn't do it right, because when I test for collision, the game might be testing block 5 and the has just hit block 20......thats my problem....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You need to step through your code line by line, and find out EXACTLY where it crashes. From what I can see above, the problem may be in your Collision function.

    Z.

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    The Collision function works just fine, but I was trying to step through the code using a loop but it didn't work because when the loop what testing block 5 I might hit block 26 with ball, and it also crashes.....any suggestions...

    The loop I was using:
    PHP Code:
    for(int countercounter<27counter++)
    if(
    Collision_BOBS(&ball,&box[counter])){
    ......
    //Do whatever with the block

    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  6. #6
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    The collision function obviously does not work fine, as it crashes. How are you defining the box variable, also?

    Z.

  7. #7

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Like this:
    BOB box[27];
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  8. #8
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Change it to a 28. You attempt to access 27, which is one beyond the number that you have.

    Z.

  9. #9

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Now I have got the loop running without hanging, but the collision still isn't good...

    PHP Code:
    #define LAST_HIT_RIGHT 1
    #define LAST_HIT_LEFT 2
    #define LAST_HIT_TOP 3
    #define LAST_HIT_BOTTOM 4

    int lasthit
    PHP Code:
    for(int counter 0counter<28counter++)
    if(
    Collision_BOBS(&ball,&box[counter])){
        if(
    lasthit == LAST_HIT_LEFT){
            if(
    xangle <0){
                
    xangle xangle;
            }
            if(
    xangle >){
                
    xangle = -xangle;
            }}
        if(
    lasthit == LAST_HIT_RIGHT){

        if(
    xangle <0){
                
    xangle xangle;
            }
            if(
    xangle 0){
                
    xangle = -xangle;
            }}

        if(
    lasthit == LAST_HIT_TOP){
            if(
    yangle 0){
                
    yangle yangle;
            }
            if(
    yangle 0){
                
    yangle = -yangle;
            }}
        if(
    lasthit == LAST_HIT_BOTTOM){
            if(
    yangle 0){
                
    yangle yangle;
            }
            if(
    yangle 0){
                
    yangle = -yangle;
            }}

    Can you see what I am doing wrong?
    The defines is to detect if the ball last hit the left or right side, the top or the paddle
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  10. #10

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    If it helps, here is the complete source code
    Attached Files Attached Files
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width