Breakout collision detection bug.
Hi, I have been creating my own version of break out in Visual Basics 6.0 using bit block transfer in a picture box. All of my logic so far has been pretty problem free however recently when implementing my logic for the collision detection on the paddle which picks up whether the ball has hit the paddle resulting in a collision or falls out the bottom of the court. I have tried many collision detection methods but sadly with little success. The problem I have been having is that when the ball hits the paddle at any other than 45 degrees the ball goes through the paddle and no collision is triggered about 50% of the time which makes the game pretty much untestable let alone playable. The code I have used to detect the collisions is below:
Code:
If (ball_X(0) >= paddle_array(0) Or ball_X(1) >= paddle_array(0)) And (ball_X(0) <= paddle_array(1) Or ball_X(1) <= paddle_array(1)) And (ball_Y(0) >= paddle_array(2) Or ball_Y(1) >= paddle_array(2)) Then 'If the ball hits the paddle then the followind code is exicuted
If ball_Y(1) > paddle_array(2) Then 'Code to stop the ball getting stuck on the paddle
Let ball_Y(0) = (paddle_array(2) - BallSprite.ScaleHeight) 'moves the ball up to prevent it getting stuck on the paddle
'Let ball_movement_X = (ball_movement_X * (0.01 * ((((paddle_array(0) + (0.5 * paddle_array(1)) - ball_X(0)) ^ 2) ^ 0.5)))) 'changes the angle of reflection of the ball
Let ball_movement_Y = (ball_movement_Y * -1) 'reverses the ball`s y axis movement
Call BallBounceSound 'Calls the procedure to make the balls bouncing sound
Else
'Let ball_movement_X = (ball_movement_X * (0.01 * ((((paddle_array(0) + (0.5 * paddle_array(1)) - ball_X(0)) ^ 2) ^ 0.5)))) 'changes the angle of reflection of the ball
Let ball_movement_Y = (ball_movement_Y * -1) 'reverses the ball`s y axis movement
Call BallBounceSound 'Calls the procedure to make the balls bouncing sound
End If
Else
If (ball_Y(0) >= (GameOutput.ScaleHeight)) Or (ball_Y(1) >= (GameOutput.ScaleHeight)) Then 'The following code is exicuted if the ball falls out of bottom of the court
Let ball_number = (ball_number - 1) 'Subtracts one from the number of balls
Let ball_X(0) = ball_start_X 'Sets the ball_X to its start positions
Let ball_Y(0) = ball_start_Y 'Sets the ball_Y to its start positions
End If
End If
The variables I am using are:
ball_X(0) which stores the top left x-cordinate of the ball
ball_X(1) which stores the bottom right x-cordinate of the ball
ball_Y(0) which stores the top left y-cordinate of the ball
ball_Y(1) which stores the bottom right y-cordinate of the ball
paddle_array(0) which stores the top left x-cordinate of the paddle
paddle_array(1) which stores the bottom right x-cordinate of the paddle
paddle_array(2) which stores the top left y-cordinate of the paddle
paddle_array(3) which stores the bottom right y-cordinate of the paddle
ball_movement_X which contains a value which is added to the balls X coordinates in order to facilitate movement in the X-axis
ball_movement_Y which contains a value which is added to the balls Y coordinates in order to facilitate the movement in the Y-axis
ball_movement which contains a value which is used to allow for a consistent speed of the ball after collisions.
This logic is in a timer with all the collision detection for the whole game as well as the ball movement for the whole game. The fact that I am using an if statement for the collision detection I would have thought would have meant that the ball shouldn’t be able to fall through the paddle. Also when the ball falls through the paddle it triggers the code for when the ball is out with the courts boundaries, this means the coordinates of the ball must be being handled properly making this bug seem a bit of an anomaly. I have shown a few programming friends it and they haven’t been able to help so it would be good if anyone has any ideas that might help solve this issue. :)
Re: Breakout collision detection bug.
ah, the old breakout game :) i made one using this tutorial:
http://www.quantumstate.co.uk/pong.html
i then modded it to make a proper pong game (that tut isnt actually pong - it's breakout without the bricks).
hope this helps,
shwhjw