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

  • \$\begingroup\$ Possible duplicate: OBB vs OBB Collision Detection \$\endgroup\$ – MichaelHouse Jan 8 '14 at 18:43
  • \$\begingroup\$ @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? \$\endgroup\$ – DampeS8N Jan 8 '14 at 19:04
  • \$\begingroup\$ 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. \$\endgroup\$ – user3150201 Jan 8 '14 at 19:20
  • \$\begingroup\$ Please see my edit. \$\endgroup\$ – user3150201 Jan 8 '14 at 19:23
  • \$\begingroup\$ Solutions go in the answers section, if you have an answer post it there. Thanks. \$\endgroup\$ – MichaelHouse Jan 8 '14 at 20:21

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

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Peter Jan 8 '14 at 22:46
  • 1
    \$\begingroup\$ yea maybe you should learn math, it won't hurt you. \$\endgroup\$ – Arne Feb 7 '14 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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