Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
Possible duplicate: OBB vs OBB Collision Detection –  Byte56 Jan 8 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 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 at 19:20
    
Please see my edit. –  user3150201 Jan 8 at 19:23
    
Solutions go in the answers section, if you have an answer post it there. Thanks. –  Byte56 Jan 8 at 20:21

1 Answer 1

Finally found a solution!

This solution isn't perfect, sometimes sprites get a little stuck. But it doesn't involve all the math stuff that people like me struggle with. I'm sharing this because maybe it will help someone.

Everytime I move a sprite, I save it's previous location in a variable. If the sprites collide, I reset it's location to the location it had before the collision. This will take the sprite only a small number of pixels back, so the 'bouncing back' won't be noticable. It will prevent the sprite from overlapping the other sprite.

Thanks for those who tried to help :)

share|improve this answer
2  
Haha okay. So if you like that kind of approach, you could check the current direction of movement by comparing the stored and the current position and then multiply the vector responsible for movement my the skalar -1. This would give you a trivial collision effect. –  Peter Jan 8 at 22:46
1  
yea maybe you should learn math, it won't hurt you. –  Arne Feb 7 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.