Sign in to follow this  

How do I check for collisions directionally?

Recommended Posts

I'm using the technique from this tutorial to check if a ball has hit a block in a breakout style game. Basically you check if any of the edges of one box are inside the other box and of so they are colliding. The problem is though that this only tells me whether there is a collision but what I want to find out whether the ball is colliding with the sides or the top/bottom of the block so i can bounce the ball accordingly. Can anyone help?

Share this post

Link to post
Share on other sites
I guess it depends on how accurate you want to be.

Right off the top of my head, I think that you'd first need to prioritize horizontal collision or vertical.

pseudo code
Is the upper right or left corner in the other object?
----Yes. But only by X much or more.
--------X much or less: Then it a horizontal collision.
--------more than X: It was vertical.
How about the lower right corner?
----(same as above pretty much)
What if only a side is in?
----(then you know which direction by which side)

I've never tested code along these lines (my game uses only circular collision), but it's my off-the-top-of-my-head hypothetical solution.

Share this post

Link to post
Share on other sites
Well, I came across the same problem with Breakout and what I did was I tested the velocity and co-ords of the ball.

If the ball collides with a block and the ball's X velocity is positive then it must be going right, and if it's X position is less than the block, then it's to the block's left; therefore the ball hit the block's left side.

I think I may have resized my collision boxes a bit too... I messed around a bit, but that was the basic gist of it.

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