Allegro collsion detection

This is a discussion on Allegro collsion detection within the C++ Programming forums, part of the General Programming Boards category; Howdy, I'm new to Allegro, but I thought I knew enough to make a Pong game. From what I can ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    17

    Allegro collsion detection

    Howdy,
    I'm new to Allegro, but I thought I knew enough to make a Pong game. From what I can see, I've made a perfectly fine one, that's very basic of course. The thing is, my collision detection system doesn't seem to work right...

    Code:
    #include <allegro.h>
    #include <iostream>
    #include <string>
    #include <time.h>
    
    using namespace std;
    
    void Check_Null(BITMAP* Picture, string Type, string Error);
    int Get_Rand(void);
    
    int main(void){
        cout << "This is a Pong game by Lorgon Jortle. Mar 31, 2009.\n";
        cin.get();
    
        allegro_init();
        install_keyboard();
    	install_timer();
    	
    	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    
        /**Declare variables**/
    
        //Bitmaps
        BITMAP* buffer = NULL;
        BITMAP* player1 = NULL;
        BITMAP* player2 = NULL;
        BITMAP* ball = NULL;
    
        //Integers
        int player1_x, player1_y, player2_x, player2_y;
    	int player1_score, player2_score;
        int ball_x, ball_y;
        int ball_speed, ball_direction; //Ball direction 1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
    
        int player1_bb_bottom, player1_bb_right, player1_bb_top, player1_bb_left;
    	int player2_bb_bottom, player2_bb_left, player2_bb_top, player2_bb_right; //The lines on the side of the paddle to check for collisions
    	int ball_bb_top, ball_bb_right, ball_bb_left, ball_bb_bottom;
    
        //Booleans
        bool Collision = false;
    	bool Show_Box = false; //For error testing with collision
    
        /**Assign variables**/
    
        //Bitmaps
        buffer = create_bitmap(640, 480);
        Check_Null(buffer, "buffer", "buffer failed to load.");
    
        player1 = load_bitmap("player1.bmp", NULL);
        Check_Null(player1, "player1.bmp", "player1.bmp failed to load.");
    
        player2 = load_bitmap("player2.bmp", NULL);
        Check_Null(player2, "player2.bmp", "player2.bmp failed to load.");
    
        ball = load_bitmap("ball.bmp", NULL);
        Check_Null(ball, "ball.bmp", "ball.bmp failed to load.");
    	
        //Integers
        player1_x = 0;
        player1_y = 240;
        player2_x = 630;
        player2_y = 240;
    	ball_x = 300;
    	ball_y = 300;
    	
    	player1_score = 0;
    	player2_score = 0;
    	
        player1_bb_bottom = (player1_y + player1->h);
        player1_bb_right = (player1_x + player1->w);
        player1_bb_left = player1_x;
    	player1_bb_top = player1_y;
    	
        player2_bb_bottom = (player2_y + player2->h);
        player2_bb_left = player2_x;
    	player2_bb_top = player2_y;
    	player2_bb_right = (player2_x + player2->w);
    	
    	ball_bb_left = ball_x;
    	ball_bb_bottom = (ball_y + ball->h);
    	ball_bb_top = ball_y;
    	ball_bb_right = (ball_x + ball->w);
    
        ball_speed = 1;
    	ball_direction = Get_Rand();
    
        while(!key[KEY_ESC]){
            //Draw the ball 1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
    		if(ball_direction == 0){
    			ball_x = ball_y = 300;
    		}
    		if(ball_direction == 1){
    			ball_x -= ball_speed;
    		}
    		if(ball_direction == 2){
    			ball_x -= ball_speed;
    			ball_y -= ball_speed;
    		}
    		if(ball_direction == 3){
    			ball_x -= ball_speed;
    			ball_y += ball_speed;
    		}
    		if(ball_direction == 4){
    			ball_x += ball_speed;
    		}
    		if(ball_direction == 5){
    			ball_x += ball_speed;
    			ball_y -= ball_speed;
    		}
    		if(ball_direction == 6){
    			ball_x += ball_speed;
    			ball_y += ball_speed;
    		}
    				
    		draw_sprite(buffer, ball, ball_x, ball_y);
    
    
            //Accept input
            if(key[KEY_UP]){
    			if(player2_y < 0){
    				player2_y = 480;
    			}
                player2_y -= 3;
            }
            if(key[KEY_DOWN]){
    			if(player2_y > 480){
    				player2_y = 0;
    			}
                player2_y += 3;
            }
            if(key[KEY_W]){
    			if(player1_y < 0){
    				player1_y = 480;
    			}
                player1_y -= 3;
            }
            if(key[KEY_S]){
    			if(player1_y > 480){
    				player1_y = 0;
    			}
                player1_y += 3;
            }
    		if(key[KEY_SPACE]){
    			Show_Box = true;
    		}
    		else if (!key[KEY_SPACE]){
    			Show_Box = false;
    		}
    		if(key[KEY_R]){
    			ball_x = ball_y = 300;
    			ball_direction = Get_Rand();
    		}
            /*
    
            LEAVE ROOM FOR FURTHER KEY PRESSES
    
            */
    		
    		//Update bounding boxes
    		player1_bb_bottom = (player1_y + player1->h);
    		player1_bb_right = (player1_x + player1->w);
    	
    		player2_bb_bottom = (player2_y + player2->h);
    		player2_bb_left = player2_x;
    		
    		ball_bb_left = ball_x;
    		ball_bb_bottom = (ball_y + ball->h);
    		ball_bb_top = ball_y;
    		ball_bb_right = (ball_x + ball->w);
    		//Done
    		
    		//Draw players and ball
    	    draw_sprite(buffer, player1, player1_x, player1_y);
            draw_sprite(buffer, player2, player2_x, player2_y);
            draw_sprite(buffer, ball, ball_x, ball_y);
    		//Done
    		
    		if(Show_Box == true){
    			line(buffer, player1_bb_right, player1_y, player1_bb_right, player1_bb_bottom, makecol(255, 0, 0)); //Player1
    			line(buffer, player2_bb_left, player2_y, player2_bb_left, player2_bb_bottom, makecol(255, 0, 0)); //Player2
    			line(buffer, ball_bb_right, ball_y, ball_bb_right, ball_bb_bottom, makecol(255, 0, 0)); //Player1
    			line(buffer, ball_bb_left, ball_y, ball_bb_left, ball_bb_bottom, makecol(255, 0, 0));
    			
    		}
    		
    		/**Collision testing**/
    		Collision = false;
    		if((ball_y - 25) >= (player2_y - 50) && ball_y <= player2_y){
    			if(ball_bb_right >= player2_bb_left){
    				Collision = true;
    			}
    		}
    		else if((ball_y - 25) >= (player1_y - 25) && ball_y <= player1_y){
    			if(ball_x <= player1_bb_right){
    				Collision = true;
    			}
    		}
    		
    		if(Collision == true){
    			if(ball_direction >= 4){
    				ball_direction = 1;
    			}
    			else if(ball_direction <= 3){
    				ball_direction = 4;
    				}
    			}
    		
    		/**Collision with top and bottom**/ //1 = left, 2 = left up, 3 = left down, 4 = right, 5 = right up, 6 = right down
    		if(ball_y >= 430){
    			if(ball_direction == 6){
    				ball_direction = 5;
    			}
    			if(ball_direction == 3){
    				ball_direction = 2;
    			}
    		}
    		if(ball_y <= 0){
    			if(ball_direction == 2){
    				ball_direction == 3;
    			}
    			if(ball_direction == 5){
    				ball_direction = 6;
    			}
    		}
    		
    	/**Check for win**/
    	if(ball_x > 640){
    		player1_score += 1;
    		ball_x = ball_y = 300;
    		ball_direction = Get_Rand();
    	}
    	if(ball_x < 0){
    		player2_score += 1;
    		ball_x = ball_y = 300;
    		ball_direction = Get_Rand();
    	}
    
    	textprintf_ex(buffer, font, 10, 10, makecol(255, 100, 200), -1, "Position: %d, %d", ball_x, ball_y);
    	textprintf_ex(buffer, font, 500, 10, makecol(255, 100, 200), -1, "Direction: %d", ball_direction);
    	textprintf_ex(buffer, font, 10, 470, makecol(255, 100, 200), -1, "Score: %d", player1_score);
    	textprintf_ex(buffer, font, 500, 470, makecol(255, 100, 200), -1, "Score: %d", player2_score);
    
            blit(buffer, screen, 0, 0, 0, 0, 640, 480);
            clear_bitmap(buffer);
        }
    
        destroy_bitmap(buffer);
        destroy_bitmap(player1);
        destroy_bitmap(player2);
        destroy_bitmap(ball);
    
        return 0;
    }
    END_OF_MAIN();
    
    void Check_Null(BITMAP* Picture, string Type, string Error){
        if(Picture == NULL){
            cout << "Error: " << Error << endl;
        }
        else if(Picture != NULL){
            cout << Type << " loaded successfully.\n";
        }
    }
    END_OF_FUNCTION(Check_Null);
    
    int Get_Rand(void){
    	int goodness;
    	srand(time(NULL));
    	goodness = (rand()%6) + 1;
    	return goodness;
    }
    END_OF_FUNCTION(Get_Rand);
    sometimes the ball changes direction like it's supposed to, but often times it doesn't. I've add the text at the top to show the variables when it's moving, and everything is as it should be... Player2 is the same image as player1, just rename it.
    Attached Images Attached Images   

  2. #2
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    I'm willing to bet this would do well for your purposes;

    Line Segment Intersection Algorithm Bryce Boe

    It looks like you'll have to approach the fundamental game design differently for it to be convenient, create some classes to describe line segments, 2d vectors, etc... - that should also keep your code clean and easier to 'read.'

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    if(ball_y <= 0){
    			if(ball_direction == 2){
    				ball_direction == 3;
    			}
    			if(ball_direction == 5){
    				ball_direction = 6;
    			}
    		}
    Perhaps that is the issue? I think that is suppose to be one equal sign.


    By the way, the program crashed everytime I tried to run it. I just noticed the equal sign issue when I compiled (gave me warnings). What are using to compile? I'm using Visual C++ Express right now.

    *Edit- Something else I just noticed:
    Code:
    //Update bounding boxes
    player1_bb_bottom = (player1_y + player1->h);
    player1_bb_right = (player1_x + player1->w);
    	
    player2_bb_bottom = (player2_y + player2->h);
    player2_bb_left = player2_x;
    I'm guessing one of those is wrong. Maybe it should be: player2_bb_left = (player2_x + player2->w);

    *Edit two- Anyone know why the program would run by launching the the executable but not when debugging?
    Last edited by Cpro; 04-06-2009 at 01:37 PM.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM

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