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:
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:
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!