Hi everyone,
I have been working on creating a break-out type video game (ball bounces off a paddle and hits bricks and they disappear) and have been experiencing some issues with the accuracy of my collision detection. I am hopeful that someone here might be able to give me some advice on how to solve the problem I am having.
Here is an image to describe my issue:
Basically, my routine goes like this:
For every frame, I trace around the ball using sin & cos, every 18 degrees (I chose 18 degrees so that it's less cpu intensive) and store those values into two variables: x_circle and y_circle (different than the ball's x and y variables which reference the center of the ball object).
During the cycle around the 360 degrees of the ball, I check if the x & y match within the bounds of a brick... If it does, then I get the side of the brick by using arc tangent... Basically, I do brick_angle = atan(brickHeight / brickWidth)-- This is illustrated by the white diagonal line through the brick.
then I get the angle of the ball in relation to the brick-- this is done by doing ball_angle = absolute value of atan(ball.y - brick.y / ball.x - brick.x) -- Brick.x & brick.y make up the green dot, and ball.x & ball.y make up the purple dot. The atan function is illustrated by the blue triangle.
Then in my program, I do a conditional check-- If the ball_angle is > the brick_angle, then the ball should reverse vertical direction.. If it's < than the brick_angle, then it should reverse horizontal direction. This is how I am detecting if the ball is hitting the side of the brick or not-- and this is where my problem lies.
The problem is, as you can see in the image, the ball's angle is wider than the brick's angle, so when the ball hits the corner, it bounces horizontally instead of vertically because the ball angle is wider-- despite the fact that the ball is hitting the corner... If that makes sense?
So basically my suspicion is that if this were to happen:
Then that would behave properly-- But only because the angle of the ball happens to match where the brick's angle is.
.. Then I thought-- maybe I should change it so that I am doing the atan function off of the x_circle and y_circle--- in other words: ball_angle = absolute value of atan(y_circle - brick.y / x_circle - brick.x) instead of the ball's center, which would give me something like this:
But that still seems to make the angle too wide upon collision...
... So the last thought I had is, perhaps I should not be using brick.y and brick.x (the brick's center), but rather the corner-- This would yield:
-- Which seems like it might work, however... I am fuzzy at the moment as to how I would know which corner the ball is hitting on any given brick...
So before I go down that road, I thought I'd post here and see if anyone has thoughts on how I should solve this problem?
Thank you.
-patrick