Thread: Problem with my collision detector in allegro

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    Problem with my collision detector in allegro

    so guys here's my code
    Code:
    // HEADER FILES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #include<stdlib.h>
    #include<stdio.h>
    #include<allegro.h> 
    #include<time.h>
    
    
    // TRANSPARENCY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #define TRANSPARENCY makecol(255,0,255)
    
    
    // Structure ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    struct Object{
        int x;
        int y;
        int life;
        int ySpeed;
    };
    Object GameObject[7][15];
    
    
    // FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    volatile long speed_counter = 0;
    void mainMenu(BITMAP *MainPage, BITMAP *buffer, BITMAP *Instructions, BITMAP *Story, BITMAP *Dodge); 
    int oddX(); 
    bool Collision(int object1_X, int object1_Y, int object1_H, int object1_W, int object2_X, int object2_Y, int object2_H, int object2_W);
    
    
    
    
    // SPEED COUNTER INCREMENT (TIME) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    void increment_speed_counter(){
    	speed_counter++; 
    }
    END_OF_FUNCTION(increment_speed_counter); 
    
    
    
    
    
    
    // COLLISION DETECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bool Collision(int object1_X, int object1_Y, int object1_H, int object1_W, int object2_X, int object2_Y, int object2_H, int object2_W) {
    
    
        //Setting up bounding box
        int object1_bb_Left = object1_X;
        int object1_bb_Top = object1_Y;
        int object1_bb_Right = (object1_bb_Left + object1_W);
        int object1_bb_Bottom = (object1_bb_Top + object1_H);
    
    
        int object2_bb_Left = object2_X;
        int object2_bb_Top =  object2_Y;
        int object2_bb_Right = (object2_bb_Left + object2_W);
        int object2_bb_Bottom = (object2_bb_Top + object2_H);
    
    
        bool Collided = true;                  //Assume there is a collision
        if(object1_bb_Bottom < object2_bb_Top){
            Collided = false;               //then no collosion
        }
        else if(object1_bb_Top > object2_bb_Bottom) {
            Collided = false;
        }
        else if(object1_bb_Right < object2_bb_Left) {
            Collided = false;
        }
        else if(object1_bb_Left > object2_bb_Right) {
            Collided = false;
        }
    
    
        return(Collided); //returns a true or false value
    
    
    }
    END_OF_FUNCTION(Collision);
    
    
    
    
    // START MAIN ==============================================================================================
    int main(int argc, char *argv[]) 
    { 
        
        allegro_init();     // Initialize Allegro 
        install_keyboard(); // Initialize keyboard routines    
    	install_timer(); // Initialize the timer routines	
    	LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's
    	LOCK_FUNCTION(increment_speed_counter);//speed.
    	install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
    	install_int_ex(increment_speed_counter, BPS_TO_TIMER(30)); //Set our BPS
        
        // COLOUR DEPTH AND GRAPHICS ---------------------------------------------------------------
        set_color_depth(desktop_color_depth()); // Set the color depth 
        set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0); // Change our graphics mode to 640x480
        set_window_title("Dragon Ball Z Mania");
    	srand(time(0));
    	
        
        // DECLARING VARIABLES ---------------------------------------------------------------------
        bool Playagain = true;
        int i_random;
        int i;
    	int j;
    	int k =0;
    	int Health = 30; // Health
    	int counter = 0;
    	int p;
    	int j_counter;
    	int j_start;
    	int Change = TRUE;
    	int cursor = 0;	
        int gravity_counter = 0;  // For gravity
    	int velocity_h = 0;       // For Different Speeds
    	int out=FALSE;
    	bool hurt = false;
    	
        // DECLARE BITMAP IMAGES ------------------------------------------------------
        BITMAP *MainPage = NULL;
        BITMAP *Instructions = NULL;
        BITMAP *Story = NULL;
        BITMAP *Dodge = NULL;
        BITMAP *CL_STAND = NULL;
    	BITMAP *CL_RUN_RIGHT = NULL;
    	BITMAP *CL_RUN_LEFT = NULL;
    	BITMAP *CITY = NULL;
    	BITMAP *GameoverCL = NULL;
    	BITMAP *GameoverF = NULL;
    	BITMAP *choose = NULL;
    	BITMAP *F_Stand = NULL;
    	BITMAP *F_RUN_RIGHT = NULL;
    	BITMAP *F_RUN_LEFT = NULL;
        
        // In Game --------------------------------------------------------------------
        BITMAP *Light, *Star, *Life, *Ball; 
        BITMAP *Asteroid, *Rocket, *Satellite, *Spaceship;	
        
        // LOAD BITMAP IMAGES ---------------------------------------------------------
        MainPage = load_bitmap("MainPage.bmp", NULL);
        Instructions = load_bitmap("Instructions.bmp", NULL);
        Story = load_bitmap("Story.bmp", NULL);
        Dodge = load_bitmap("DODGE THESE.bmp", NULL);
        CL_STAND = load_bitmap("CL_STAND.bmp", NULL);
    	CL_RUN_RIGHT = load_bitmap("CL_RUN_RIGHT.bmp", NULL); 
    	CL_RUN_LEFT = load_bitmap("CL_RUN_LEFT.bmp", NULL); 
    	CITY = load_bitmap("CITY.bmp", NULL);
    	GameoverCL = load_bitmap("GameoverCL.bmp", NULL);
    	GameoverF = load_bitmap("GameoverF.bmp", NULL);
    	choose = load_bitmap("Choice.bmp",NULL);
    	F_Stand = load_bitmap("Fish_Stand.bmp",NULL);
    	F_RUN_RIGHT = load_bitmap("Fish_Right.bmp",NULL);
    	F_RUN_LEFT = load_bitmap("Fish_Left.bmp",NULL);
    	
        // In Game ----------------------------------------------------------------------
        Light = load_bitmap("Lightning.bmp", NULL);
        Star = load_bitmap("STAR FALLING.bmp", NULL);
        Life = load_bitmap("LIFE.bmp", NULL);
        Ball = load_bitmap("Ball.bmp", NULL);
        Asteroid = load_bitmap("asteroid.bmp", NULL);
        Rocket = load_bitmap("rocket.bmp", NULL);
        Satellite = load_bitmap("satellite.bmp", NULL);
        
        
    	// BITMAP BUFFER ----------------------------------------------------------------
    	BITMAP *buffer = NULL; 
    	buffer = create_bitmap(640,480); //Create an empty bitmap.
    
    
        //PLAYAGAIN LOOP ----------------------------------------------------------------
        do{ 
       
    	   // X AND Y COORDINATES OF THE PICTURES ---------------------------------------	
    	   int MainPagex = 0;
    	   int MainPagey = 0;
    	   int helpSwitch = 0; // For Game movememt
    	   int CITYx = 0;
    	   int CITYy = 0;
           int CL_STANDx = 330;
    	   int CL_STANDy = 440;
           int CL_RUN_RIGHTx = 330;
    	   int CL_RUN_RIGHTy = 330;
    	   int CL_RUN_LEFTx = 0;
    	   int CL_RUN_LEFTy = 0;
    	   int timecounter = 0;
    	   bool out = FALSE;
    	
            for(i=0;i<7;i++){
                for(j=0;j<30;j++){ 
                    GameObject[i][j].x = oddX();
                    GameObject[i][j].life = 0;
                    GameObject[i][j].y = -100;
                    GameObject[i][j].ySpeed = 0;
                }
            }            
                
            // MENU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // Load and Play Music ----------------------------------------------------
            int panning = 128;
            int pitch = 1000;
            int volume = 200;
            SAMPLE *Guile;
            Guile = load_sample("Guile's Theme.wav");
            if (!Guile) {                   //error checking
                allegro_message("Error reading Guile's Theme.wav file");
                return 0;
            } // if loop for Guile
            play_sample(Guile, volume, panning, pitch, 1);
         
        	
            
            // MENU BUTTONS =========================================================================
            while(!key[KEY_F1]) {
                               
                // Display Menu --------------------------------------------------------------------
                mainMenu(MainPage, buffer, Instructions, Story, Dodge);
                
                
                if(key[KEY_ESC]) {
                        return(0);
                }
             }// while loop
            
            while(out==FALSE){
               // Play as GOKU
               if (key[KEY_C]){
                  cursor = 0; 
                  out= TRUE;
                  //printf("%d \n", cursor);
               }
               
               // Play as VEGETA
               if (key[KEY_F]){
                  cursor = 1; 
                  out= TRUE;
                  //printf("%d \n", cursor);
               }
               if(key[KEY_ESC]) { // ends the program if escape button is pressed
                  break;
               }
               draw_sprite(buffer, choose, 0, 0);
               blit(buffer, screen, 0, 0, 0, 0, 640,480);
            }
            // MAIN GAME ==================================================================
            while(!key[KEY_ESC] && Health > -1) //If the user hits escape, quit the program 
        	{               
                
        		// Making Random Objects Fall
                while(speed_counter > 0)
        		{    
                     
                     for(p=0;p<2;p++){
                         if(Change==TRUE){       
                             i_random=rand() % 7;
                             j_counter=rand() % 2+1;
                             for(j=0;j<j_counter;j++){ 
                                 GameObject[i_random][j].life = 1;  
                             }        
                         }// if loop   
                                 
                     }// for loop
                      
                     Change=FALSE;
                    if (key[KEY_F1]){
                        helpSwitch = 2;
                    }
                 
                    if(helpSwitch == 2) {              
                         // MOVING THE PICTURE -----------------------------------------
                         // Right
        
                         if(key[KEY_RIGHT]) 
                         { 
                              CL_RUN_RIGHTx +=15;  
                         }
                         // Left 
                         else if(key[KEY_LEFT])  
                         { 
                              CL_RUN_RIGHTx -=15; 
                              
                         }
                         if(CL_RUN_RIGHTx<0){
                             CL_RUN_RIGHTx=0;
                         }
                         
                         // Restricts Movement to only the screen-------------------------
                         if((CL_RUN_RIGHTx+CL_RUN_LEFT->w)>640){
                             CL_RUN_RIGHTx=640-CL_RUN_LEFT->w;
                         }
                    	// Repeats the action of Falling objects--------------------------
                        for(i=0;i<7;i++){
                            for(j=0;j<30;j++){ 
                                if(GameObject[i][j].life == 1 && GameObject[i][j].y > 480){
                                    GameObject[i][j].life = 0;
                                    GameObject[i][j].y = -200;
                                    GameObject[i][j].x=oddX();
                                }
                            }
                        }
                         
                         speed_counter --;
                         gravity_counter++;
                         if(j_counter>=30){
                             j_counter=0;
                         }
                         
                         // Terminal Velocity ----------------------------------------------
                         if(gravity_counter>30){
                             gravity_counter=0;
                             Change=TRUE;
                         }
                    }// Helpswitch loop   
                    
                    // Different Speeds for different Objects ------------------------------
                   	for(i=0;i<7;i++){
                        for(j=0 ;j<15 ;j++){
                            if(GameObject[i][j].life>0){
                                 if(gravity_counter%3==0){
                                     GameObject[0][j].ySpeed++;//Light
                                 } 
                                 if(gravity_counter%5==0){
                                     GameObject[1][j].ySpeed++;//Star
                                     GameObject[6][j].ySpeed++;//Satellite
                                 }                          
                                 if(gravity_counter%7==0){
                                     GameObject[2][j].ySpeed++;//Life
                                     
                                 }                          
                                 if(gravity_counter%9==0){
                                     GameObject[4][j].ySpeed++;//Asteroid                            
                                     GameObject[5][j].ySpeed++;//Rocket
                                 }                          
                                 if(gravity_counter%11==0){
                                    GameObject[3][j].ySpeed++;//Ball
                                     
                                 }   
                                 if(GameObject[i][j].ySpeed>10)
                                     GameObject[i][j].ySpeed=10;
                                 GameObject[i][j].y+= GameObject[i][j].ySpeed;    
                            }// if loop for gravity 
                                      
                        }// for loop for j
                    }// for loop for i   
                          
                        
                }                          
                // BUFFERING -----------------------------------------------------------			
                draw_sprite(buffer, CITY, CITYx, CITYy);
                          
                // Making the Objects Fall
               	for(i=0;i<7;i++){
                    for(j=0; j<15; j++){ 
                        if(i==0 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Light, GameObject[i][j].x, GameObject[i][j].y);
                            }
                        
                        if(i==1 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Star, GameObject[i][j].x, GameObject[i][j].y);
                            }
                            
                        if(i==2 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Life, GameObject[i][j].x, GameObject[i][j].y);
                            }
                            
                        if(i==3 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Ball, GameObject[i][j].x, GameObject[i][j].y);
                            }
                            
                        if(i==4 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Asteroid, GameObject[i][j].x, GameObject[i][j].y);
                            }
                            
                        if(i==5 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Rocket, GameObject[i][j].x, GameObject[i][j].y);
                             }
                             
                        if(i==6 && GameObject[i][j].life==1){
                            draw_sprite(buffer, Satellite, GameObject[i][j].x, GameObject[i][j].y);
                            } 
                           // COLLISION AND HEALTH 
                           if(Collision(GameObject[i][j].x, GameObject[i][j].y, 99, 79, CL_RUN_RIGHTx, CL_RUN_RIGHTy, 80, 46)== true && hurt == false){
                               if(i == 2){
                                  Health +=2;
                                  }// if loop for i
                               Health -=1;
                               GameObject[i][j].life=0;
                               hurt = true;
                           }// if loop for Collision
                             
                    }//for loop for j
                    
                    
                }// for loop for i             
                
                       
                // Positioning the Goku acc. to the Keys
                if (cursor == 0){
                    if (!key[KEY_RIGHT] && !key[KEY_LEFT])
                    {
                        draw_sprite(buffer, CL_STAND, CL_RUN_RIGHTx, CL_RUN_RIGHTy);                 
                    } // if loop
                    
                    else if(key[KEY_RIGHT]) 
                    {  
                        draw_sprite(buffer, CL_RUN_RIGHT, CL_RUN_RIGHTx, CL_RUN_RIGHTy); 
                    } // else if
                    
                    else if(key[KEY_LEFT])  
                    { 
                        draw_sprite(buffer, CL_RUN_LEFT, CL_RUN_RIGHTx, CL_RUN_RIGHTy); 
                                  
                    }// else if 
                }
                            
                // Positioning Vegeta acc. to keys
                else if (cursor == 1){
                    if (!key[KEY_RIGHT] && !key[KEY_LEFT])
                    {
                        draw_sprite(buffer, F_Stand, CL_RUN_RIGHTx, CL_RUN_RIGHTy);                 
                    } // if loop
                    
                    else if(key[KEY_RIGHT]) 
                    {  
                        draw_sprite(buffer, F_RUN_RIGHT, CL_RUN_RIGHTx, CL_RUN_RIGHTy); 
                    } // else if
                    
                    else if(key[KEY_LEFT])  
                    { 
                        draw_sprite(buffer, F_RUN_LEFT, CL_RUN_RIGHTx, CL_RUN_RIGHTy); 
                                  
                    }// else if 
                }
                
                timecounter ++;
                if (timecounter > 100){
                   hurt = false;    
                   }  
                if (timecounter > 100){
                   timecounter = 0;
                   }         			
                blit(buffer, screen, 0,0,0,0,640,480); 
                clear_bitmap(buffer); 
                		   
            }
            clear_bitmap(buffer);
            
        //////////////////////////////////////////////////////////////////////////
            while(!key[KEY_ENTER]){
                                 if (cursor == 0){
                                    blit(GameoverCL, screen, 0,0,0,0,640,480);
                                 }
                                 else if (cursor == 1){
                                    blit(GameoverF, screen, 0,0,0,0,640,480);
                                 }
            }// while loop
            
            if (!key[KEY_ENTER]){
                                Playagain = false;
                                }// if loop
        }
        while(Playagain = true);// While loop for Playagain
        ////////////////////////////////////////////////////////////////////////////
    	
        					
    	//RELEASE BITMAP DATA ------------------------------------------------------
    	destroy_bitmap(MainPage);
    	destroy_bitmap(Instructions);
    	destroy_bitmap(Story);
    	destroy_bitmap(Dodge);
    	destroy_bitmap(CITY);
    	destroy_bitmap(CL_STAND);
    	destroy_bitmap(CL_RUN_RIGHT); 
        destroy_bitmap(CL_RUN_LEFT);
        destroy_bitmap(F_Stand);
    	destroy_bitmap(F_RUN_RIGHT);
    	destroy_bitmap(F_RUN_LEFT); 
    	destroy_bitmap(Light);
    	destroy_bitmap(Star);
        destroy_bitmap(Life);
        destroy_bitmap(Ball);
        destroy_bitmap(Asteroid);
        destroy_bitmap(Rocket);    
        destroy_bitmap(Satellite);
        destroy_bitmap(GameoverCL);
        destroy_bitmap(GameoverF);
        destroy_bitmap(buffer); 
        
    	
        //EXIT WITH NO ERRORS ------------------------------------------------------
        return 0;  
    } 
    END_OF_MAIN() 
    
    
    
    
    // MENU FUNCTION ====================================================================================
    void mainMenu(BITMAP *MainPage, BITMAP *buffer, BITMAP *Instructions, BITMAP *Story, BITMAP *Dodge) {
        int help = 0;
        int story = 0;
        
        // Display Menu ------------------------------------------------------------         
        draw_sprite(buffer, MainPage, 0, 0);
        blit(buffer, screen, 0,0,0,0,640,480);
        
        // If P is pressed, Pause Music --------------------------------------------
        if(key[KEY_P]){
                       int panning = 128;
                       int pitch = 1000;
                       int volume = 200;
                       SAMPLE *NoSound;
                       NoSound = load_sample("NoSound.wav");
                       if (NoSound == NULL) {                   //error checking
                       allegro_message("Error reading NoSound.wav file");
                      
                       } // if loop for NoSound
                      play_sample(NoSound, volume, panning, pitch, 1);
                      }
        
        
        // If F6 is pressed, display Instructions ----------------------------------
        if(key[KEY_F6]) {
            help = 1;
        }
        while(help == 1) {
                   if(key[KEY_D]){
                   }
                   else{
                        draw_sprite(buffer, Instructions, 0, 0);
                        blit(buffer, screen, 0,0,0,0,640,480);
                   }   
            // If D is pressed, show dodge objects ---------------------------------
            if(key[KEY_D]){
                           blit(Dodge,buffer , 0,0,0,0,640,480);
                           blit(buffer, screen, 0,0,0,0,640,480);
                           if(key[KEY_BACKSPACE]){
                                                  help = 1;
                                                  }
                                                  }
    
    
            if(key[KEY_BACKSPACE]) {
                help = 0;
            }
        }
    
    
        // If F4 is pressed, display Story -----------------------------------------
        if(key[KEY_F4]) {
            story = 1;
        }
        while(story == 1) {
            draw_sprite(buffer, Story, 0, 0);
            blit(buffer, screen, 0,0,0,0,640,480);
    
    
            if(key[KEY_BACKSPACE]) {
                story = 0;
            }
        }
    
    
        // If F7 is pressed, go windowed mode---------------------------------------
        if(key[KEY_F7]) {
            set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0);
        }
        
        // If F8 is pressed, go fullscreen------------------------------------------
        if(key[KEY_F8]) {
            set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);
        }
    }
    END_OF_FUNCTION(mainMenu)
    
    
    
    
    
    
    
    
    // X RANDOM ====================================================================
    int oddX() {
        int valueX = 0;
        valueX =(rand() % 480 + 1) ;
        return(valueX);
    }
    END_OF_FUNCTION(oddX);
    and the character doesnt seem to get hit by the object if it is standing right beneath it !!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Here are some problems for you to fix.

    1. Your main is 400+ lines longs, contains multiple deeply nested loops and is generally impossible to read.

    2. Object GameObject[7][15];
    Create #define constants for the sizes, like say
    #define MAX_GROUP 7
    #define MAX_INSTANCE 15
    Object GameObject[MAX_GROUP][MAX_INSTANCE];


    Then fix ALL your loops to use those constants, so you don't blow off the end of the arrays in some crazy assed manner, like this.
    Code:
            for(i=0;i<7;i++){
                for(j=0;j<30;j++){
                    GameObject[i][j].x = oddX();
                    GameObject[i][j].life = 0;
                    GameObject[i][j].y = -100;
                    GameObject[i][j].ySpeed = 0;
                }
            }
    3. Group your variables into structures.
    Code:
    struct images {
        // DECLARE BITMAP IMAGES ------------------------------------------------------
        BITMAP *MainPage;
        BITMAP *Instructions;
        BITMAP *Story;
        BITMAP *Dodge;
        BITMAP *CL_STAND;
        BITMAP *CL_RUN_RIGHT;
        BITMAP *CL_RUN_LEFT;
        BITMAP *CITY;
        BITMAP *GameoverCL;
        BITMAP *GameoverF;
        BITMAP *choose;
        BITMAP *F_Stand;
        BITMAP *F_RUN_RIGHT;
        BITMAP *F_RUN_LEFT;
         
        // In Game --------------------------------------------------------------------
        BITMAP *Light, *Star, *Life, *Ball;
        BITMAP *Asteroid, *Rocket, *Satellite, *Spaceship; 
    };
    Then things like this
    // LOAD BITMAP IMAGES ---------------------------------------------------------
    MainPage = load_bitmap("MainPage.bmp", NULL);
    and this
    //RELEASE BITMAP DATA ------------------------------------------------------
    destroy_bitmap(MainPage);
    can be moved into separate functions with ease.

    > while(Playagain = true);
    You're using gcc, so add -Wall -Werror to your compiler command line options.
    Make sure you fix all warnings before you even try to run the code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allegro Pong Collision Problems
    By gillypie in forum Game Programming
    Replies: 4
    Last Post: 12-30-2007, 06:05 AM
  2. Pong Clone Collision Allegro
    By gillypie in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2007, 02:42 AM
  3. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  4. Collision Detection Problem
    By kas2002 in forum Game Programming
    Replies: 3
    Last Post: 06-10-2006, 11:04 PM
  5. Allegro bitmap collision? how?
    By elfjuice in forum Game Programming
    Replies: 5
    Last Post: 07-06-2002, 11:45 AM