Sign in to follow this  
Asros

Need help with collision urgently

Recommended Posts

Asros    100
What is the easiest and quickest way to see on what side two sprites collide? I'm making a zelda style game. I can get the collision to be detected, but it's only true or false. I can't push the character away from the wall if I don't know on which side did the collision happen. I'm using bounding boxes at the moment and the collision detection sees if all the sides from box A are outside of box B and then returns true or false. With this setup I can get it to work on top and left or bottom and right, but I can't get it to work on both at the same time. I would appreciate if someone could tell me with code what is the best way to get over this problem. This is part of school project and I only have few days left. I don't have the time to make anything complex so if anyone could show me a simple way to do this, I would be really grateful. PS. I don't have too much experience with programming. PS2. I'm using c++ / SDL

Share this post


Link to post
Share on other sites
jyk    2094
It looks like this is a continuation of your thread here. (Sorry, when I mentioned that the topic was more 'Math & Physics' oriented, I didn't mean to suggest cross-posting. I probably should have been more clear about that though.)

Anyway, the answers in my other thread still apply. Of the solutions I proposed, I think that computing the minimum translational vector for the two boxes would probably be the most reasonable here.

You should be able to find info on how to do this if you search the forum archives for 'aabb mtd', 'aabb minimum translational distance', or 'aabb sat'.

In short, you need to compute four values representing how far box A (or B) needs to be moved in each of the four cardinal directions in order to resolve the collision. Each of these values can be computed by simple subtraction - if you draw a picture or two, it should become clear what value needs to be subtracted from what value in each case to yield the correct result.

Once you have the four values, select the one with the smallest magnitude; this will tell you which direction to move (+/- cardinal x or y axis), and how far.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this