# How to prevent a sprite to move in an angle that will lead to collision

I have two sprites that can move in any angle. The sprites are rectangular.

I created bounding boxes for both the sprites. These boxes rotate whenever the sprite rotates. They always surround the sprite and only the sprite. (Exact fit).

I have succesfully implemented collision detection for the two bounding boxes. I did this by checking wether box1 contains any of the vertices of the box2, and the opposite. (I know this way is not perfect, since it is possible for two rectangles to intersect without having each other's vertices inside themselves, but for my needs it's good enough). So now I know when the two sprites collide.

But I have a problem with reacting when the two sprites collide.

When these sprites collide, I want to prevent them from overlapping each other (any more than a few pixels). In other words, upon collision, I want to allow the sprite to move only in a direction where it won't go deeper into the other sprite.

As I said, each sprite can move freely in any angle (the game is for two players, each controls a sprite).

EDIT: To word it differently - Upon collision of two sprites, I need to find out the range of angles a sprite can move, that will allow it to to move out of the collision - this is so I can prevent the user from moving the sprite in any other angle, upon collision.

How can I do that? Thank you for your help

• Possible duplicate: OBB vs OBB Collision Detection – MichaelHouse Jan 8 '14 at 18:43
• @Byte56 I believe this question is asking about how the two sprites should bounce off of each other, and not how to detect collision. Is this correct? – DampeS8N Jan 8 '14 at 19:04
• I already know how to detect collision, but am not exactly asking how they should bounce of each other. I have to rectangles colliding. I need to find out in what angles they can move, to get out of the collision, so I can prevent them from moving in any other angle. – user3150201 Jan 8 '14 at 19:20
• Please see my edit. – user3150201 Jan 8 '14 at 19:23
• Solutions go in the answers section, if you have an answer post it there. Thanks. – MichaelHouse Jan 8 '14 at 20:21