# Thread: Ball Maze collision detection

1. ## Ball Maze collision detection

I would like to write a ball-maze game but I am wondering about collision detection of the ball with horizontal and vertical barriers. Would simply checking at the top, bottom, left and right sides of the ball be adequate. I plan to use the color 'Point' method to detect barriers. When the ball rounds the corner of a barrier and I invert the X or Y values (as necessary), this does not seem like it would accurately detect hitting a barrier corner. Perhaps checking 8 or more points around the ball will work in which case both the X and Y values will be adjusted at, say, 45 degree angles. What would be a good starting philosophy to implement ball/barrier detection?

PS: using VB6

2. ## Re: Ball Maze collision detection

If you use a round ball then all you need to do is check the distance between a barrier and the center of the ball.
If it is equal or less than the radius of the ball there is a collision.

If you use only vertical and horizontal barriers thi is simple.
For a vertical barrier both ends have the same X and different Y coordinates.

Test the ball's horizontal position relative to the barrier.
-Left of barrier - ball-radius, no collision, end.
-Right of barrier + ball-radius, no collision, end.
Test the ball's vertical position relative to the barrier.
-Above top-point + ball-radius, no collision, end.
-Below bottom-point - ball-radius, no collision, end.
-Between top-point and bottom-point, collision, end.
-Above top-point, reference top-point.
-Below bottom-point, reference bottom-point.
Test distance between reference and ball.
-Distance less than ball-radius, collision, end.
No collision, end.

3. ## Re: Ball Maze collision detection

Using the ball's center as a reference makes good sense. I don't want to check every single barrier in the picture. I may be able to break it down into zones based upon where the ball is or limit the number of checks somehow.
I understood the simple cases: -Left of barrier - ball-radius, no collision, end.

-Above top-point, calculate distance between ball-center and top-point.
-Below bottom-point, calculate distance between ball-center and top-point.

As you probably know, doing a little thinking before coding can turn a mess of code into a simpler solution. That is why I am trying to consider some of the problems before putting down code.

I also plan to have black holes for the ball to drop down and the player loses.

4. ## Re: Ball Maze collision detection

I modified it a bit.

If the ball is above the barrier then it would first collide with it's top.
Therefore you would need to know if the ball is below or above the barrier to know against which end you need to test it's distance.

I attached a picture.
If the ball is in the bright green zone then it is more than one radius from the barrier and therefore safe, regardless of vertical position.
If it is closer then we must test where it is vertically.
The dark green zones are safe as they are too far above or below the barrier's ends.
The red zone is a collision for certain.
Untill now we could test everything by just adding and subtracting
In the yellow zones the ball could be safe or have a collision.
Only if it is in a yellow zone, no more than one radius above or below the barrier's ends, do we need Pythagoras.

Of course, for a horizontal barrier you just rotate the picture and replace above for right, below for left, etc.

5. ## Re: Ball Maze collision detection

Thanks for the ideas. If I use color 'Point' at a number of places around the ball's center I won't have to worry about checking for each barrier.
Also, I haven't decided to either to use the mouse or arrow keys to tilt the board.
I have written many simple games. I enjoy trying to figure out the logic of the code from an academic perspective.

6. ## Re: Ball Maze collision detection

You could use a 2D per-pixel or rectangular collision detection method. If you use per-pixel you basically get the bounds of the object/sprite and then get the color of the both pixels of the object at each point, and then check if the pixels are transparent and if they are you know there is a collision.

The rectangular collision detection is much simpler just check if the object intersect basically. So get the bounds of the rectangle and just compare.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured