Public Group

# How do I check for collisions directionally?

This topic is 3996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using the technique from this tutorial http://lazyfoo.net/SDL_tutorials/lesson17/index.php 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 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 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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013301
×