Advertisement

Collisions.. How do I tell which side of the Rect hit something?

Started by January 02, 2001 04:32 PM
5 comments, last by compumatrix 24 years ago
I am a newbie to game programming working on writing a 2D game engine to use for a game that I plan to make. I know how to detect collisions, but how do I determine which side of the bounding box hit another object, so i can determine if I need to revers the delta y''s or the delta x''s. I have been trying to figure this out for a while. I was thinking that I could see if the width of the overlapping rectangle was bigger than the height. But this would pose a problem if the overlapped rectangle happened to be a square, as well as other situations. So, how would I go about figureing out if the delta x''s or the delta y''s should be reversed? I a may just be overlooking the solution to this problem. Any help is greatly appreciated. Thanks, Jon
If you have the object positions (or just take a point in the bounding box) this is simple. check whether objectA''s x is less than objectB''s x and this means it is on the left!,
x1x0 = right y1y0 = ^ read above.
and if x or y aret he same for both objects, you can avoid the bounding box check for that dimension.
Advertisement
I don''t really understand what you are saying. I don''t see how subtracting the x locations and the y locations of the rectangles will help me figure out if I should reverse the delta_y''s or the delta_x''s. I need to figure out if the rectangles collided top to bottom (need to reverse delta_y''s) or left to right (need to reverse delta_x''s). Or maybe I am just misunderstanding what you are saying.
I''m no expert at this, but in my BreakOut clone that I wrote in VB a while back I used the method you mentioned above. I took the intersection of the two bounding rectangles, and if it''s wider than tall reverse the Y delta, and it it''s taller than wide, reverse the X delta.

This doesn''t pose too much of a problem with squares if you use the right ifs. This, however, won''t tell you if it hit the right or left side, just that you hit one of the sides.

I used completely different collision detection for the paddle in the game, it was really lame, but it broke the paddle into sections, with each section adding (or subtracting) a little more to the balls X delta allowing for some control of ball speed.

Micah
quote: I used completely different collision detection for the paddle in the game, it was really lame, but it broke the paddle into sections, with each section adding (or subtracting) a little more to the balls X delta allowing for some control of ball speed.


Don''t diss that technique, that''s what I used!

To do the collision detecting for my the ball in my pong game I simply had a series of if statments:

if (BallY < 0)
.....

That would mean the ball hit the top of the screen.

if (BallX > WINDOW_SIZE)
.....

Here, the ball hits the right (where WINDOW_SIZE is the size of your screen)

Hope it helps

Peon
Peon
Thanks for the help, I will see if in my implimentation seeing if the width or height of the intersecting rectangle poses a real problem, if it does I will have to think up some other method....
Advertisement
When I check the width and height of the overlapping rectangle, I have problems with it... It works fine until once in a while when a couple of the rectangles in my test program decide to get stuck together and overlap momentarily. What other methods are there to check if the delta x''s or delta y''s should be reversed? or perhaps just something that I could use when the width and height of the overlapping rectangle are equal.

PS, sorry for kind of dragging this topic on, but I''d really like to be able to figure this out.

--Jon

This topic is closed to new replies.

Advertisement