Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with this bouncing code??  (Read 1578 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Help with this bouncing code??
« on: October 30, 2011, 09:07:08 pm »
My "rocks" are not bouncing off of each other in the most natural way, and I was wondering if anyone would be willing to look at my code and give me some advice on a better way to accomplish better rect-wall-detection of my sprite and the sprite it is bouncing off of.

Here is the code I am using to cause my rocks to bounce off of each other:

Code: [Select]
for(unsigned sprite1 = 0; sprite1 < rockbox.size(); sprite1++){
    rockbox[sprite1].Update();
    Window.Draw(rockbox[sprite1]);

    for(unsigned sprite2 = 0; sprite2 < rockbox.size(); sprite2++){
        if(sprite1 != sprite2){
            bounce_sprites(rockbox[sprite1], rockbox[sprite2]);
        }
    }
}


Here is the function bounce_sprites:

Code: [Select]
void bounce_sprites(SubSprite& sprite1, SubSprite& sprite2)
{
    if(sprite1.Rect.Intersects(sprite2.Rect)){

        if(sprite1.Rect.Top <= sprite2.Rect.Top + sprite2.Rect.Height
           and sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top + sprite2.Rect.Height){

            switch(sprite1.Direction){
                case UPRIGHT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top
           and sprite1.Rect.Top <= sprite2.Rect.Top){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = UPRIGHT;
                    break;
                case DOWNLEFT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left
           and sprite1.Rect.Left <= sprite2.Rect.Left){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                case UPRIGHT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left <= sprite2.Rect.Left + sprite2.Rect.Width
           and sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left + sprite2.Rect.Width){

            switch(sprite1.Direction){
                case DOWNLEFT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = UPRIGHT;
                    break;
                default:
                    break;
            }
        }
    }
}


If anyone is willing and able to give me some advice in the general direction of what I could be doing better I would be very appreciative!

I prefer to not import a huge library like Box2D, since this is about the extent of the physics that I will be doing in this game.

Thanks so much!

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Help with this bouncing code??
« Reply #1 on: October 31, 2011, 07:24:53 am »
Nevermind... It's actually working very well now that I have more rocks on the screen. Also, it looks far more natural when I am using rock images instead of colored squares...