Jump to content
  • Advertisement
Sign in to follow this  
roccog

[C++/SDL] A little collision problem

This topic is 4619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm a beginner! :D I'm writing a 2d game with SDL and C++... I have a little problem with my collision function, in particular it works well when the sprite is moving on the 4 standard direction: 1. nord <-> sud 2. west <-> est but it gives me some problems when the sprite has to move from nord-est to sud-ovest... for example.
void controllo( Sprite *a, Sprite *b, int dir, int diag2, int diag4) {
   
   
      if ( dir == 2 ){
      
      if ( ( a -> x > ( b->x - ( a -> img -> w) ) ) && ( a -> x < b -> x ) && ( a -> y < b -> y + ( a->img->h)) && (a->y > b->y - ( a->img->h) ) ){
      /* controllo che lo sprite non superi il lato sinistro delle x dell'altro sprite */
      SDL_WM_SetCaption("Lato sinistro delle x !!!", "");
      /* cerco di settare la posizione dello sprite quando questo supera il lato dx */
      a->x = ( b->x - (a ->img->w));
      }
      
      }
     
      
      else if ( dir == 1 ){
      
      if( ( a-> x < b->x+ ( b ->img ->w) ) && ( a->x > b->x - ( a->img->w) + ( b->img->w ) ) && ( a->y < b->y + ( a->img->h ) ) && (a->y > b->y - ( a->img->h)) ){ 
      /* controllo che lo sprite  non superi il lato destro delle x dell'altro sprite, venendo da destra verso sinistra */
      SDL_WM_SetCaption("Lato destro delle x !!!", "");
      /* setto la posizione */
      a->x = ( b->x + ( b->img->w ) );
      }
      
      }     
     
     else if ( dir == 4 ){
     if( ( a->y > b->y- ( a->img->h ) ) && ( a->y < b->y ) && ( a->x > b->x - ( a -> img-> w ) && ( a->x < b->x + ( b->img->w) )) ){ 
     /* controllo che lo sprite non superi il lato superiore delle y andando dall'alto verso il basso */
     SDL_WM_SetCaption("Lato superiore delle y !!!", "");
     /* setto la nuova posizione */
     a->y = b->y - ( a->img->h );
     }
     
     }  
       
     else if ( dir == 3 ){
     if( ( a->y < b->y + ( b->img->h) ) && ( a->y > b->y ) && ( a->x > b->x - ( a->img->w) ) && ( a->x < b->x+( b->img->w) ) ){ 
     /* controllo che lo sprite non superi il lato delle y andando dal basso verso l'alto */
     SDL_WM_SetCaption("Lato inferiore delle y !!!", "");
     /* setto la posizione corretta */
     a->y = b->y + ( b->img->h );
     }
     
     }
     
       
     }
See this: http://www.mangaitalia.net/sprite1.gif if i try to go on the right and i press also the down-button my sprite go over the black rettangle... i'm sorry for my English, i'm italian... i hope that pic will help u to understand my problem...

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!